Find the answer to your Linux question:
Results 1 to 6 of 6
Hello, int main(void) { char s[] = "Hello"; // here the data will be stored in stack char* ss = "Hello"; // Can u tell me where this data will ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    May 2009
    Posts
    21

    C: diff between char* a and char s[]


    Hello,

    int main(void) {
    char s[] = "Hello"; // here the data will be stored in stack
    char* ss = "Hello"; // Can u tell me where this data will be stored in process
    // memory layout
    ss[0] = 'a'; // gives segmentation fault, why
    }



    Thanks in advance
    Madhu

  2. #2
    Linux Enthusiast gerard4143's Avatar
    Join Date
    Dec 2007
    Location
    Canada, Prince Edward Island
    Posts
    714
    Its stored in read only memory.
    Make mine Arch Linux

  3. #3
    Just Joined!
    Join Date
    May 2009
    Posts
    21

    Reg: C: diff between char* a and char s[]

    Then what is the difference between the below two statements

    const char* p = (char*) malloc(50);
    p[0] = 'a'; // This will gives a compilation error

    const char pp[] = "Hello";
    pp[0] = 'a'; // This will gives a compilation error

    char* s = "Hello";
    s[0] = 'a'; // this gives a runtime error segmentation fault.

    Thanks in advance,
    Madhu

  4. #4
    Linux Enthusiast gerard4143's Avatar
    Join Date
    Dec 2007
    Location
    Canada, Prince Edward Island
    Posts
    714
    Number one its three statements not two. If your curious about programming at this level you should get a good tutorial on the debugger and familiarize yourself with bin utilities like objdump.

    The first two are suggestions to the compiler, to make the variables const. How the compiler achieves this const state is implementation dependent....I think.
    Last edited by gerard4143; 10-12-2010 at 12:31 PM.
    Make mine Arch Linux

  5. #5
    Linux Newbie
    Join Date
    Nov 2009
    Posts
    218

    Diff between char * and char[]

    char * means "character pointer". That is, it is the address of a character. It is a single "Field". Trying to refer to char CP like CP[] means you are trying to access an array called CP.

    char *CP[4] defines an array of 4 character pointers. CP[0] is the first of those pointers.

    char C[4] defines an array of four characters. C[0] is the first of those characters.

    Pointers are usually implemented as a word (sizeof(int)). Characters as 8/16 bits.

    Not sure why you get the compiler errors, what are they?

    Cheers - VP

  6. #6
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, in Chicago, or in a galaxy far, far away.
    Posts
    11,453
    Quote Originally Posted by madhuti View Post
    Then what is the difference between the below two statements

    const char* p = (char*) malloc(50);
    p[0] = 'a'; // This will gives a compilation error
    You have defined 'p' as a const char*, and then you are trying to modify it. You either need to declare it as simply a char* (recommended), or you need to cast away the const on the assignment (not recommended - a violation of the intention indicated by const).

    const char pp[] = "Hello";
    pp[0] = 'a'; // This will gives a compilation error
    Same reason as before. You cannot modify a const variable once it has been initialized.

    char* s = "Hello";
    s[0] = 'a'; // this gives a runtime error segmentation fault.
    You have initialized 's' with a string literal, which unless you change your compilation options is in read-only memory. So, when you try to modify it, the application will segfault as you discovered. The compiler should have also issued a warning about assigning a const string literal to a non-const variable.
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

Posting Permissions

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