Click here to Skip to main content
15,886,012 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
(Newb here so please go easy on terming in your anwer if possible, many thanks!)

My C textbook says (if I understand it correctly):

type *pointername;
//type here decides the data type of the target of the pointer

type *arrayname[SIZE];
//type here also decides the data type of the targets of the pointers in the array ... but now where's the typing of the array elements?

type (*arrayname[SIZE])(typeofarguments);
//type now decides the type of returned values of function pointed by the pointers in the array??????? omg but now where are typing of pointer targets AND array elements???

How can type in formats so similar have so drastically different meanings? I'm so confused! Any clarification is greatly appreciated!


Another question: By the way, is '*' in definition of pointers still considered as an operator? If not, what is it exactly?
Posted
Updated 10-Dec-14 7:22am
v4

Your book's not doing you a lot of favours by using "type" the way it does. The type of a declaration is everything in a declaration apart from the name. So when someone writes:

C++
int (*a[100])(int);


the type of a is int (*[100])(int); The author is saying "a is an array of 100 pointers to functions that take an integer as its only parameter and returns an integer" or "the type of a is an array of 100 pointers to functions that take an integer as its only parameter and returns an int". The type of each element in the array is "pointer to a function that takes an integer as it's only argument and returns an integer.

Normally you'd use typedefs to make this a bit more readable:

C++
typedef int (*func_ptr)( int );

func_ptr a[100];

which has the same meaning as the first declaration but tells you in a form that's a bit easier to read what the type of each element of the array is and it shows a lot more directly that a is an array.

The best coverage of reading C declarations I've seen is in Expert C Programming by Peter Van Der Linden. I'd recommend dashing to Amazon or your local library and seeing if you can get a copy - it's a really lucid account of C's parsing rules for declarations and explains it far better than I can.
 
Share this answer
 
Comments
Member 11212276 11-Dec-14 5:44am    
Thanks for:
The type of a declaration is everything in a declaration apart from the name.
and Expert C Programming
!!
See http://cdecl.org/[^]: You enter your C-declaration and get "prosa" description of the declaration.

To read a C-declaration, do the following iterative rule:

  1. start at the entity name
    --> "declare name as "
  2. if there is a ( ... ) to the right of the current location
    --> "function (...) returning "
  3. else if there is a [ ... ] to the right of the current location
    --> "array ... of "
  4. else if there is const or volatile to the left of the current location
    --> "const " or "volatile "
  5. else if there is * to the left of the current location
    --> "pointer to "
  6. else if the current location is nested in ( ... )
    --> nothing
  7. else if there is a typename to the left of the current location
    --> "typename "
  8. if all "eaten up"
    --> done
  9. else goto 2)


E.g.
type *pointername;                        --> 1,5,9,7,8
type *arrayname[SIZE];                    --> 1,3,9,5,9,7,8
type (*arrayname[SIZE])(typeofarguments); --> 1,3,9,5,9,6,9,2,7,8

I.e. the above mentioned declarations translate into:
- declare pointername as | pointer to | type
- declare arrayname as | array SIZE of | pointer to | type
- declare arrayname as | array SIZE of | pointer to | function (typeofarguments) returning | type

Cheers
Andi

PS: C++ is a bit more complex with respect to references (e.g. & and && goes with *), unnamed entities (e.g. in alias declaration which are analogous to typedefs: find the location where the starting name would be located), deducing if a name is type or a class member name, pointer to member, etc. The good thing is, one can easily extend the above rules to fit C++ declaration (or to reject them so they are expressions). E.g. x*y; is a declaration if x is a type, otherwise, it is an expression where the result of the operator call is not stored.
 
Share this answer
 
v6
Comments
Member 11212276 11-Dec-14 5:44am    
Thanks for your awesome rules!
Andreas Gieriet 14-Dec-14 15:01pm    
You are welcome!
Cheers
Andi
OK, lets think about this.
C++
int i;
Defines an integer veriable - it holds a number.
C++
int *pi;
Defines a pointer to an integer veriable - it holds the address of a number.
C++
int ai[10];
Defines an array of integers - it holds a sequence of numbers.
C++
int *api[10];
Defines an array of pointers to integers - it holds a sequence of pointers to numbers.

The type defines the eventual type of variable you will be working with: when you dereference the variable, and decide which array element you are dealing with (if any) that is what you will be able to put into and take out of the variable.
So all these are valid:
C#
i = 666;
*pi = 666;
ai[3] = 666;
*ai[3] = 666;

With an array of pointers to integers, the type is int, the "*" says "this is pointers to" and is additional information that defines the actual type of the array elements.
 
Share this answer
 
v2
Comments
Member 11212276 11-Dec-14 5:45am    
Thanks for:
The type defines the eventual type of variable you will be working with
and the "*" says "this is pointers to" and is additional information that defines the actual type of the array elements.
!!
OriginalGriff 11-Dec-14 6:08am    
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900