0

Basic, my problem is here:

(*(char *) &aluno.nome) = *lista_alunos[i-1];

the code above return just the first element.

and the code...

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct alunos {

    int id;
    char nome[50];
    int idade;
};

int main()
{

    struct alunos aluno;
    char var_struct[50] = {0};
    char *lista_alunos[] = {
        {"Mauricio"},
        {"Jose"}
        };

    memcpy(&aluno, var_struct, 50);
    for(int i=1;i<3;i++){
        (*(int *) &aluno.id) = i;
        (*(char *) &aluno.nome) = *lista_alunos[i-1];
        printf("Entrou no for\n");
        printf("%u\n", aluno.id);
        printf("%s\n", aluno.nome);
        // ((int) *&alunos)[i] = (struct alunos) {i, &lista_alunos[i-1] , 33};
    };
    return 0;
}

i resolved with this:

        sprintf(aluno.nome, lista_alunos[i-1]);

this is the new code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct alunos {

    int id;
    char nome[50];
    int idade;
};

int main()
{

    struct alunos aluno;
    char *lista_alunos[] = {
        {"Mauricio"},
        {"Jose"}
        };

    for(int i=1;i<3;i++){
        aluno.id = i;
        sprintf(aluno.nome, lista_alunos[i-1]);

    };
    return 0;
}
Martino
  • 1
  • 3
  • You should use [`strcpy()`](https://man7.org/linux/man-pages/man3/strcpy.3.html) to copy c-style strings (that are trusted to be short enough). – MikeCAT Jun 17 '21 at 16:10
  • `memcpy(&aluno, var_struct, 50);` will not do anything useful, and not even initialize the full structure. Perhaps you wanted to do e.g. `memset(&aluno, 0, sizeof aluno)`? Then just do `struct alunos aluno = { 0 };` instead. And `(*(int *) &aluno.id) = i` is the same as `aluno.id = i`. – Some programmer dude Jun 17 '21 at 16:11
  • Also, the `printf` format `%u` is for **`unsigned int`**, while `aluno.id` is a (`signed`) `int`. Mismatching format specifier and argument type is actually *undefined behavior*. – Some programmer dude Jun 17 '21 at 16:12
  • thanks everyone, but didnt work. @MikeCAT strcpy not for this. i resolved the question with 'sprintf': sprintf(aluno.nome, lista_alunos[i-1]); – Martino Jun 17 '21 at 18:01
  • 1
    `sprintf(aluno.nome, lista_alunos[i-1]);` has a risk. Consider what will hapen when `lista_alunos[i-1]` contains `%`. It should be `sprintf(aluno.nome, "%s", lista_alunos[i-1]);`. – MikeCAT Jun 17 '21 at 18:07
  • tnks @MikeCAT! works fine! – Martino Jun 17 '21 at 18:42
  • I still don't understand why you can't do e.g. `strcpy(aluno.nome, lista_alunos[i-1])`? – Some programmer dude Jun 17 '21 at 19:21
  • And please start loops iterating over arrays with `0`, so you don't have to do something like `i-1`. – Some programmer dude Jun 17 '21 at 19:21
  • Humm..... do u copied and tried that? i did and didnt work. I dont know why this actually happens, but it happens. maybe because of pointers. really dont know. by the way.... the loop with 0 is just because i dont want to set i+1 at the ID. thnks 'one more time'! – Martino Jun 17 '21 at 19:30
  • That's weird, using `strcpy` [works for me](https://godbolt.org/z/j9WoWfbGo). – Some programmer dude Jun 17 '21 at 19:38
  • sorry about that. when i tried before, probably had more things wrong. now, because of u, i tried again and did work. thannnnks a lot! @Someprogrammerdude – Martino Jun 17 '21 at 19:53

0 Answers0