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

This is a discussion on I am trying to set an element of a char array what am I doing wrong? within the C Programming forums, part of the General Programming Boards category; Hello world this is my first post. I thought I would try to learn c programming and I stumbled onto ...

  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
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,158
    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
    ZuK
    ZuK is offline
    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
    20,968
    hmm... but shouldn't it be possible to initialise the string(s):
    Code:
    char names[100][21] = {"raymond"};
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    ZuK
    ZuK is offline
    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
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,158
    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
    20,968
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 04: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

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