0

can any one explain the following instructions:

int *c[10];

char *(**n)(void);

float *(**r(void))[6];


short *(**v(void))(int);


long *(*(*(*z)(void))[7])(void);
Cœur
  • 37,241
  • 25
  • 195
  • 267
ibnyadam
  • 45
  • 6
  • The first one is getting the data at the address stored in c[10] – user623879 Apr 13 '11 at 06:22
  • Are those from a real program? (I hope not) - What do you need them for? Is this some homework you have to do? What do you mean by explaining? – slhck Apr 13 '11 at 06:23
  • 2
    @user623879: no, it's an array of 10 pointer to int. – Mat Apr 13 '11 at 06:24
  • these are the instruction from my home work. i know the double pointer but these instructions are quite confusing. – ibnyadam Apr 13 '11 at 06:28
  • These are declarations. See this SO question for pointers to techniques to decipher them: http://stackoverflow.com/questions/1501292/how-do-you-decipher-complex-declarations-of-pointersarrays – Michael Burr Apr 13 '11 at 06:38
  • possible duplicate of [How do you read C declarations?](http://stackoverflow.com/questions/89056/how-do-you-read-c-declarations) – Mat Apr 13 '11 at 06:39

2 Answers2

4

http://www.cdecl.org/ will explain all these statements. C Right-Left rule explains how to read C declerations pretty well. There are plenty of other resources available, notably in this question.

Community
  • 1
  • 1
Mat
  • 202,337
  • 40
  • 393
  • 406
  • Although the question is kind of lame, how does "declare z as pointer to function (void) returning pointer to array 7 of pointer to function (void) returning pointer to long" help? – slhck Apr 13 '11 at 06:22
  • 2
    Well it explains what `z` is. Since there's no "code", just declarations in the post, I don't see anything else to explain. – Mat Apr 13 '11 at 06:25
2

Since this is your homework you won't learn this by me telling you everything ;) But, one hint. You can create and pass pointers to functions in C, not just variables.

Function arguments of all but the first example are prototypes for function pointers.

Say we have a library for testing colours, we might want to allow the users of our library to provide custom ways of getting the name of the colour. We might define a struct for users to pass in containing callbacks we can call.

struct colour_tester {
  char *(*colour_callback)(void);    
}

// test the user's function if given
void run_test(struct colour_tester *foo ){
  // use the callback function if set
  if ( foo->colour_callback != NULL ){
    char * colour = (*foo->colour_callback)();
    printf( "colour callback returned %s\n", colour );
  }
}

Users of the library would then be free to define implementations of these callback functions and pass them to us as a function pointer.

#include <colour_tester.h>

char * get_shape_colour(){
  return "red";
}

int main ( int argc, char** argv ) {
  // create a colour tester and tell it how to get the colour
  struct colour_tester foo;
  foo.colour_callback = &get_shape_colour;
  run_test( &foo );
}

I've leave you to work out what is going on with the ones with extra numbers of *s.

IanNorton
  • 7,145
  • 2
  • 25
  • 28