Struct vector and char pointer

Asked

Viewed 1,259 times

1

Why is this syntax 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 comes out some strange characters and if it is more of a repeated sai? That is, as if the first position and the second of the vector had the same value.

1 answer

2


There are two problems. One of them is that is not allocating memory to the strings that will be pointed out in the structure. Note that there will only be one pointer indicating where the text is. And where is it? Where was it placed? Anywhere else. And then access will be done somewhere virtually random from memory since it is not known where it was allocated.

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

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

I’d still have the problem of not being freeing the memory with free(), but in such a simple exercise is not a problem.

Ideally you should have one limit on 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;
        } 
    }
}

Behold working in the ideone. And in the repl it.. I also put in the Github for future reference.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.