I am writing a program with structs and pointers. As you will see in the program I am asking the user if he wants to continue adding students. And I want to check if his answer is Yes or No. Obviously I am doing something wrong because my program finishes after his answer. So I want help to spot the bug and to find a way to write the code correctly.
Also I want to ask why number[i].age needs an & in font of it in scanf. I mean it is a pointer so why use a &?
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct student {
char name[30];
int age;
}student ;
int main(){
typedef int size;
int i;
size siz=0;
student *number;
printf("Welcome\n\nEnter the number of student you want to evaluate: ");
scanf("%d",&siz);
printf("\n");
number=malloc(siz*(sizeof(struct student)));
for(i=0;i<siz;i++){
printf("Student name: ");
scanf("%s",number[i].name);
printf("Student age: ");
scanf("%d",&number[i].age);
printf("\n");
}
printf("Do you want to add Students(Yes or No)? ");
void * answer;
scanf("%s",*answer);
if(strcmp(answer, "Yes")==0){
printf("\n Give the number of the additional students: ");
int adsize;
scanf("%d",&adsize);
student*realloc=(number,adsize*sizeof(int));
int j=0;
for(j=0;j<adsize;j++){
printf("Student name: ");
scanf("%c",number[j].name);
printf("Student age: ");
scanf("%d",number[j].age);
printf("\n");
}
}
else
printf("\nEnd of programm");
return 0;
}
You must learn to indent the code properly for you and others to gain readability.
Here is a reformatted version of your code:
Some problems appear immediately:
typedef int size;defines a type with a local scope in the functionmain. Avoid this type of definition that is confusing and error prone.in
scanf("%d", &siz), you do not test the return value ofscanf(). This is risky as invalid or missing input will be ignored and cause unexpected behavior if not undefined behavior later in the code. Always test for and report invalid or missing input:you allocate memory with
mallocbut do not check for allocation failure. testing for and reporting memory allocation failure will help track problems too.scanf("%s", number[i].name);does not need the&becausenumber[i].nameis an array and arrays are always passed as a pointer to their first element. Yet thisscanf()call is risky because you do not providescanf()with a way to prevent buffer overflow on overlong input. Use this instead:scanf("%d", &number[i].age);needs the&becausenumber[i].ageis anintmember and you must pass its address toscanffor it to store the converted value. Of course you must test the return value to detect and report invalid or missing input.void *answer; scanf("%s", *answer);is completely bogus: you dereference avoidpointer, which would not compile in Standard C (your compiler might support it an extension) andansweris uninitialized anyway, so dereferencing the pointer has undefined behavior, probably a segmentation fault, which causes your program to stop as observed. You should write this instead:student*realloc=(number,adsize*sizeof(int));is the most surprising! it is actually parsed aswhich defines a local variable
reallocas a pointer tostudent, initialized with the conversion ofadsize * sizeof(int)as a pointer... quite meaningless and no side effect on the array pointed to bynumber.in the second loop,
scanf("%c", number[j].name)uses the wrong conversion specifier%cwhich reads a single character into acharvariable, not a string into an array ofchar. Only the first character will be read (most likely a pending newline) and stored into the first entry of thenamearray, leaving the rest uninitialized, ie: not even forming a proper string. Usescanf("%29s", number[j].name)instead and check the return value.You should definitely compile with extra warnings to detect such errors using gcc -Wall -Wextra -Werror or similar options for your compiler.
Here is a modified version for you to study: