Pointer to String

This is a discussion on Pointer to String within the C Programming forums, part of the General Programming Boards category; I'm getting started with c and i have the following question: Within main() Code: char string[] = "Hallo World"; function(string); ...

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    5

    Pointer to String

    I'm getting started with c and i have the following question:

    Within main()
    Code:
    char string[] = "Hallo World";
    function(string);
    
    void function(char *str)
    {
        printf("%s\n",str);
        char *ptr = str + 5;
        *ptr = '-';
        printf("%s\n",str);
    }
    Output:
    Hallo World
    Hallo-World

    Why does the program doesn't work if i'm defining:
    char *string = "Hallo World";

  2. #2
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Why does the program doesn't work if i'm defining:
    char *string = "Hallo World";
    This defines str as a string literal, a string constant. The copiler puts it into read only memory.
    Kurt

  3. #3
    Registered User
    Join Date
    Apr 2012
    Posts
    5
    Thanks for the fast answer! But why the keyword "const" is not required? It seems that char* implicitly declares as const, same as const char[] ="Hallo World", and const char* = "Hello World" doesn't make sense then.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,717
    Quote Originally Posted by tinu73
    But why the keyword "const" is not required?
    Because there was a time where string literals existed but the const keyword did not, and the powers that be decided that allowing a string literal to be assigned to/to initialise a char* was the right choice, perhaps for backward compatibility.

    Quote Originally Posted by tinu73
    It seems that char* implicitly declares as const
    It doesn't.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    I don't remember exactly. It's some backwards compatibility issue.
    Kurt

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 01-15-2012, 05:09 AM
  2. How do I add to a pointer string?
    By scarlet00014 in forum C Programming
    Replies: 9
    Last Post: 10-15-2008, 01:21 PM
  3. string vector to string pointer manipulation
    By stanlvw in forum C++ Programming
    Replies: 11
    Last Post: 07-16-2008, 01:43 AM
  4. pointer to string.
    By cstudent in forum C Programming
    Replies: 6
    Last Post: 04-22-2008, 02:51 AM
  5. Pointer to String and Pointer to Char
    By vb.bajpai in forum C Programming
    Replies: 3
    Last Post: 06-15-2007, 03:03 PM

Tags for this Thread


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