problem with assigning a string value to a char variable

This is a discussion on problem with assigning a string value to a char variable within the C Programming forums, part of the General Programming Boards category; Hey there! I am having a problem again but this time, just I am not so sure with the string ...

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    23

    Angry problem with assigning a string value to a char variable

    Hey there! I am having a problem again but this time, just I am not so sure with the string in C.

    Why cant I do something like this below chunk of code???

    Code:
    #include <stdio.h>
    int main(void)
    {
    char myName[100];
    
    myName="This is my real name"; // error might be here!!!, 
    
    return 0;
    }
    My Visual Studio 2005 compiler never compile such code, it only returns some kind of error like this:

    Code:
    left operand must be l-value

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Because it's considered illegal.

    Here are two options:

    Code:
    char myName[100] = "This is my real name";
    Code:
    char myName[100];
    ...
    strcpy(myName, "This is my real name");

  3. #3
    Registered User
    Join Date
    Nov 2007
    Posts
    23
    So it means i have to use de strcpy when i want assign any value to a variable of type char? Or is there any other easier way?

  4. #4
    Aia
    Aia is offline
    Beautiful to C Aia's Avatar
    Join Date
    Jun 2007
    Posts
    124
    Quote Originally Posted by whichet View Post
    So it means i have to use de strcpy when i want assign any value to a variable of type char? Or is there any other easier way?
    You seems to be confused with what a variable of type char is.
    Code:
    char character;
    That's a variable of type char. It can hold only one character; something like 'A'
    When you do this
    Code:
    char myName[100] = "This is my real name";
    You are trying to get 'T', 'h', 'i', 's', ' ', 'i', etc..., into myName[0], myName[1], myName[2], etc...
    However the compiler does not know which variable char corresponds to which myName[].
    So strcpy basically makes that possible by assigning the following:
    Code:
    myName[0] = 'T';
    myName[1] = 'h';
    myName[2] = 'i';
    myName[3] = 's';
    .
    .
    .
    etc.
    I hope you understand better now.
    There's not such a thing as string variable in C. Strings are made of a sequence of variables of type char.

  5. #5
    Registered User
    Join Date
    Nov 2007
    Posts
    23
    Thanks for givin the explanation. I got that. But when i start to learn about pointer and string, i get confused! I have been tryin to read more about it now. Maybe you can suggest to me a good reference website.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by whichet View Post
    Thanks for givin the explanation. I got that. But when i start to learn about pointer and string, i get confused! I have been tryin to read more about it now. Maybe you can suggest to me a good reference website.
    There is a tutorial on this website. That together with writing your own code, and asking questions, will get you there.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Aia
    Aia is offline
    Beautiful to C Aia's Avatar
    Join Date
    Jun 2007
    Posts
    124
    Quote Originally Posted by whichet View Post
    Maybe you can suggest to me a good reference website.
    This pointers explanation seems to be easy to grasp.

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,823
    I don't know about strcpy_s if it's a Microsoft-only or non-portable? Otherwise it's preferred to use it since strcpy is also deprecated in VC++ 2005.

  9. #9
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211

  10. #10
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,823
    So, in other words, it may not be cross-compatible, but it isn't Microsoft-only.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Custom String class gives problem with another prog.
    By I BLcK I in forum C++ Programming
    Replies: 1
    Last Post: 12-18-2006, 02:40 AM
  2. lvp string...
    By Magma in forum C++ Programming
    Replies: 4
    Last Post: 02-26-2003, 11:03 PM
  3. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 01:45 PM
  4. Strings are V important...
    By NANO in forum C++ Programming
    Replies: 15
    Last Post: 04-14-2002, 11:57 AM
  5. String Processing Problem
    By muffin in forum C Programming
    Replies: 0
    Last Post: 08-24-2001, 10:13 AM

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