Results 1 to 7 of 7
explain me the output please .As far as my knowledge. a is a character type pointer variable and its size should be 4 bytes irrespective of what its pointing to.but ...
- 01-28-2010 #1Just Joined!
- Join Date
- Jan 2010
- Posts
- 4
Explain the output
explain me the output please .As far as my knowledge. a is a character type pointer variable and its size should be 4 bytes irrespective of what its pointing to.but i get a result =50.
*a is the value of a[0] and its size is rightly 1 byte.
but how can the size of that variable change to 4 byte when i just add 7 to it(although i don't store the added result anywhere)
output:Code:#include<stdio.h> int main(){ char a[50]; printf("\n%d ",sizeof(a)); printf("\n%d ",sizeof(*a)); printf("\n%d ",sizeof(*a+7)); return 0; }HTML Code:50 1 4
- 01-28-2010 #2
The first result simply reflects the fact that you have declared a as a character array. Yes, a is also a pointer to that array, but when you ask for the size, the sizeof function assumes that you want the size of the array and not the size of the pointer. It's a more sensible thing to ask for. Try setting another pointer equal to a. I think you will find that when you ask for the size of that pointer, you do get 4 bytes.
I think the last statement is wrong. It should be sizeof(*a)+7. The way you have done it, the contents of a[0] (which are quite random) will be added to 7 and the result passed to sizeof as a purely numeric argument. I have no idea what sizeof does in those circumstances."I'm just a little old lady; don't try to dazzle me with jargon!"
- 01-28-2010 #3
- 01-28-2010 #4Just Joined!
- Join Date
- Jan 2010
- Posts
- 4
- 01-28-2010 #5
- 01-30-2010 #6
gerard4143 is right. a is an array, not a pointer.
If you pass it to a function, the address of the array is passed, so in the function the argument is a pointer.
Also, consider these two declarations:In both cases, 8 bytes are allocated to store the text.Code:char a[8] = "example"; char *b = "example";
In the first line, that's all there is. When you reference a[], the compiler uses the address of a that it knows. You can't change it.
But in the second line, b is a pointer that is probably 4 or 8 bytes containing the address of the text. And you can set that pointer variable equal to some other address.
- 01-30-2010 #7
best way to see this is with an simple example
test.c
test.sCode:#include <stdio.h> char ch[] = "Hello, World1!\n"; char *cptr = "Hello, World2!\n"; int main() { fputs(ch, stdout); fputs(cptr, stdout); return 0; }
We can see that ch address/label has the value .string "Hello, World1!\n"Code:.file "test.c" .globl ch .data .type ch, @object .size ch, 15 ch: .string "Hello, World1!\n" .globl cptr .section .rodata .LC0: .string "Hello, World2!\n" .data .align 8 .type cptr, @object .size cptr, 8 cptr: .quad .LC0
and cptr address/label has the value .quad .LC0
where .LC0 is the address/label of .string "Hello, World2!\n" i.e cptr is a pointerMake mine Arch Linux


Reply With Quote
