Like Tree2Likes
  • 1 Post By c99tutorial
  • 1 Post By JohnGraham

Assigning value to char pointer

This is a discussion on Assigning value to char pointer within the C Programming forums, part of the General Programming Boards category; I'm confused once again. I thought we needed to allocate memory before assigning a value to a char* and also ...

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    796

    Assigning value to char pointer

    I'm confused once again.
    I thought we needed to allocate memory before assigning a value to a char*
    and also that we needed to use functions like strcpy() to copy something into it.
    Then how come this works and does not crash?

    Code:
    #include <iostream>
    using namespace std;
    int main()
    {
        char * buf;
        buf = "Hello";
        cout << buf << endl;
    
        buf = "World!!!!!!!!";
        cout << buf << endl;
    
        return 0;
    }
    Compiler MSVC++ 2013 with Code::Blocks.

  2. #2
    Registered User
    Join Date
    Nov 2012
    Posts
    1,100
    1. Pick a language for learning, C or C++.
    2. "Hello" is a character constant, which has read-only memory assigned to it by the compiler.

    You could imagine the code is working internally like this

    Code:
    #include <stdio.h>
    
    static const char STRING_LITERAL_1[] = "Hello";
    static const char STRING_LITERAL_2[] = "World!!!!!!!!";
    
    int main(void)
    {
        char * buf;
        buf = (char*)STRING_LITERAL_1;
        printf("%s\n", buf);
     
        buf = (char*)STRING_LITERAL_2;
        printf("%s\n", buf);
     
        return 0;
    }
    The static char buffers, as well as the "..." literals are all initialized at compile time. When you say buf = STRING_LITERAL_1 or buf = "..." then the address of that memory is assigned.

  3. #3
    Registered User
    Join Date
    Sep 2008
    Posts
    200
    Quote Originally Posted by Ducky View Post
    I thought we needed to allocate memory before assigning a value to a char*

    Code:
        buf = "Hello";
    In the code snipped above, the string "Hello" is allocated a slot in your binary. The compiler does this when it compiles your program so you can refer to it later. Essentially this is allocating memory, but it's not being dynamically allocated - it's statically allocated, so the string exists throughout the lifetime of your program.
    Ducky likes this.
    Programming and other random guff: cat /dev/thoughts > blogspot.com (previously prognix.blogspot.com)

    ~~~

    "The largest-scale pattern in the history of Unix is this: when and where Unix has adhered most closely to open-source practices, it has prospered. Attempts to proprietarize it have invariably resulted in stagnation and decline."

    Eric Raymond, The Art of Unix Programming

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    796
    Ah, Ok I get it now. Thanks.
    Compiler MSVC++ 2013 with Code::Blocks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. assigning char value to an int
    By theju112 in forum C++ Programming
    Replies: 3
    Last Post: 09-22-2012, 11:56 AM
  2. Replies: 3
    Last Post: 06-18-2012, 01:06 PM
  3. Replies: 8
    Last Post: 12-08-2009, 01:47 AM
  4. Assigning a value to a char
    By super_stripey in forum C Programming
    Replies: 5
    Last Post: 10-08-2004, 01:00 PM
  5. Assigning Const Char*s, Char*s, and Char[]s to wach other
    By Inquirer in forum Linux Programming
    Replies: 1
    Last Post: 04-29-2003, 10:52 PM

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