Thread: String Declaration and Initialization Question

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    1

    String Declaration and Initialization Question

    Hello,

    I have been looking for an answer to this for quite sometime. In my beginners book, I learned that you need to allocate memory for a string in order to use it in a program (i.e. the way I did it with var2 below, or by using an array, etc.)

    However, I also tried the following. Declare a global char pointer with no initial value (i.e. like var1). In the main function, I seem to be able to assign a literal "string" to that variable without allocating memory.

    Is this correct, it seems to work. I think I remember seeing somewhere that a string literal evaluates to the address it is stored at and automatically contains a '\0' at the end.

    Or should this be handled by explicitly allocating like with var2?




    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    char var1*;
    char var2*;
    
    
    int main( int argc, char *argv[] )
    {
        /* assign literal string without using malloc , is this ok???, it seems to work*/
         var1 = "this is a test";
    
    
         /* use malloc to allocate memory        */
         /* test for success, then assign string */
         var2 = (char *) malloc( 81 * sizeof( char ) );
    
         if( var2 == NULL ) {
              puts( "couldn't get the memory!" );
              exit(1);
         }
    
          var2 = "this is a test";
    
          puts( var1 );
          puts( var2 );
    
          return 0;
          
    }

  2. #2
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    It depends on what processing you want to do. The compiler is responsible for storing the string literal somewhere in the heap space as an array of char. By pointing at it, var1 points to the first element of the string literal. The major difference between var1 and var2 is that the latter can modify the string (within the limits of its storage) it points to, but not the former.

  3. #3
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    You don't use = to assign to a char* string in C, otherwise you're just changing the pointer. Use strcpy() to copy a string into the memory that you allocate with malloc().
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    strictly speaking

    var1 should be declared as
    const char*


    also you do not need to cast malloc in C - read FAQ
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. initialization of char array (c style string)
    By manzoor in forum C++ Programming
    Replies: 1
    Last Post: 09-24-2008, 06:29 AM
  2. Brand new to C need favor
    By dontknowc in forum C Programming
    Replies: 5
    Last Post: 09-21-2007, 10:08 AM
  3. Errors
    By Rhidian in forum C Programming
    Replies: 10
    Last Post: 04-04-2005, 12:22 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  5. Variable Declaration & Initialization :: C++
    By kuphryn in forum C++ Programming
    Replies: 6
    Last Post: 12-26-2001, 10:22 AM