Another Pointer Question

This is a discussion on Another Pointer Question within the C Programming forums, part of the General Programming Boards category; I struggle understanding why the following is illegal: char *pointer = "hello"; pointer[2] = 'L'; yet putchar(pointer[2]); and the following ...

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    16

    Another Pointer Question

    I struggle understanding why the following is illegal:

    char *pointer = "hello";
    pointer[2] = 'L';

    yet putchar(pointer[2]); and the following is legal:

    char blah[] = "hello";
    blah[2] = 'L';

    Thanks for the help

  2. #2
    TK
    Guest
    The first initialization is 'read only'.

  3. #3
    Registered User
    Join Date
    Jul 2002
    Posts
    28
    There must be some other problem because I got this to compile and run with turbo C.


    Code:
    #include <stdio.h>
    
    int main(void)
    {
     char *pointer = "hello"; 
     pointer[2] = 'L'; 
    
     char blah[] = "hello"; 
     blah[2] = 'L'; 
    
     printf(" %s", pointer);
     printf(" %s", blah);
    } // end int main(void)

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >The first initialization is 'read only'.

    >There must be some other problem because I got this to compile and run with turbo C.

    TK is correct here. This
    >char *p = "hello";
    is a pointer to a string constant, and the compiler can stick the string in read only memory if it wants to (for optimisation purposes).

    This is a pitfall many people seem to fall into, I'm afraid.

    FYI, I can compile this code on Borland 5.5, and it will work, but if I use gcc it won't.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Registered User
    Join Date
    Jul 2002
    Posts
    28
    Hammer is of course correct which tells me if you're going to answer questions in this forum you had better try it on more than
    one platform or you will get "THE HAMMER"

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by jerryvtts
    Hammer is of course correct which tells me if you're going to answer questions in this forum you had better try it on more than
    one platform or you will get "THE HAMMER"

    When you're bored and have nothing better to do, I recommend reading this faq and buried in it you will find this. It's how I learnt about a few of the quirks in C.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. Easy pointer question
    By Edo in forum C++ Programming
    Replies: 3
    Last Post: 01-19-2009, 09:54 AM
  3. char pointer to pointer question
    By Salt Shaker in forum C Programming
    Replies: 3
    Last Post: 01-10-2009, 10:59 AM
  4. Pointer question
    By rakan in forum C++ Programming
    Replies: 2
    Last Post: 11-19-2006, 01:23 AM
  5. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM

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