#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    30
    Rep Power
    2

    Strings in memory


    If I can get the base address of an array by simply using its name like
    Code:
    int array[2];
    printf("%p", array);
    How does a function like this
    Code:
    puts("String");
    get the base address of the string that is not declared with a name?
  2. #2
  3. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,643
    Rep Power
    4248
    The C compiler stores all string literals in the compiled code in a separate section of the program. Where it does depends on the compiler and the format of the executable file it generates: gcc calls that section .rodata (i.e. read only data), Visual C++ calls it .rdata etc. You can see this section if you ask your compiler to generate the assembly language for the C code (again, compiler specific option to do this).

    Regardless, say when you do this:
    char *p = "this is a string";

    The C compiler will allocate an array with "this is a string" in the .rodata section (or .rdata section or text section or whatever). Then, it will generate another variable for p and point it to the first element of the array.

    Similarly, when you call
    puts("hello world");
    the C compiler will allocate an array with "hello world" in the .rodata or .rdata or text section and then pass a pointer to the first element of this array to puts().

    For the record, have you tried this:
    printf("%p", "hello world");
    You'll see what I'm talking about if you run that.
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    30
    Rep Power
    2
    I have tried doing that, and I got the address of the first character.
    Thanks a lot man!
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    4
    Rep Power
    0
    Hey Scorpions4ever, I am kind of a new member here.
    About part of your post,

    The C compiler will allocate an array with "this is a string" in the .rodata section (or .rdata section or text section or whatever). Then, it will generate another variable for p and point it to the first element of the array.

    what do you mean by the "another variable for p"?
  8. #5
  9. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,643
    Rep Power
    4248
    ^^^
    Jeero, I'll answer your question in the new thread you created.
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    30
    Rep Power
    2
    I forgot to ask one more question. It is about the char arrays.
    Consider this character array.

    Code:
    char array[3] = "my";
    It creates 3 bytes in memory with characters 'm', 'y' and '\0'. I've read that the array's name is an address of the first character of the string. How can it be if it is of type char?
    Did it create a pointer variable?
  12. #7
  13. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Location
    India
    Posts
    95
    Rep Power
    4
    Yes actually it use pointer mechanism to access array elements.
    Array name represent address of first element.
    and when you declare char array[3] it reserve 3 byte in memory and address of first byte is stored.

    check below statement for how array and pointer are related to each other.
    Code:
    array[1]=*(array+1)
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    30
    Rep Power
    2
    Originally Posted by eramit2010
    Yes actually it use pointer mechanism to access array elements.
    Array name represent address of first element.
    and when you declare char array[3] it reserve 3 byte in memory and address of first byte is stored.

    check below statement for how array and pointer are related to each other.
    Code:
    array[1]=*(array+1)
    Where is the address of first byte stored?
  16. #9
  17. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Location
    India
    Posts
    95
    Rep Power
    4
    Originally Posted by MiguelC
    Where is the address of first byte stored?
    in array itself.

    array name is constant pointer.
    means char a[5];
    here a is char * const a;
    and 5 byte are allocated in the memory and base address is stored in a.
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    30
    Rep Power
    2
    please consider this code:

    Code:
    char array[] = "array";
    array is of type char[] and string "array" is of type char*. Am I right?
    If I am right, how is char[] type treated like a char pointer?

    By the way, sorry for questioning about arrays a lot of times. It is just so hard to grasp.
  20. #11
  21. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,242
    Rep Power
    2222
    Originally Posted by MiguelC
    please consider this code:

    Code:
    char array[] = "array";
    array is of type char[] and string "array" is of type char*. Am I right?
    If I am right, how is char[] type treated like a char pointer?

    By the way, sorry for questioning about arrays a lot of times. It is just so hard to grasp.
    In this case, memory for six characters is allotted to array (length of "array" plus one) and array is initialized to contain the string, "array". array is not being used as a pointer here.
  22. #12
  23. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    30
    Rep Power
    2
    Originally Posted by eramit2010
    in array itself.

    array name is constant pointer.
    means char a[5];
    here a is char * const a;
    and 5 byte are allocated in the memory and base address is stored in a.
    So eramit2010 is wrong?
  24. #13
  25. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,242
    Rep Power
    2222
    Originally Posted by MiguelC
    So eramit2010 is wrong?
    No, because he was offering an analogy in his attempt to answer your questions. In his analogy, there are two different a's, one being the array itself and the other being a const pointer to that array. In this analogy, that pointer has to be const, since the location of the array cannot change.

    In reality, there is only one a and that is the array, whose location still cannot change. But that array does still have a location, just as any other variable has a location. The syntax of the C programming language allows the name of the array to be used as if it were a pointer, except for any attempt to change the value of that "pointer".

    That means that C behaves as if eramit's analogy were literally true. His analogy is not literally true, but it is still correct.

    For you to arrive at the literal truth, you will need to read the assembly code that is actually generated by the compiler. Until then, accept the explanations of how the language works so that you will be able to use it properly. And as you grow and mature within the language, then you will be ready to learn what's happening down at the level where the machine code meets the silicon.

IMN logo majestic logo threadwatch logo seochat tools logo