Small C question!

Started by
7 comments, last by bodhisatva_b 18 years ago
#include <stdio.h>

int main()
{
     char *a[4]={ "oracle", "java"};
     char *p=a;
     printf("%d, %d",sizeof(a),sizeof(char *));
     printf("\n The size of p=%d",sizeof(p));
     getch();
     return 0;
} 
wats the output?

16,4
 The size of p=4
Why?? Coz we all know tht "a" is a constant point which is pointing to the first element of the array. Alright? so sizeof(a) shud be 4 (the size of a memory element on 32 bit OS) ... instead the sizeof(a) takes in the entire array into account. However, as u can see p is also a pointer which is pointing to the same address as "a". And sizeof(p) is 4. :-S PS: Am using devc++
Any ship can be a minesweeper... once.
Advertisement
sizeof(a) is the size of the variable a in memory (which is 4*sizeof(char*) == 16, not 4).

Quote:
Coz we all know tht "a" is a constant point which is pointing to the first element of the array.


No. a is an array of char*. It is not a pointer that points to the first element of the array, it is the array itself, hence its size.

Quote:
However, as you can see p is also a pointer which is pointing to the same address as "a". And sizeof(p) is 4. :-S


p is a char*. p is really a pointer - thus, it's size is 4.

Regards,
a is a static array of 4 character pointers, thus the size of a is 4 * the size of a character pointer, since sizeof will resolve to the size of a whole array if the object it is being used on is a static array [one that is not assigned by 'new' or malloc]. p is a character pointer that you've assigned to point to the first element of the character pointer array, so for that reason, p and a are not even of the same type [but are both pointers, and thus have a size of 4 per element]. Other compilers will actually flag this as an error, demanding explicit casting from what is effectively a char** to a char* [though some compilers will call it a cast from char*[4] to char*, but they are nearly equivilant].

a[0] and p are of the same type. The type difference has nothing to do with the size difference in the case, the size difference is caused by a being a static array of elements each of size 4 [and 4 of them, thus 16]

edit:: darn, beat to the punch... again :P
"a" is an array of pointer-to-pointer, char* p = a makes "p" a pointer-to-a-pointer-to-a-pointer, I believe.

The reason sizeof(a) returns 16 is because "a" is a statically allocated array of 4 pointers; 4 * 4 = 16.
I always confuse when I deal with pointer, So if I'm wrong please correct me. In this case I think char* a[4] have 4 element of pointer (char* a[0] to char* a[3]) so sizeof(a) must be equal to sizeof(char*) * 4. but char* p is the pointer-to-pointer it only point to the first pointer element which mean p point to the a[0] which make sizeof(p) = sizeof(char*).
DinGY
Yesterday is history.Tomorrow is a mystery. Today is a gift"
Wow... Thanks a lot guys!
Really cleared tht up! :D
Quick question, doesnt the array name store the address of the 1st element?
Also, acc to what you all have said, "a" basically refers to the entire array implicitly?

char *a[4]={ "oracle", "java"};
const char *p=a;
printf("%d, %d",sizeof(a),sizeof(char *));
printf("\n The size of p=%d",sizeof(p));
printf("\n The address of a=%d, p=%d",a,p);
printf("\n the address of 1st element=%d",&a[0]);
getch();
return 0;

The output statements prints memory addresses which are the same.
Any ship can be a minesweeper... once.
a and p have same address because p pointer point to a adress.
and adress of a also same as &a[0].
but adress of a not same as &a[1]

Hope this is helpful
DinGY
Yesterday is history.Tomorrow is a mystery. Today is a gift"
Quote:Original post by bodhisatva_b
Quick question, doesnt the array name store the address of the 1st element?

The array name stores the elements in the array. When used in a pointer context, the name of the array is implicitely converted to a pointer to the first element. That doesn't mean it IS a pointer though, only that it can (with some restrictions) be used as one.
Exactly wat i wanted to know. Thanks a lot Bob.. (and all the others who took time to reply as i learnt something from each one of em)
Thanks again!
Any ship can be a minesweeper... once.

This topic is closed to new replies.

Advertisement