Dynamic string allocation in an array

Asked

Viewed 356 times

2

I have a problem to store a vector dynamically need to save 10 names but I’m not knowing how to access this data for later printing follows my attempt

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

#define tam 3

int media(int *v1);
char *mais_alta(int *v1);
char *mais_baixa(int *v1);

void quantas_e_quais_mais_altas();
void quantas_e_quais_mais_baixas();
void imprime(int t, char *v1, int *v2);

int main() {
    int i;

    char *vet1 = (char *) malloc(tam * sizeof(char) + 1);
    int *vet2 = (int *) malloc(tam * sizeof(int));

    for (i = 0; i < tam; i++) {
        printf("Nome % de altura %d\n", i, i);
        scanf("%s%d", &vet1[i], &vet2[i]);
    }

    imprime(tam, vet1, vet2);

    free(vet1);
    free(vet2);

    return 0;
}

void imprime(int t, char *v1, int *v2){
    int i;
    for (i = 0; i < t; i++) {
        printf("Nome:%s\nAltura:%d\n", v1[i], v2[i]);
    }
}

1 answer

1


There are several errors there, some not exactly mistakes but this is not how it is usually done:

The syntax errors I just fixed, I won’t even mention them. Pay attention to every detail of the code because I simplified, improved readability and made it easier to detect errors.

I think for this case dynamic allocation is not recommended, but maybe you want to exercise and use something different in the future, but know that this case is not how it is usually done.

You have to pass the vectors to the function, in the form used should already give the sizes since it has it globally available, if there was no difference, I will not complicate what does not need. Note that I passed a vector of char *, because before is passing only the char *. The char * is the string, is not a vector of strings, so at the time of declaring I did changing so that allocate a vector of strings.

I had to make the allocation of each string, again do not mix a character array that is the string with a vector of strings, that in the background is a vector of vectors of characters, so it has two pointers in the variable declaration vet1 (could have used better names).

I separated the reading because this form generates much confusion.

I didn’t forget to release the memory of these strings.

Obviously there are ways to get better.

#include<stdio.h>
#include<stdlib.h>
#define TAM 3

void imprime(char *v1[TAM], int v2[TAM]) {
    for (int i = 0; i < TAM; i++) printf("Nome:%s\nAltura:%d\n", v1[i], v2[i]);
}
int main() {
    char **vet1 = malloc(TAM);
    int *vet2 = malloc(TAM * sizeof(int));
    for (int i = 0; i < TAM; i++) {
        printf("Nome %d de altura %d\n", i, i);
        vet1[i] = malloc(21);
        scanf("%20s", vet1[i]);
        scanf("%d", &vet2[i]);
    }
    imprime(vet1, vet2);
    for (int i = 0; i < TAM; i++) free(vet1[i]);
    free(vet1);
    free(vet2);
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

  • Thanks @Maniero I’m starting data structure now and I’m having some difficulties your response was of great help!

Browser other questions tagged

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