Thread: Char pointers

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

    Join Date
    Aug 2013
    Posts
    4
    Rep Power
    0

    Question Char pointers


    Is a string literal
    Code:
    "Hello"
    the same as char pointer
    Code:
    char * p = "Hello"
    . If the answer is yes, HOW?

    Also, the char pointer, p pointed to the first element of the string. How did p know where to point in the string?
  2. #2
  3. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,592
    Rep Power
    4207
    They are actually two different things. Here's what happens when you declare:
    Code:
    char *p;
    The compiler allocates some memory for a variable called p, which is declared to be of type char pointer. Currently, variable p is not pointing to anywhere, because you've only declared a char pointer, not actually set its value.

    Now say you use a literal string, say "Hello World", in your code like this:
    Code:
    puts("Hello World");
    The compiler allocates some memory for a char array in memory somewhere, which contains:
    Code:
    {'H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd', '\0'}
    Then it calls puts() and passes it the address of the first element of this array, namely the memory location that contains 'H'. Note that when creating the array, the compiler automatically adds a '\0' (i.e. ASCII 0) at the end.

    Now, when you declare this:
    Code:
    char *p = "Hello World";
    The compiler allocates some memory for a char array in memory somewhere, which contains:
    Code:
    {'H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd', '\0'}
    Then it allocates some more memory for p and sets the value of p to point to the address of the first element of the array, i.e. the address of the memory location containing 'H'.

    Make sense?

    By the way, when you do this:
    Code:
    char *p = "Hello World";
    puts("Hello World");
    Some of the older C compilers would allocate two separate arrays, each containing the string "Hello World", one for each time the string literal occurs in the code. So the two string literals would be stored in different memory addresses, even though they contain the same data. Newer C compilers get smart about this and figure out a list of the unique string literals at the compilation stage and may only allocate one "Hello World" string and use that array for both statements. Most newer C compilers usually also have a switch to turn this behavior on or off (for example, in Visual C++, the /GF compilation option controls this).

    Some compilers even do substring merging. For instance:
    Code:
    char *p = "Hello World";
    char *q = "World";
    A smart compiler will notice that "World" is part of "Hello World" and therefore allocates memory for only one array containing:
    Code:
    {'H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd', '\0'}
    Then it allocates memory for the variable p and points it to the address of the memory location containing 'H'. Then it allocates memory for the variable q and points it to the address of the memory location containing 'W'. Again, this is compiler specific (the newer version of Visual C++ supports this, I believe) and the behavior can be controlled by setting various compiler options.

    Comments on this post

    • eramit2010 agrees
    Last edited by Scorpions4ever; August 10th, 2013 at 03:06 PM.
    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
    109
    Rep Power
    2
    If you think only about one word- there is NEARLY no difference.

    It will look like this in the memory:
    ["hello"]
    (p*)----->["hello"]

    Now imagine you have 300 words. How will you print them shortly ?
    1: you want to print them using a single(or a few) command. Then you will have to use a loop, inside which you print the words using the pointers
    Code:
    for(i=0; i<300; ++i)
        printf("%s\n\n", pointer[i]); %s AND (pointer) means print the contents of the address as a string
    2: If you want to sort the 300 words into alphabetical order-- Are you going to use strcmp(word1, word2) a million times ? No- you will give strcmp function the pointers of the words you wish to compare(inside a loop ofc), and when you need to swap them-- you will swap the pointers to these words. When you print them afterwards(THRU POINTERS), the order will be correct because the pointers have changed their positions.

    You can also use strcpy() to changed the order by copying each string into another, but i can imagine this increases run-time tenfold.

    >>About the second question-- It is built-in.
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    4
    Rep Power
    0
    Okay thanks. I think I got it...
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    109
    Rep Power
    2
    More efficient programs use pointers widely. Think about it- instead of moving whole arrays, structs, strings etc' you just swap the pointers- they are like passageways to these variables, and now they can lead you straight to another variable after swapping.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    31
    Rep Power
    1
    From what I understood :
    Code:
    char * string;
    string = "Hello";
    and
    Code:
    char * string = "Hello";
    are two different things : in the first one you allocate for the whole "hello string", and string is the address of the first element of "Hello", so string is the address of "H". So char * string will only points to "H". For printing the chain, you will need to do so (for example) :
    Code:
    char * string;
    string = "Hello";
    while(*string)
    {
    printf("%c", *string);
    string++;
    }
    
    //or
    
    printf("%s", string);
    but in the second case, you put the address of the chain "Hello" in the pointer, which means you can do this :

    Code:
    char * string = "Hello";
    printf("%s", string);
  12. #7
  13. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Location
    India
    Posts
    95
    Rep Power
    4
    as far as my understanding of strings and pointer both are same.

    string "Hello" is constant in both case so it will be stored in code section.

    if u use
    Code:
    puts("Hello");
    and use
    Code:
    char *ptr = "Hello";
    Memory for hello will be allocated in code section only.

    but in case of pointer u can read that memory block in future also because u have stored starting address of string "Hello" but in case of constant string u can only use it once.

IMN logo majestic logo threadwatch logo seochat tools logo