Advertisements

Hi, this is a very easy question but very often asked by beginners. The
point here is you should know how to decrypt pointer declarations.
Please note the examples may not be very readable if you do not use a
fixed width font to read. These and many other topics related to
pointers are explaine din detail in an article put up on my website:

The steps normally followed to decrypt a pointer declaration are:
1. Start with the name that will identify the pointer, which is known
as the identifier.

2. Move to the right until you encounter a right-parantheses [symbol =
")" ] or reach the end. Do not stop if the () brackets are used to pass
parameters to a function. Also do not stop on encountering brackets
used with arrays: [ ].

3. Now go left of the identifier to continue deciphering the
declaration. Keep going left until you find a left-parantheses [the
symbol "(" ] or reach the end. Do not stop if the brackets are used to
pass parameters to a function.

4. The whole interpretation should be a nice long sentence.

This will not be clear without a couple of examples. So here they are:

1. int *ptr[5]

ptr is an array of size 5... [nothing more on the right]
....of pointers to an int [nothing more to read]

2. int (*ptr)[5]

ptr is... [ the ) indicates: "stop reading further to the right".
Now read left.]
....a pointer to... [we encounter a (. Now read further right.]
....an array of size 5... [nothing more on right]
....of type int. [nothing more on left]

3. ( * f1 ( ) ) ( )
1 2 3 4 5 6

f1 is a function... [we see a ), character 5. Now read left.]
[The arguments in () at position 4 should be read as one character. Do
not stop at () that pass parameters to a function.]
....returning a pointer... [we see a (, character 1. Now read right.]
....to a function... [nothing more on right]
....returning an int [not specified, but implicit]

Share This Page

Welcome to The Coding Forums!

Welcome to the Coding Forums, the place to chat about anything related to programming and coding languages.

Please join our friendly community by clicking the button below - it only takes a few seconds and is totally free. You'll be able to ask questions about coding or chat with the community and help others.
Sign up now!