Vector of struct and pointer to char

1

Why this syntax is wrong:

#include <stdio.h>

typedef struct {

  char* nome;
  char* numero;

}Agenda;

void adiciona(Agenda* reg, int i) {

    scanf("%s", reg[i]->nome);
    scanf("%s", reg[i]->numero);

}


void imprime(Agenda* reg, int i) {

  for(int j = 0; j < i; j++) {

    printf("Nome: %s | ", reg[j]->nome);
    printf("Numero: %s\n", reg[j]->numero);
  }

}



int main() {

  int sair = 0;
  int i = 0;

  Agenda registro[10];

  while(!sair) {

    int escolha;
    printf("O que voce deseja ? (1)Inserir (2)Imprimir (3)Sair\n");
    scanf("%d", &escolha);

    switch(escolha) {       
        case 1:
            adiciona(registro, i);
            i++;
            break;
        case 2:
            imprime(registro, i);
            break;
        case 3:
            sair = 1;
            break;
      } 


   }


}

And why when I ask to print the name and the contact leaves some strange characters and if it is more than one sai repeated? That is, as if the first and second position of the vector had the same value.

    
asked by anonymous 03.06.2017 / 00:22

1 answer

0

There are two problems. One is that is not allocating memory for the strings that will be pointed to in the structure. Note that there will only be a pointer indicating where the text is . And where is it? where was it placed? Nowhere whatsoever. And then access will be done somewhere near the random memory since it is not known where it was allocated.

Allocating memory with malloc() will ensure that the text has a place to be stored and returns a < a pointer for it, that pointer is to be placed in the structure.

And you can not use the pointer -> , . is correct since the allocation of the structures go directly into the array and not indirectly allocated, as occurs as the text.

I would still have the problem of not being releasing memory with free() , but in a simple exercise like this not a problem.

Ideally you should have a limit how many characters can be typed .

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

typedef struct {
    char *nome;
    char *numero;
} Agenda;

void adiciona(Agenda* reg, int i) {
    reg[i].nome = malloc(30);
    reg[i].numero = malloc(6);
    scanf("%s", reg[i].nome);
    scanf("%s", reg[i].numero);
}

void imprime(Agenda* reg, int i) {
    for (int j = 0; j < i; j++) {
        printf("Nome: %s | ", reg[j].nome);
        printf("Numero: %s\n", reg[j].numero);
    }
}

int main() {
    int sair = 0;
    int i = 0;
    Agenda registro[10];
    while (!sair) {
        int escolha;
        printf("O que voce deseja ? (1)Inserir (2)Imprimir (3)Sair\n");
        scanf("%d", &escolha);
        switch(escolha) {       
            case 1:
                adiciona(registro, i);
                i++;
                break;
            case 2:
                imprime(registro, i);
                break;
            case 3:
                sair = 1;
                break;
        } 
   }
}

See running on ideone . And at Coding Ground . I also put it on GitHub for future reference .

    
03.06.2017 / 01:37