Thread: assignment of arrays problem

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    9

    assignment of arrays problem

    Hi.

    I am having a bit of a problem with a struct.

    Code:
    struct jogador
    {
        char nome[tamanho_nome];
        int ind_jogador;
        int area_j, area_r, area_c, area_i;
    } jogador1, jogador2;
    further ahead i want to assign a char array to jogador1.nome, like so:

    Code:
    char nome1[tamanho_nome],  // nome do jogador1
        nome2[tamanho_nome]; // nome do jogador2
    
        //jogador 1
        cout << "Nome do Jogador 1: ";
        cin.getline(nome1, tamanho_nome);
    
        //jogador 2
        cout << "Nome do Jogador 2: ";
        cin.getline(nome2, tamanho_nome);
    
        //dados relativos ao jogador 1
        jogador1.nome = nome1;
    
        ...
        ...
    When I try to compile the file it returns this error:

    "ISO C++ forbids assignment of arrays"

    Does anyone know why this happens? And what can i do to work around this?

    Thanks.

    BTW, i am using DEVC++ beta 5 (4.9.9.2), under Win XP SP2.

  2. #2
    C / C++
    Join Date
    Jan 2006
    Location
    The Netherlands
    Posts
    312
    maybe put an integer "array length" in your struct. And declare the "arraylength".
    Operating Systems:
    - Ubuntu 9.04
    - XP

    Compiler: gcc

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    9
    Quote Originally Posted by Ideswa
    maybe put an integer "array length" in your struct. And declare the "arraylength".
    I forgot to say that "tamanho_nome" is initialized as a const int in the beggining of the file, as

    const int tamanho_nome = 11;

    Was this what you were talking about?

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    > char nome[tamanho_nome];
    The best thing to do is change this to a string:
    Code:
    #include <string>
    .
    .
        std::string nome;
    Then you would simply change the remaining code to:
    Code:
    std::string nome1,  // nome do jogador1
        nome2; // nome do jogador2
    
        //jogador 1
        cout << "Nome do Jogador 1: ";
        getline(cin, nome1);
    
        //jogador 2
        cout << "Nome do Jogador 2: ";
        getline(cin, nome2);
    
        //dados relativos ao jogador 1
        jogador1.nome = nome1;
    If you must use char arrays, then use strcpy() to do the copy:
    Code:
    #include <cstring>
    .
    .
        //dados relativos ao jogador 1
        strcpy(jogador1.nome, nome1);

  5. #5
    Registered User
    Join Date
    Apr 2006
    Posts
    9
    Thanks a lot swoopy!

    I only tried the second aproach, and it worked great.

    I'll try the other one, too, but will probably stick with the second method.

    Thanks a lot.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. arrays problem
    By swapnil_sandy in forum C Programming
    Replies: 3
    Last Post: 11-30-2005, 04:14 PM
  2. Assignment of Two-Dimensional Arrays
    By LuckY in forum C++ Programming
    Replies: 4
    Last Post: 08-06-2004, 03:40 PM
  3. An assignment problem
    By EeeK in forum C Programming
    Replies: 1
    Last Post: 10-03-2003, 07:51 PM
  4. Problem with chars and char arrays
    By Zagaberoo in forum C++ Programming
    Replies: 3
    Last Post: 09-27-2003, 08:47 AM
  5. Assignment of two char arrays
    By moemen ahmed in forum C++ Programming
    Replies: 4
    Last Post: 07-07-2002, 12:44 AM