Find the answer to your Linux question:
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 ...
  1. #1
    Just 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)
    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:
    HTML Code:
    50
    1
    4

  2. #2
    Linux Engineer hazel's Avatar
    Join Date
    May 2004
    Location
    Harrow, UK
    Posts
    955
    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!"

  3. #3
    Linux Enthusiast gerard4143's Avatar
    Join Date
    Dec 2007
    Location
    Canada, Prince Edward Island
    Posts
    714
    Quote Originally Posted by ishandutta2007 View Post
    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.
    If a is a pointer to char then try reassigning it a new pointer value...You'll find you can't assign a a new pointer value...
    Last edited by gerard4143; 01-28-2010 at 07:09 PM.
    Make mine Arch Linux

  4. #4
    Just Joined!
    Join Date
    Jan 2010
    Posts
    4
    Quote Originally Posted by gerard4143 View Post
    If a is a pointer to char then try reassigning it to a new pointer value...You'll find you can't assign a a new pointer value...
    i have assigned and the following code works as under:
    Code:
    #include<stdio.h>
    int main(){
    	char a[50];
    	char* b = a;
    	printf("\n%d  ",sizeof(a));
    	printf("\n%d  ",sizeof(b));
    	return 0;
    }
    output:
    Code:
    50
    4

  5. #5
    Linux Enthusiast gerard4143's Avatar
    Join Date
    Dec 2007
    Location
    Canada, Prince Edward Island
    Posts
    714
    Quote Originally Posted by ishandutta2007 View Post
    i have assigned and the following code works as under:
    Code:
    #include<stdio.h>
    int main(){
    	char a[50];
    	char* b = a;
    	printf("\n%d  ",sizeof(a));
    	printf("\n%d  ",sizeof(b));
    	return 0;
    }
    output:
    Code:
    50
    4
    I meant you can't assign char a[50] a new pointer value.
    Make mine Arch Linux

  6. #6
    Linux Enthusiast KenJackson's Avatar
    Join Date
    Jun 2006
    Location
    Maryland, USA
    Posts
    506
    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:
    Code:
    char a[8] = "example";
    char *b = "example";
    In both cases, 8 bytes are allocated to store the text.

    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.

  7. #7
    Linux Enthusiast gerard4143's Avatar
    Join Date
    Dec 2007
    Location
    Canada, Prince Edward Island
    Posts
    714
    best way to see this is with an simple example

    test.c
    Code:
    #include <stdio.h>
    
    char ch[] = "Hello, World1!\n";
    char *cptr = "Hello, World2!\n";
    
    int main()
    {
    	fputs(ch, stdout);
    	fputs(cptr, stdout);
    	return 0;
    }
    test.s
    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
    We can see that ch address/label has the value .string "Hello, World1!\n"
    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 pointer
    Make mine Arch Linux

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...