I am writing a simple program in c so I can understand better the language but I have a strange problem. As you see from the code below I have only one loop that it exits when I insert 255 as a value. The problem is that when I select the first(insert option) and after I insert a name the program starts something like a looping and gives me all the time the selection screen...
#include<stdio.h>
#include<stdlib.h>
struct student{
char *name;
int id;
};
void insertStudent(void);
struct student * init(void);
int main(){
struct student *p;
int selectionCode=0;
whi开发者_开发问答le(selectionCode!=255){
printf("\nInsert students:1");
printf("\nDisplay students:2");
printf("\nExit:255");
printf("\n\nEnter selection:");
scanf("%d",&selectionCode);
p=init();
switch(selectionCode){
case 1:
insertStudent();
//printf("1\n");
break;
case 2:
//printf("2\n");
break;
case 255:
break;
}
}
//p->name="stelios";
//p->id=0;
//printf("Name:%s ID:%d",p->name,p->id);
//free(p);
//p=NULL;
return 0;
}
struct student *init(void)
{
struct student *p;
p=(struct student *)malloc(sizeof(struct student));
return p;
}
void insertStudent(void){
struct student *p;
p=init();
printf("Enter Name:");
scanf("%s",p->name);//return 1;
printf("Enter ID:");
scanf("%d",&p->id);
//printf("test");
}
Part of the problem may be that the code is not allocating memory for the name
field in the structure. The init
function allocates a new structure but does not initialize the name
field. The insertStudent
function then uses scanf
to read into that uninitialized pointer. That results in writing to "random" memory and can result in any number of problems including an access violation.
Looks like you have a memory leak, I would pass p into insertStudent().
You also have a return 1;
in the middle of the insertStudent() call, so it will be returning before finishing its job.
You have "return 1;" after you scan in the name. It looks like logically you should not be returning at that point, since you want to enter in the ID. Also, you declared the function as returning "void" so returning one is an error.
Edit: The real problem is that you never allocated space for the name string.
try to:
struct student *insertStudent(void){
struct student *p;
p=init();
printf("Enter Name:");
scanf("%s",p->name);
printf("Enter ID:");
scanf("%d",&p->id);
//printf("test");
return p;
}
On the main
case 1:
free(p);
p=insertStudent();
//printf("1\n");
On the init you have to allocate space for the name.
What a mess... :-) You never malloc() a buffer for p->name, but you are filling if with the scanf(). That is corrupting the memory of your program. Besides... In your functions you are using the variable p and in your main program as well. This is NOT the same variable, but you seem to assume it is. Another problem: return 1; after the scanf() aborts the insertStudent() function so "enter ID " is never executed. It is a void function so it should not return a value, by the way. The compiler has probably issued a warning about that.
There is probably more wrong with it, but this is what I spot after giving it a quick once over.
You need to remove the "return 1;" from insertStudent, otherwise is will no compile.
You should initialize p->name with malloc and change "scanf("%s",p->name);" to "scanf("%s", &p->name);", because you need a pointer to *char.
精彩评论