# some pointer issues....

Discussion in 'C Programming' started by sumedh....., Aug 12, 2007.

1. ### sumedh.....Guest

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?

sumedh....., Aug 12, 2007

2. ### Guest

On 12 Aug., 17:24, "sumedh....." <> wrote:
> 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?

Try this test program:

/****************************/
#include <stdio.h>

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, 2007

3. ### sumedh.....Guest

On Aug 12, 9:21 pm, wrote:
> On 12 Aug., 17:24, "sumedh....." <> wrote:
>
> > 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?

>
> Try this test program:
>
> /****************************/
> #include <stdio.h>
>
> 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)?????

sumedh....., Aug 12, 2007
4. ### Mike WahlerGuest

"sumedh....." <> wrote in message
news:...
> double * X[5]

Array of five pointers to type double

>
> 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

Mike Wahler, Aug 12, 2007
5. ### Mike WahlerGuest

"sumedh....." <> wrote in message
news:...
> On Aug 12, 9:21 pm, wrote:
> sizeof(main) -> 1
> sizeof(main()) -> sizeof(int)?????

The type of 'main' is a pointer type.
The type of 'main()' is int.

Obviously, the sizes of these two different types
are not the same on your platform.

-Mike

Mike Wahler, Aug 12, 2007
6. ### sumedh.....Guest

On Aug 12, 10:42 pm, "Mike Wahler" <> wrote:
> "sumedh....." <> wrote in message
>
> news:...
>
> > On Aug 12, 9:21 pm, wrote:
> > sizeof(main) -> 1
> > sizeof(main()) -> sizeof(int)?????

>
> The type of 'main' is a pointer type.
> The type of 'main()' is int.
>
> Obviously, the sizes of these two different types
> are not the same on your platform.
>
> -Mike

i have read that size of pointer is 2bytes,
its 4bytes on my compiler... not an issue...
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?

sumedh....., Aug 12, 2007
7. ### Flash GordonGuest

Mike Wahler wrote, On 12/08/07 18:42:
> "sumedh....." <> wrote in message
> news:...
>> On Aug 12, 9:21 pm, wrote:
>> sizeof(main) -> 1
>> sizeof(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

Flash Gordon, Aug 12, 2007
8. ### Flash GordonGuest

sumedh..... wrote, On 12/08/07 19:17:
> On Aug 12, 10:42 pm, "Mike Wahler" <> wrote:
>> "sumedh....." <> wrote in message
>>
>> news:...
>>
>>> On Aug 12, 9:21 pm, wrote:
>>> sizeof(main) -> 1
>>> sizeof(main()) -> sizeof(int)?????

>> The type of 'main' is a pointer type.
>> The type of 'main()' is int.
>>
>> Obviously, the sizes of these two different types
>> are 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

Flash Gordon, Aug 12, 2007
9. ### Mike WahlerGuest

"sumedh....." <> wrote in message
news:...
> On Aug 12, 10:42 pm, "Mike Wahler" <> wrote:
>> "sumedh....." <> wrote in message
>>
>> news:...
>>
>> > On Aug 12, 9:21 pm, wrote:
>> > sizeof(main) -> 1
>> > sizeof(main()) -> sizeof(int)?????

>>
>> The type of 'main' is a pointer type.
>> The type of 'main()' is int.
>>
>> Obviously, the sizes of these two different types
>> are not the same on your platform.
>>
>> -Mike

>
> i have read that size of pointer is 2bytes,
> its 4bytes on my compiler... not an issue...

The C language does not specify particular
sizes for pointer types. This depends upon your
implementation.

> 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?

There are several different pointer types in C. E.g.
'pointer to int', 'pointer to double', 'pointer to function',
etc. There's no requirement that the sizes of these pointers
are the same?

BTW why are you so concerned about these sizes? If some
part(s) of your program needs to know, just use the sizeof
operator. The specific sizes should not concern you.

-Mike

Mike Wahler, Aug 12, 2007
10. ### Mike WahlerGuest

"Flash Gordon" <> wrote in message
news:-gordon.me.uk...
> sumedh..... wrote, On 12/08/07 19:17:
>> On Aug 12, 10:42 pm, "Mike Wahler" <> wrote:
>>> "sumedh....." <> wrote in message
>>>
>>> news:...
>>>
>>>> On Aug 12, 9:21 pm, wrote:
>>>> sizeof(main) -> 1
>>>> sizeof(main()) -> sizeof(int)?????
>>> The type of 'main' is a pointer type.
>>> The type of 'main()' is int.
>>>
>>> Obviously, the sizes of these two different types
>>> are 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.

Huh? Are you saying that the name of a function (without parentheses)
does not yeild a pointer value?

-Mike

Mike Wahler, Aug 12, 2007
11. ### Mike WahlerGuest

"Flash Gordon" <> wrote in message
news:-gordon.me.uk...
> Mike Wahler wrote, On 12/08/07 18:42:
>> "sumedh....." <> wrote in message
>> news:...
>>> On Aug 12, 9:21 pm, wrote:
>>> sizeof(main) -> 1
>>> sizeof(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

Mike Wahler, Aug 12, 2007
12. ### Flash GordonGuest

Mike Wahler wrote, On 12/08/07 20:34:
> "Flash Gordon" <> wrote in message
> news:-gordon.me.uk...
>> Mike Wahler wrote, On 12/08/07 18:42:
>>> "sumedh....." <> wrote in message
>>> news:...
>>>> On Aug 12, 9:21 pm, wrote:
>>>> sizeof(main) -> 1
>>>> sizeof(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.

N1124 section 6.3.2.1 para 4 it says
| A function designator is an expression that has function type.
| Except when it is the operand of the sizeof operator54) or the
| unary & operator, a function designator with type ‘‘function
| returning type’’ is converted to an expression that has type
| ‘‘pointer to function returning type’’.

Further, the constraints section of the definition of the sizeof
operator says:
| The sizeof operator shall not be applied to an expression that
| has function type or ...

So as I said the type is function, not pointer to funtion, and a
diagnostic (warning or error) is *required*.
--
Flash Gordon

Flash Gordon, Aug 12, 2007
13. ### peteGuest

Mike Wahler wrote:
>
> "Flash Gordon" <> wrote in message

> > 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?

The only two times when an expression of function type is not
automatically converted to a pointer, are:
1 as an operand of &
2 as an operand of sizeof

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, Aug 12, 2007
14. ### Keith ThompsonGuest

"Mike Wahler" <> writes:
[...]
> 'main' (without parentheses) yields the address of
> the function 'main()'.

In most contexts, yes.

> So sizeof(main) will give
> the size of a pointer to a function.

No, the operand of a sizeof operator is one of the contexts in which a
function name (more generally an expression of function type) is not
converted to a pointer. 'sizeof(main)' (or 'sizeof main'; the
parentheses are not necessary) is a constraint violation. <OT>gcc's
"-pedantic" option causes it to emit the required diagnostic.</OT>

--
Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

Keith Thompson, Aug 12, 2007
15. ### sumedh.....Guest

On Aug 13, 2:35 am, pete <> wrote:
> Mike Wahler wrote:
>
> > "Flash Gordon" <> wrote in message
> > > 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?

>
> The only two times when an expression of function type is not
> automatically converted to a pointer, are:
> 1 as an operand of &
> 2 as an operand of sizeof
>
> 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?

sumedh....., Aug 13, 2007
16. ### peteGuest

sumedh..... wrote:
>
> On Aug 13, 2:35 am, pete <> wrote:
> > Mike Wahler wrote:
> >
> > > "Flash Gordon" <> wrote in message
> > > > 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?

> >
> > The only two times when an expression of function type is not
> > automatically converted to a pointer, are:
> > 1 as an operand of &
> > 2 as an operand of sizeof
> >
> > 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.

--
pete

pete, Aug 13, 2007
17. ### peteGuest

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

pete, Aug 13, 2007
18. ### Ian CollinsGuest

sumedh..... wrote:
> On Aug 13, 2:35 am, pete <> wrote:
>> Mike Wahler wrote:
>>
>>> "Flash Gordon" <> wrote in message
>>>> 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?

>> The only two times when an expression of function type is not
>> automatically converted to a pointer, are:
>> 1 as an operand of &
>> 2 as an operand of sizeof
>>
>> 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 would you like to explain by giving an example?
> if i derive a pointer from it what would be the type of that pointer?
>

Does this make it clearer?

typedef int (*Fn)(void);

int main( void ) {
Fn fn = main;
}

--
Ian Collins.

Ian Collins, Aug 13, 2007
19. ### sumedh.....Guest

> 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;
}

sumedh....., Aug 13, 2007
20. ### Ian CollinsGuest

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) 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.

Ian Collins, Aug 13, 2007