LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Explain the output(pointers) (https://www.linuxquestions.org/questions/programming-9/explain-the-output-pointers-785311/)

ishandutta2007 01-28-2010 05:38 AM

Explain the output(pointers)
 
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)



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

output:
Code:

50
1
4


SigTerm 01-28-2010 06:19 AM

Quote:

Originally Posted by ishandutta2007 (Post 3843462)
but i get a result =50.

"a" is an array of 50 chars, so size is correct.
Try this:
Code:

#include<stdio.h>
int main(){
        char a[50];
        char* b = a;
        printf("\n%d  ",sizeof(a));
        printf("\n%d  ",sizeof(b));
        return 0;
}


Dan04 01-28-2010 08:25 AM

*a+7 means (*a)+7. And char+int=int.


All times are GMT -5. The time now is 10:36 PM.