Thread: doubt while defining array and pointer

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    53

    doubt while defining array and pointer

    Code:
    ..................
    ..................
    char a[]="Hello";
    char *b="World";
    .................
    .................
    when I compiled the above program in linux using gcc -S, I found only "World" in .rodata segment. Why "Hello" is not there in the .rodata segment?

  2. #2
    Registered User Char*Pntr's Avatar
    Join Date
    Sep 2007
    Location
    Lathrop, CA
    Posts
    198

    Smile

    Hi,

    It's my understanding that address of the first byte of a char arrray is the name itself. That seems to be the context you are using. Loose the [] off of "a", and you'll have just the name itself.

    Try instead this:

    Code:
    char *a = "Hello";
    Let me know if this helps.

    Happy coding! :-)
    Last edited by Char*Pntr; 08-23-2010 at 10:04 PM. Reason: typo

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Quote Originally Posted by karthik537 View Post
    Code:
    ..................
    ..................
    char a[]="Hello";
    char *b="World";
    .................
    .................
    when I compiled the above program in linux using gcc -S, I found only "World" in .rodata segment. Why "Hello" is not there in the .rodata segment?
    The first variable is modifiable, so it is built directly into the stack frame code. The second one is really a pointer to a constant (array of chars) which is generally stored in a segment reserved for static, initialized data.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  4. #4
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Quote Originally Posted by Char*Pntr View Post
    Hi,

    It's my understanding that address of the first byte of a char arrray is the name itself. That seems to be the context you are using. Loose the [] off of "a", and you'll have just the name itself.

    Try instead this:

    Code:
    char *a = "Hello";
    Let me know if this helps.

    Happy coding! :-)
    They are not functionally identical.

    What the OP has written is,
    Code:
    char a[6] = {'H', 'e', 'l', 'l', 'o', '\0'};
    If you're still wondering why "Hello" isn't in the .rodata segment. Then you shouldn't be poking around in there... (You shouldn't be anyway). String literals are implementation-defined regarding modification -- usually read-only.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Char*Pntr View Post
    Hi,

    It's my understanding that address of the first byte of a char arrray is the name itself. That seems to be the context you are using. Loose the [] off of "a", and you'll have just the name itself.

    Try instead this:

    Code:
    char *a = "Hello";
    Let me know if this helps.

    Happy coding! :-)
    Arrays are not pointers. They are similar, but they are not the same thing.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting the matrix question..
    By transgalactic2 in forum C Programming
    Replies: 47
    Last Post: 12-22-2008, 03:17 PM
  2. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  3. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM