434,720 Members | 2,096 Online
Need help? Post your question and get tips & solutions from a community of 434,720 IT Pros & Developers. It's quick & easy.

# some pointer issues....

 P: n/a double * X[5] size of X->?? size of X[0]->?? double (*X)[5] size of X->?? size of X[0]->?? Also if i want to print sizeof(main) it gives me 1 and sizeof(main()) gives me 4 why? Aug 12 '07 #1
34 Replies

 P: n/a On 12 Aug., 17:24, "sumedh....." ?? size of X[0]->?? double (*X)[5] size of X->?? size of X[0]->?? Also if i want to print sizeof(main) it gives me 1 and sizeof(main()) gives me 4 why? Try this test program: /****************************/ #include double *X[5]; /* X is an array of 5 "pointers to double" */ double (*Y)[5]; /* Y is a pointer to an array of 5 doubles */ int main(void) { double array_of_5_double[] = {0.5, 1.5, 2.5, 3.5, 4.5}; X[0] = &array_of_5_double[0]; X[1] = &array_of_5_double[1]; X[2] = &array_of_5_double[2]; X[3] = &array_of_5_double[3]; X[4] = &array_of_5_double[4]; #if 0 // why does this not work? X = {&array_of_5_double[0], /* array_of_5_double */ \ &array_of_5_double[1], \ &array_of_5_double[2], \ &array_of_5_double[3], \ &array_of_5_double[4]}; #endif printf("sizeof(double) = %d // sizeof(double *) = %d // sizeof(double (*)[5]) = %d\n", \ sizeof(double), sizeof(double *), sizeof(double (*)[5])); /* 8 */ printf("sizeof(X) = %d // X = %p\n", sizeof(X), X, &array_of_5_double); printf("sizeof(X[0]) = %d // X[0] = %p // &array_of_5_double[0] = %p\n", sizeof(X[0]), X[0], &array_of_5_double[0]); // X[0] is the first element of our array: here it is a pointer to a double with value 0.5 printf("sizeof(Y) = %d\n", sizeof(Y)); printf("sizeof(Y[0]) = %d\n", sizeof(Y[0])); // 40 printf("sizeof(main) = %d // sizeof(main()) = %d\n", sizeof(main), sizeof(main())); return 0; } /* main is a function pointer! main() is the invocation of the function "int main(void)" which will return an integer of the following size: sizeof(int) == sizeof(main()) */ Explanations: double *X[5]; /* X is an array of 5 "pointers to double" */ Thus sizeof(X) = 5*sizeof(double *) "double *" is a pointer to a double (tip: move from right to left) double (*Y)[5]; /* Y is a pointer to an array of 5 doubles */ Thus sizeof(Y) = sizeof(double (*)[5]) "double (*)[5]" is a pointer to an array of 5 doubles (tip: move from inside to outside) /* /* main is a function pointer! main() is the invocation of the function "int main(void)" which will return an integer of the following size: sizeof(int) This sizeof(main()) is the same size, i.e. the size of the returned integer */ -anon.asdf Aug 12 '07 #2

 P: n/a On Aug 12, 9:21 pm, anon.a...@gmail.com wrote: On 12 Aug., 17:24, "sumedh....." ?? size of X[0]->?? double (*X)[5] size of X->?? size of X[0]->?? Also if i want to print sizeof(main) it gives me 1 and sizeof(main()) gives me 4 why? Try this test program: /****************************/ #include double *X[5]; /* X is an array of 5 "pointers to double" */ double (*Y)[5]; /* Y is a pointer to an array of 5 doubles */ int main(void) { double array_of_5_double[] = {0.5, 1.5, 2.5, 3.5, 4.5}; X[0] = &array_of_5_double[0]; X[1] = &array_of_5_double[1]; X[2] = &array_of_5_double[2]; X[3] = &array_of_5_double[3]; X[4] = &array_of_5_double[4]; #if 0 // why does this not work? X = {&array_of_5_double[0], /* array_of_5_double */ \ &array_of_5_double[1], \ &array_of_5_double[2], \ &array_of_5_double[3], \ &array_of_5_double[4]}; #endif printf("sizeof(double) = %d // sizeof(double *) = %d // sizeof(double (*)[5]) = %d\n", \ sizeof(double), sizeof(double *), sizeof(double (*)[5])); /* 8 */ printf("sizeof(X) = %d // X = %p\n", sizeof(X), X, &array_of_5_double); printf("sizeof(X[0]) = %d // X[0] = %p // &array_of_5_double[0] = %p\n", sizeof(X[0]), X[0], &array_of_5_double[0]); // X[0] is the first element of our array: here it is a pointer to a double with value 0.5 printf("sizeof(Y) = %d\n", sizeof(Y)); printf("sizeof(Y[0]) = %d\n", sizeof(Y[0])); // 40 printf("sizeof(main) = %d // sizeof(main()) = %d\n", sizeof(main), sizeof(main())); return 0; } /* main is a function pointer! main() is the invocation of the function "int main(void)" which will return an integer of the following size: sizeof(int) == sizeof(main()) */ Explanations: double *X[5]; /* X is an array of 5 "pointers to double" */ Thus sizeof(X) = 5*sizeof(double *) "double *" is a pointer to a double (tip: move from right to left) double (*Y)[5]; /* Y is a pointer to an array of 5 doubles */ Thus sizeof(Y) = sizeof(double (*)[5]) "double (*)[5]" is a pointer to an array of 5 doubles (tip: move from inside to outside) /* /* main is a function pointer! main() is the invocation of the function "int main(void)" which will return an integer of the following size: sizeof(int) This sizeof(main()) is the same size, i.e. the size of the returned integer */ -anon.asdf gr8 explanation: just wanted to know why does sizeof(main) -1 sizeof(main()) -sizeof(int)????? Aug 12 '07 #3

 P: n/a "sumedh....." size of X->?? size of X is reported by sizeof X or sizeof(double *[5]) I recommend the first method. size of X[0]->?? size of X[0] is reported by sizeof X[0] or sizeof(double) Again, I recommend the first method. > double (*X)[5] Pointer to an array of five doubles size of X->?? size of X is reported by sizeof X or sizeof(double(*)[5] Again, I recommend the first method. size of X[0]->?? size of X[0] is reported by sizeof X[0] or sizeof(double[5]) Again, I recommend the first method. > Also if i want to print sizeof(main) it gives me 1 and sizeof(main()) gives me 4 why? 'main' (without parentheses) yields the address of the function 'main()'. So sizeof(main) will give the size of a pointer to a function. 'main()' invokes the function main(), and returns its return value. This type is 'int'. So 'sizeof(main())' gives the size of type 'int'. Note that these type sizes will vary among platforms (and could conceivably vary among implementations for the same platform). -Mike Aug 12 '07 #4

 P: n/a "sumedh....."

 P: n/a On Aug 12, 10:42 pm, "Mike Wahler"

 P: n/a Mike Wahler wrote, On 12/08/07 18:42: "sumedh....." On Aug 12, 9:21 pm, anon.a...@gmail.com wrote:sizeof(main) -1sizeof(main()) -sizeof(int)????? The type of 'main' is a pointer type. Wrong. The time of main is a function, not a pointer. sizeof(main) is actually a constraint violation and a diagnostic is required. Of course, with some C compilers (e.g. gcc) you have to poke the compiler hard enough to get all the required diagnostics. Personally I think this gcc extension is pointless. The type of 'main()' is int. Obviously, the sizes of these two different types are not the same on your platform. Indeed. -- Flash Gordon Aug 12 '07 #7

 P: n/a sumedh..... wrote, On 12/08/07 19:17: On Aug 12, 10:42 pm, "Mike Wahler" "sumedh....." >On Aug 12, 9:21 pm, anon.a...@gmail.com wrote:sizeof(main) -1sizeof(main()) -sizeof(int)????? The type of 'main' is a pointer type.The type of 'main()' is int.Obviously, the sizes of these two different typesare not the same on your platform.-Mike i have read that size of pointer is 2bytes, Whatever you read is implementation specific, not part of C. Also irrelevant. its 4bytes on my compiler... not an issue... As you see. I've used implementations where the sizeof a pointer is 1. now... If its a pointer type? why an integer/float/void/double pointer declared inside main yields there size as 4 and main as 1? so is this pointer diff than others? Because main is not a pointer and Mike was wrong. -- Flash Gordon Aug 12 '07 #8

 P: n/a "sumedh....." "sumedh....."

 P: n/a "Flash Gordon" On Aug 12, 10:42 pm, "Mike Wahler" >"sumedh....." its 4bytes on my compiler... not an issue... As you see. I've used implementations where the sizeof a pointer is 1. >now...If its a pointer type? why an integer/float/void/double pointerdeclared inside main yields there size as 4 and main as 1?so is this pointer diff than others? Because main is not a pointer and Mike was wrong. Huh? Are you saying that the name of a function (without parentheses) does not yeild a pointer value? -Mike Aug 12 '07 #10

 P: n/a "Flash Gordon" "sumedh....." >On Aug 12, 9:21 pm, anon.a...@gmail.com wrote:sizeof(main) -1sizeof(main()) -sizeof(int)????? The type of 'main' is a pointer type. Wrong. The time of main is a function, Yes, its type is 'function'. But in a program, the name (without parens) will yield an address. -Mike Aug 12 '07 #11

 P: n/a Mike Wahler wrote, On 12/08/07 20:34: "Flash Gordon" Mike Wahler wrote, On 12/08/07 18:42: >>"sumedh....."

 P: n/a Mike Wahler wrote: > "Flash Gordon"

 P: n/a "Mike Wahler" gcc's "-pedantic" option causes it to emit the required diagnostic. -- Keith Thompson (The_Other_Keith) ks***@mib.org San Diego Supercomputer Center <* "We must do something. This is something. Therefore, we must do this." -- Antony Jay and Jonathan Lynn, "Yes Minister" Aug 12 '07 #14

 P: n/a On Aug 13, 2:35 am, pete

 P: n/a sumedh..... wrote: > On Aug 13, 2:35 am, pete

 P: n/a pete wrote: > sumedh..... wrote: Since sizeof is not defined for expressions of function type, the only thing that you can do with an expression of function type in a correct C program, is to derive a pointer from it. > -- pete pete would you like to explain by giving an example? if i derive a pointer from it what would be the type of that pointer? The type of (&main) is a pointer to a function returning type int and taking no arguments. Well actually, how many arguments depends on how main is defined. -- pete Aug 13 '07 #17

 P: n/a sumedh..... wrote: On Aug 13, 2:35 am, pete Mike Wahler wrote: >>"Flash Gordon"

 P: n/a Does this make it clearer? typedef int (*Fn)(void); int main( void ) { Fn fn = main; } -- Ian Collins. typedef int (*Fn)(void); int main( void ) { Fn fn = main; printf("%d\n",sizeof(fn)); // o/ps 4 (that is size of pointer to function returning int.) printf("%d\n",sizeof(&fn)); // o/ps 4 (that is size of pointer to function returning int.) printf("%d\n",sizeof(&main)); // o/ps 4 (that is size of pointer to function returning int.) printf("%d\n",sizeof(main)); //o/ps 1 inspite of main & (&main) are both the same thing.. pointer //to function returning int printf("%d %d \n",fn,&fn); //o/ps addresses A & B printf("%d %d \n",main,&main); //o/ps addresses A & A system("PAUSE"); return 0; } Aug 13 '07 #19

 P: n/a sumedh..... wrote: >Does this make it clearer?typedef int (*Fn)(void);int main( void ) { Fn fn = main;} *Please don't quote signatures.* > typedef int (*Fn)(void); int main( void ) { Fn fn = main; printf("%d\n",sizeof(main)); //o/ps 1 inspite of main & (&main) are both the same thing.. pointer //to function returning int Constraint violation, see 6.5.3.4.1 "The sizeof operator shall not be applied to an expression that has function type..." Did this even compile? -- Ian Collins. Aug 13 '07 #20

 P: n/a On Aug 13, 1:19 pm, Ian Collins

 P: n/a sumedh..... wrote: On Aug 13, 1:19 pm, Ian Collins sumedh..... wrote: >>>Does this make it clearer?typedef int (*Fn)(void);int main( void ) { Fn fn = main;}typedef int (*Fn)(void);int main( void ) { Fn fn = main; printf("%d\n",sizeof(main)); //o/ps 1 inspite of main & (&main) areboth the same thing.. pointer //to function returning int Constraint violation, see 6.5.3.4.1"The sizeof operator shall not be applied to an expression that hasfunction type..."Did this even compile? *Please don't quote signatures.* > it compiles and runs properly. Then either get a better compiler, or turn up the warnings. -- Ian Collins. Aug 13 '07 #22

 P: n/a Ian Collins >Does this make it clearer?typedef int (*Fn)(void);int main( void ) { Fn fn = main;} *Please don't quote signatures.* >>typedef int (*Fn)(void);int main( void ) { Fn fn = main; > printf("%d\n",sizeof(main)); //o/ps 1 inspite of main & (&main) areboth the same thing.. pointer //to function returning int Constraint violation, see 6.5.3.4.1 "The sizeof operator shall not be applied to an expression that has function type..." Did this even compile? Quite possibly it does; gcc accepts 'sizeof(main)' (as an extension) by default. To the original poster: if you invoke gc with "-std=c99 -pedantic", it will complain about several problems in your program. If you replace "-std=99" with "-ansi", it will also complain about the "//" comments (actually it will completely fail to recognize them and complain about syntax errors). Using "//" comments in Usenet postings is usually a bad idea; wrapping of long lines can easily introduce syntax errors. And you've neglected the #include directives for #include typedef int (*Fn)(void); int main( void ) { Fn fn = main; printf("%d\n", (int)sizeof(fn)); printf("%d\n", (int)sizeof(&fn)); printf("%d\n", (int)sizeof(&main)); /* printf("%d\n", (int)sizeof(main)); */ printf("%p %p\n", (void*)fn, (void*)&fn); printf("%p %p\n", (void*)main, (void*)&main); return 0; } I wrote "nearly" because the casts on the last two printf statements are actually illegal. There is no portable way to print the value of a pointer-to-function (other than by decomposing it into a sequence of bytes). Allowing conversion to void* is a common extension -- more common, I think, than allowing sizeof to be applied to functions. -- Keith Thompson (The_Other_Keith) ks***@mib.org San Diego Supercomputer Center <* "We must do something. This is something. Therefore, we must do this." -- Antony Jay and Jonathan Lynn, "Yes Minister" Aug 13 '07 #23

 P: n/a Ian Collins wrote: > sumedh..... wrote: On Aug 13, 1:19 pm, Ian Collins printf("%d\n",sizeof(main)); Constraint violation, see 6.5.3.4.1 "The sizeof operator shall not be applied to an expression that has function type..." Did this even compile? *Please don't quote signatures.* it compiles and runs properly. Then either get a better compiler, or turn up the warnings. .... and or make sure that the compiler is configured as an ANSI C compiler. -- pete Aug 13 '07 #24

 P: n/a Keith Thompson wrote: > Quite possibly it does; gcc accepts 'sizeof(main)' (as an extension) by default. That's one thing that has puzzled me for a long time, why would anyone want to apply sizeof to a function? Even more puzzling considering gcc evaluates it as 1 when they do. -- Ian Collins. Aug 13 '07 #25

 P: n/a Ian Collins said: Keith Thompson wrote: >>Quite possibly it does; gcc accepts 'sizeof(main)' (as an extension)by default. That's one thing that has puzzled me for a long time, why would anyone want to apply sizeof to a function? Well, if we stop thinking C and start thinking raw machine for a minute, it's not difficult to imagine why it might be useful to know how big a function is - given that information, we could replace some or all of the function with inline machine code at runtime, presumably only under certain runtime conditions (because otherwise, why bother?). -- Richard Heathfield Email: -www. +rjh@ Google users: "Usenet is a strange place" - dmr 29 July 1999 Aug 13 '07 #26

 P: n/a Richard Heathfield wrote: Ian Collins said: >Keith Thompson wrote: >>Quite possibly it does; gcc accepts 'sizeof(main)' (as an extension)by default. That's one thing that has puzzled me for a long time, why would anyonewant to apply sizeof to a function? Well, if we stop thinking C and start thinking raw machine for a minute, it's not difficult to imagine why it might be useful to know how big a function is - given that information, we could replace some or all of the function with inline machine code at runtime, presumably only under certain runtime conditions (because otherwise, why bother?). Fair enough, but what's the point if the result of the expression is always 1? -- Ian Collins. Aug 13 '07 #27

 P: n/a Ian Collins wrote: Richard Heathfield wrote: >Ian Collins said: >>Keith Thompson wrote:Quite possibly it does; gcc accepts 'sizeof(main)' (as an extension)by default.That's one thing that has puzzled me for a long time, why would anyonewant to apply sizeof to a function? Well, if we stop thinking C and start thinking raw machine for a minute,it's not difficult to imagine why it might be useful to know how big afunction is - given that information, we could replace some or all ofthe function with inline machine code at runtime, presumably only undercertain runtime conditions (because otherwise, why bother?). Fair enough, but what's the point if the result of the expression is always 1? Even for an extern function! -- Ian Collins. Aug 13 '07 #28

 P: n/a Ian Collins wrote: Keith Thompson wrote: >>Quite possibly it does; gcc accepts 'sizeof(main)' (as an extension)by default. That's one thing that has puzzled me for a long time, why would anyone want to apply sizeof to a function? Even more puzzling considering gcc evaluates it as 1 when they do. gcc makes sizeof(void) and sizeof(func) 1 so that you can do pointer arithmetic on void and function pointers as if they were char pointers. In standard C, a conversion to char * is required for this: /* non-standard */ void *p = malloc(12); /* NULL check omitted */ int *i = p + 4; long *l = p + 8; versus /* standard */ void *p = malloc(12); /* NULL check omitted */ int *i = (int *) ((char *) p + 4); long *l = (long *) ((char *) p + 8); I don't particularly see the need for this, especially for function pointers, but apparently others did. Aug 13 '07 #29

 P: n/a Ian Collins said: Richard Heathfield wrote: >Ian Collins said: >>Keith Thompson wrote:Quite possibly it does; gcc accepts 'sizeof(main)' (as anextension) by default.That's one thing that has puzzled me for a long time, why wouldanyone want to apply sizeof to a function? >> Fair enough, but what's the point if the result of the expression is always 1? Perhaps gcc is just very, very, very good at optimisation? :-) -- Richard Heathfield Email: -www. +rjh@ Google users: "Usenet is a strange place" - dmr 29 July 1999 Aug 13 '07 #30

 P: n/a On Mon, 13 Aug 2007 13:31:10 +0000, Richard Heathfield wrote: Ian Collins said: >Richard Heathfield wrote: >>Ian Collins said:Keith Thompson wrote:Quite possibly it does; gcc accepts 'sizeof(main)' (as anextension) by default.>That's one thing that has puzzled me for a long time, why wouldanyone want to apply sizeof to a function? >>> Fair enough, but what's the point if the result of the expression isalways 1? Perhaps gcc is just very, very, very good at optimisation? :-) The problem is when it has to optimize more than 256 functions that well. :-) -- Army1987 (Replace "NOSPAM" with "email") No-one ever won a game by resigning. -- S. Tartakower Aug 13 '07 #31

 P: n/a Richard Heathfield wrote: Well, if we stop thinking C and start thinking raw machine for a minute, it's not difficult to imagine why it might be useful to know how big a function is - given that information, we could replace some or all of the function with inline machine code at runtime, presumably only under certain runtime conditions (because otherwise, why bother?). I don't think it is useful. The function with the size in question, might work by calling other functions. If the sizeof the function doesn't include those helper functions then it doesn't have the information that you need. If the sizeof the function does include those helper functions, you don't know how many of those helper functions are being used by other functions, which means that if you delete one function that calls them, there may still be other functions that call some of the same helper functions and your calculations of how much space can be saved by deleting the function in question, will be wrong. -- pete Aug 13 '07 #32

 P: n/a pete said: Richard Heathfield wrote: >Well, if we stop thinking C and start thinking raw machinefor a minute,it's not difficult to imagine why it might be useful to know how biga function is - given that information, we could replace some or allof the function with inline machine code at runtime,presumably only undercertain runtime conditions (because otherwise, why bother?). I don't think it is useful. Well, it isn't all /that/ useful. The function with the size in question, might work by calling other functions. Wouldn't matter, if you were replacing it at runtime with inline assembly language. If the sizeof the function doesn't include those helper functions then it doesn't have the information that you need. You lost me. Why would you need that info? All you're doing is an overlay, not a complete runtime code optimisation. -- Richard Heathfield Email: -www. +rjh@ Google users: "Usenet is a strange place" - dmr 29 July 1999 Aug 14 '07 #33

 P: n/a Richard Heathfield wrote: Ian Collins said: >Richard Heathfield wrote: >>Ian Collins said:Keith Thompson wrote:Quite possibly it does; gcc accepts 'sizeof(main)' (as anextension) by default.>That's one thing that has puzzled me for a long time, why wouldanyone want to apply sizeof to a function? >Fair enough, but what's the point if the result of the expression isalways 1? Perhaps gcc is just very, very, very good at optimisation? :-) And psychic, considering the sizeof an extern function is also 1! -- Ian Collins. Aug 14 '07 #34

 P: n/a Richard Heathfield wrote: > pete said: Richard Heathfield wrote: Well, if we stop thinking C and start thinking raw machine for a minute, it's not difficult to imagine why it might be useful to know how big a function is - given that information, we could replace some or all of the function with inline machine code at runtime, presumably only under certain runtime conditions (because otherwise, why bother?). If the sizeof the function doesn't include those helper functions then it doesn't have the information that you need. You lost me. Why would you need that info? All you're doing is an overlay, not a complete runtime code optimisation. I guess I don't understand the usage. I look at compiler listing files when I want to see the memory usage for the program instructions. -- pete Aug 15 '07 #35

### This discussion thread is closed

Replies have been disabled for this discussion.