Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > some pointer issues....

Reply
Thread Tools

some pointer issues....

 
 
sumedh.....
Guest
Posts: n/a
 
      08-12-2007
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?

 
Reply With Quote
 
 
 
 
anon.asdf@gmail.com
Guest
Posts: n/a
 
      08-12-2007
On 12 Aug., 17:24, "sumedh....." <sumedhsak...@gmail.com> 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

 
Reply With Quote
 
 
 
 
sumedh.....
Guest
Posts: n/a
 
      08-12-2007
On Aug 12, 9:21 pm, anon.a...@gmail.com wrote:
> On 12 Aug., 17:24, "sumedh....." <sumedhsak...@gmail.com> 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)?????

 
Reply With Quote
 
Mike Wahler
Guest
Posts: n/a
 
      08-12-2007

"sumedh....." <> wrote in message
news: ups.com...
> 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


 
Reply With Quote
 
Mike Wahler
Guest
Posts: n/a
 
      08-12-2007

"sumedh....." <> wrote in message
news: oups.com...
> On Aug 12, 9:21 pm, anon.a...@gmail.com 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



 
Reply With Quote
 
sumedh.....
Guest
Posts: n/a
 
      08-12-2007
On Aug 12, 10:42 pm, "Mike Wahler" <mkwah...@mkwahler.net> wrote:
> "sumedh....." <sumedhsak...@gmail.com> wrote in message
>
> news: oups.com...
>
> > On Aug 12, 9:21 pm, anon.a...@gmail.com 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?

 
Reply With Quote
 
Flash Gordon
Guest
Posts: n/a
 
      08-12-2007
Mike Wahler wrote, On 12/08/07 18:42:
> "sumedh....." <> wrote in message
> news: oups.com...
>> On Aug 12, 9:21 pm, anon.a...@gmail.com 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
 
Reply With Quote
 
Flash Gordon
Guest
Posts: n/a
 
      08-12-2007
sumedh..... wrote, On 12/08/07 19:17:
> On Aug 12, 10:42 pm, "Mike Wahler" <mkwah...@mkwahler.net> wrote:
>> "sumedh....." <sumedhsak...@gmail.com> wrote in message
>>
>> news: oups.com...
>>
>>> On Aug 12, 9:21 pm, anon.a...@gmail.com 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
 
Reply With Quote
 
Mike Wahler
Guest
Posts: n/a
 
      08-12-2007

"sumedh....." <> wrote in message
news: ups.com...
> On Aug 12, 10:42 pm, "Mike Wahler" <mkwah...@mkwahler.net> wrote:
>> "sumedh....." <sumedhsak...@gmail.com> wrote in message
>>
>> news: oups.com...
>>
>> > On Aug 12, 9:21 pm, anon.a...@gmail.com 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


 
Reply With Quote
 
Mike Wahler
Guest
Posts: n/a
 
      08-12-2007

"Flash Gordon" <> wrote in message
news:...
> sumedh..... wrote, On 12/08/07 19:17:
>> On Aug 12, 10:42 pm, "Mike Wahler" <mkwah...@mkwahler.net> wrote:
>>> "sumedh....." <sumedhsak...@gmail.com> wrote in message
>>>
>>> news: oups.com...
>>>
>>>> On Aug 12, 9:21 pm, anon.a...@gmail.com 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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
pointer to an array vs pointer to pointer subramanian100in@yahoo.com, India C Programming 5 09-23-2011 10:28 AM
Pointer to pointer or reference to pointer A C++ 7 07-05-2011 07:49 PM
Pointer to pointer Vs References to Pointer bansalvikrant@gmail.com C++ 4 07-02-2009 10:20 AM
passing the address of a pointer to a func that doesnt recieve a pointer-to-a-pointer jimjim C Programming 16 03-27-2006 11:03 PM
Pointer-to-pointer-to-pointer question masood.iqbal@lycos.com C Programming 10 02-04-2005 02:57 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57