sizeof: determining the size of char array problems...

This is a discussion on sizeof: determining the size of char array problems... within the C++ Programming forums, part of the General Programming Boards category; @John_ even i am not sure of the value of 4 but from what i know it returns the size ...

  1. #16
    Registered User
    Join Date
    Dec 2005
    Posts
    50
    @John_

    even i am not sure of the value of 4 but from what i know it returns the size of the pointer. not the one it points to. i know that pointers also occupies its own memory address so pointers also have its size.

    hope i does not made you confuse...


    Code:
    long * w;
    int * x;
    float * y;
    char * z;
    
    std::cout<< sizeof(w) <<std::endl;
    std::cout<< sizeof(x) <<std::endl;
    std::cout<< sizeof(y) <<std::endl;
    std::cout<< sizeof(z) <<std::endl;
    
    //all will return 4 the size of their pointers.

  2. #17
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Hi,

    A pointer variable is a variable that stores an address in memory. An address in memory is a series of numbers. Normally, if you cout<< a pointer variable, you can see the address displayed in hexadecimal format. Try this:
    Code:
    int nums[] = {1, 2, 3};
    cout<<nums<<endl;
    However, if you try that on a pointer to a char array, you won't get an address. cout<< is programmed differently to handle pointers to char arrays. Instead of outputting the contents of the pointer variable, i.e. the address, cout<< goes to the address in memory contained in the pointer variable and tries to output all the values in the array located there. It looks for a '\0' to know when to stop. If there isn't a '\0' at the end of the char array, then cout<< will overrun the end of the array and output a bunch of junk. The main point is, in an attempt to understand what a pointer variable really contains, using cout<< on char arrays isn't insightful.

    The reason the code outputs 4 is because sizeof() returns the size of the pointer variable. A pointer variable contains an address, and an address takes up 4 bytes of memory. The reason the code outputs 1 is because the value at that location in memory, which is obtained by dereferencing the pointer, is a char, and by definition sizeof() returns 1 for a char type.
    Last edited by 7stud; 02-09-2006 at 01:43 AM.

  3. #18
    Registered User
    Join Date
    Jan 2006
    Posts
    63
    Thank you.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. dynamically setting size of 2d char array
    By waxydock in forum C Programming
    Replies: 4
    Last Post: 05-13-2007, 10:58 PM
  2. Error with a vector
    By Tropicalia in forum C++ Programming
    Replies: 20
    Last Post: 09-28-2006, 07:45 PM
  3. Need help on pointer to VOID
    By dv007 in forum C Programming
    Replies: 19
    Last Post: 07-09-2002, 06:15 PM
  4. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21