0

So I have one struct and I initialized a variable A with that struct data type then Ι put in some values. But now Ι need to take those values and put it into another variable B with the same struct data type. How can Ι achieve this?

struct s_Especialidade{
    int id;
    char nome[60];  
    char descricao[60];
    struct s_Especialidade *proximo;
}; 
typedef struct s_Especialidade  Especialidade;

PESPECIALIDADE p, *array;
p->nome = &array[i]->nome; //THIS LINE GIVES THE ERROR
Odys
  • 8,951
  • 10
  • 69
  • 111

3 Answers3

1

Since it is an array of characters, you need to copy each element of the array.

strcpy(p->nome, array[i]->nome) will do it, but for extra security look at strncpy where you can set a maximum length to avoid overruns.

John3136
  • 28,809
  • 4
  • 51
  • 69
1

Try that way :

memcpy( p->nome, array[i].nome, 60 * sizeof(char) );

Or generalizing the type as well, picking the type used in the p->nome array :

memcpy( p->nome, array[i].nome, 60 * sizeof(*(p->nome)) );

This is the generalized and secure way to copy an array into another (not only for strings).

Gauthier Boaglio
  • 10,054
  • 5
  • 48
  • 85
0

To extend the answer recommending strcpy() I'd use memcpy() and use a #defined length to make sure you always use the same value.

#define NOME_LENGTH 60    

struct s_Especialidade{
    int id;
    char nome[NOME_LENGTH];  
    char descricao[60];     
    struct s_Especialidade *proximo;
}; 
typedef struct s_Especialidade  Especialidade;

PESPECIALIDADE p, *array; 

memcpy(p->nome, array[i]->nome, NOME_LENGTH);

Things get even more complicated trying to consider what an assignment does, but, in an example program:

struct stuff {
    char buf[2];
};

int main() {
    struct stuff a;
    memcpy(a.buf, "aa", 2); // *Edit: "aa" is a bad example as it 
                            // actually becomes at compilation 3 bytes long 
                            // {'a','a','\0'} as noted in the comments

    struct stuff b;
    b.buf = a.buf; // *Edit: For illustrative purposes, an assignment 
                   // between two char[2] members is not correct and
                   // does not compile.
}

Compilation yeilds the error error: incompatible types when assigning to type ‘char[2]’ from type ‘char *’ for the b.buf = a.buf line.

The topic of pointers and arrays has been covered elsewhere, Is an array name a pointer? among others.

*Edit: As noted in the comments, in the above code if instead of b.buf = a.buf; the struct assignemnt b = a; were done, the internal members would be copied correctly. This is because struct to struct assignment is effectively memcpy(&b, &a, sizeof(b)); (Assign one struct to another in C)

Community
  • 1
  • 1
Macattack
  • 1,917
  • 10
  • 15
  • 1
    `char buf[3];` because string `"aa"` is terminated by `'\0'` ! You have 3 chars to copy. – Gauthier Boaglio Jun 18 '13 at 01:11
  • 1
    @Golgauth It isn't relevant that "aa" is terminated by `'\0'`, since only 2 chars are being copied from it and the destination is only 2 chars long. buf won't be NUL-terminated but that's a different matter. – Jim Balter Jun 18 '13 at 01:19
  • 1
    "Compilation yeilds the error " -- so do `a = b;` instead ... that works fine. – Jim Balter Jun 18 '13 at 01:20
  • 1
    @JimBalter Yes, I know that was just for a pedagogic purpose. In case someone read this page and tries to manipulate C strings from that.I'm not busy right now so... ;-) – Gauthier Boaglio Jun 18 '13 at 01:23