Problem with pointer and malloc

I am new to C and I am looping about arrays and pointers when they reference strings. I can ask to enter 2 numbers (ints) and then return the one I want (first number or second number), without any problems. But when I ask for names and try to return them, the program crashes after entering the first name and is not sure why.

In theory, I am looking for backup memory for a name, and then expand it to include a middle name. Can someone explain why this breaks down?

Thank!

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



void main ()
{
    int NumItems = 0;

    NumItems += 1;
    char* NameList = malloc(sizeof(char[10])*NumItems);
    printf("Please enter name #1: \n");
    scanf("%9s", NameList[0]);
    fpurge(stdin);

    NumItems += 1;
    NameList = realloc(NameList,sizeof(char[10])*NumItems);
    printf("Please enter name #2: \n");
    scanf("%9s", NameList[1]);
    fpurge(stdin);

    printf("The first name is: %s",NameList[0]);
    printf("The second name is: %s",NameList[1]);

    return 0;

}
+3
source share
4 answers

I think your problem in this code:

scanf("%9s", NameList[0]);

, scanf , , , . -, , scanf , , .

. -, NameList, char *. , char * - , . char **, char * s. :

char** NameList;

. , . -, , :

NameList = malloc (sizeof(char*) * NumItems);

, , . , , , - 10:

int i;
for (i = 0; i < NumItems; ++i)
    NameList[i] = malloc (10); // Space for 10 characters

scanf("%9s", NameList[0]);

NameList[0] - char *, , .

- , , , . . , char * s, , , , char * , - . , , , , .

, , , . :

for (i = 0; i < NumItems; ++i)
    free (NameList[i]);
free (NameList);

, free . .

, :

free (NameList);
for (i = 0; i < NumItems; ++i)
    free (NameList[i]);

, Bad Things, , , , , .

, !

+6

NameList char *, . (char - , char * - , char ** - .)

NameList [1], , .

, :

  char (*NameList)[10];
  NameList = malloc(10*sizeof(char)*NumItems);

: . ( .) , sizeof (char) , 1. , .

+1

2d (, char s), . :

int main(){

int i;
int NumItems = 2;

/* Alocate a variable which every position points to an array of character */
char ** NameList = (char **) malloc(sizeof(char *) * NumItems);

/* For each position, allocate an array of 10 characters */
for(i = 0; i < NumItems; i++){
    NameList[i] = (char *) malloc(sizeof(char) * 10);
}

printf("Please enter name #1: \n");
scanf("%s", NameList[0]);

printf("Please enter name #2: \n");
scanf("%s", NameList[1]);

printf("The first name is: %s",NameList[0]);
printf("The second name is: %s",NameList[1]);

/* Free allocated memory. Always a good practice and prevents memory leaks. */
for(i = 0; i < NumItems; i++){
    free(NameList[i]);
}
free(NameList);

return 0;

}
+1

malloc :

#define NumItems 2
char NameList[NumItems][10];

printf("Please enter name #1: \n");
scanf("%9s", NameList[0]);

printf("Please enter name #2: \n");
scanf("%9s", NameList[1]);

printf("The first name is: %s",NameList[0]);
printf("The second name is: %s",NameList[0]);

, , .

0

Source: https://habr.com/ru/post/1791736/


All Articles