Thread: I am trying to set an element of a char array what am I doing wrong?

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    10

    I am trying to set an element of a char array what am I doing wrong?

    Hello world this is my first post. I thought I would try to learn c programming and I stumbled onto this

    >>>Why don't something like this work what am I doing wrong?<<<

    Code:
    #include<stdio.h>
    main()
    {
    
    // initialize my multidimensional char array
    
    char names[100][21];
    
    names[0] = "raymond";
    
    printf("&#37;s\n",names[0]);
    
    getchar();
    
    }

    How do I do something like that I am new to this thanks for your help?

    i dont want to do this

    char names[][21] = {{"raymond"}};

    all I want to do is this names[0] = "raymond";

    how come I can't do that thanks for your time and help bye!
    Last edited by raymond1234; 04-22-2007 at 10:50 AM.

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by raymond1234 View Post
    all I want to do is this names[0] = "raymond";
    That's unfortunate, because you can't do that.

  3. #3
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    there is no such thing like array assignement in c/c++
    use
    Code:
    strcpy(names[0], "raymond");
    Kurt

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    hmm... but shouldn't it be possible to initialise the string(s):
    Code:
    char names[100][21] = {"raymond"};
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Apr 2007
    Posts
    10
    thanks all my problem was solved Now I am ready to build my program thanks

    I am trying to make a cd database

    strcpy(names[0], "value you want");

    thank you!

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    laserlight is correct. There is such thing as array assignment, at the time you declare the array.
    Code:
    char array[][10] =
    {
        { "hello" },
        { "world" },
        { "foo" },
        { "bar" },
        { "baz" },
    };
    After that point, you have to change one element at a time (which is what strcpy does).


    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by quzah View Post
    laserlight is correct. There is such thing as array assignment, at the time you declare the array
    I never said you cannot initialize an array. I just said that you cannot assigne one array to another. ( c-string == char array ).
    Kurt

  8. #8
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by quzah View Post
    laserlight is correct. There is such thing as array assignment, at the time you declare the array.
    Despite the fact that it uses the '=' sign, I'd call that an initialization, not an assignment. Similar to how in C++ a declaration with initializer actually invokes a constructor, not the assignment operator.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Ironically, the comment in raymond1234's code reads "initialize my multidimensional char array". That is what made me think of literally initialising them in the first place.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  2. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 05:42 PM
  3. Modify an single passed array element
    By swgh in forum C Programming
    Replies: 3
    Last Post: 08-04-2007, 08:58 AM
  4. char array wrong value
    By yeller in forum C Programming
    Replies: 3
    Last Post: 07-27-2007, 08:27 PM
  5. comparing fields in a text file
    By darfader in forum C Programming
    Replies: 9
    Last Post: 08-22-2003, 08:21 AM