Thread: A question regarding strings

  1. #1
    Registered User
    Join Date
    Jun 2007
    Location
    Mysore, India
    Posts
    14

    Question A question regarding strings

    Hi,

    I've a doubt working with strings. To store a name into a character array, I give like
    Code:
    char Temp[]="Array";
    . Here the declaration and the initialization of the array is done in a single step. But what if I want to initialize the array later in my program. Is there any way to do it?

    Thanks,
    Babu

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Since Temp is on the stack you'd still have to specify it's size, then you can copy data into it later.

    eg:
    Code:
    char Temp[32];
    
    /* ... */
    strcpy(Temp, "Hello World");
    /* Temp now holds "Hello World" */
    Basically the compiler fills in the empty brackets when you do: char Temp[]="Array";, it See's that 'Temp' only has to be 6 bytes long (Array + NUL)

  3. #3
    Registered User
    Join Date
    Jul 2007
    Posts
    19
    Code:
    char Temp[]="Array";
    is semantically

    Code:
    char const Temp[]="Array";
    What exists in memory is "Array" and 'Temp' is just an alias, NOT a variable per se...

    I would LOVE to stand corrected.

  4. #4
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Well, Temp is an actual array in this case, and "Array" is copied to it automatically (hence why on systems where string literals are read-only, you can alter these arrays).

    Code:
    char *Temp = "Array";
    Code:
    char Temp[] = "Array";
    Two totally different meanings. One points to the string literal (generally in read-only memory), and one is an actual array that has the string copied to it upon creation.

  5. #5
    Registered User
    Join Date
    Jul 2007
    Posts
    19
    The first is a pointer variable which on initialization points to the location where "Array" is stored, but can be reassigned to any time....

    The second is an array in which "Array" is stored. We MIGHT overwrite that location, but the compiler will definitely not allow us to do that directly.

    Am I making sense or am I just plain wrong man?

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    You're just plain wrong.
    But perhaps you should start your own thread rather than hijacking someone elses from several weeks ago.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    The second is an array in which "Array" is stored. We MIGHT overwrite that location, but the compiler will definitely not allow us to do that directly.
    Compiler will allows us to overwrite it unless yoy declare it as an const variable. Have a look at this code

    Code:
    #include <stdio.h>
    
    int main()
    {
        char temp[] = "string"; 
        
        /* You should be more careful here since it is temp3[6] */
        strcpy(temp,"Test");
        
        printf("&#37;s",temp);
        
        getchar();
        return 0;
    }
    
    /* my output
    Test
    */
    ssharish2005

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question about strings in c
    By gp364481 in forum C Programming
    Replies: 9
    Last Post: 11-13-2008, 06:32 PM
  2. Question About Strings
    By spanker in forum C++ Programming
    Replies: 1
    Last Post: 07-13-2008, 05:09 AM
  3. strings question
    By cstudent in forum C Programming
    Replies: 4
    Last Post: 04-18-2008, 07:28 AM
  4. Functions and Strings Question
    By StrikeMech in forum C Programming
    Replies: 4
    Last Post: 07-18-2007, 06:07 AM
  5. Strings question
    By kimimaro in forum C Programming
    Replies: 10
    Last Post: 03-15-2005, 12:14 AM