1.sprintf, scanf
Today, write a C program, I do not know why the error has been in use sprintf and scanf functions. Back to find relevant information, and finally a little clue. The following code:
char * str;
scanf("%s",str);
This code Why go wrong? Because there is no allocate memory for it, it will go wrong, in fact, sprintf function is similar. So we can have two kinds of solutions, one by malloc function dynamically allocated memory, and second, we can declare a string array of characters. code show as below:
char * str = (char *)malloc(100);//1
char strs[100] ;//2
2. Introduction The following function sprintf
We can use this function to achieve concatenate strings and numbers into a string of type int
char str1[100];
sprintf(str1,"name=%s\tid=%d\tscore=%d\n",stu[i].name,stu[i].stuId,stu[i].score);
3. Simple ordering
for(i=0;i<=9;i++){
for (j=i+1;j<=10;j++){
if(stu[i].score>stu[j].score)
{
temp=stu[i];
stu[i]=stu[j];
stu[j]=temp;
}
}
}
This ordering is not difficult, but that is because I did not have this piece of code is written to the boundary conditions, it is time to run the program has prompted Segmentation Fault .