指针结构 [英] Pointer to structure
问题描述
我是新来的指针,并试图用一个指针结构。但第一个条目后,我的程序崩溃。请帮助我。
I am new to pointers and trying to use a pointer to the structure. But after the first entry, my program crashes. Kindly assist me.
这里的结构定义:
struct students{//structure students definition
char name[20];
char RegNo[15];
char CourseUnit[10];
int score;
char grade;
};
的等级不应该由用户输入,而是由程序计算的。
The grade should not be entered by the user but instead computed by the program.
下面是我的code到目前为止,我已经写了:
Here's my code so far I have written:
int main()//start of main
{
struct students *myStudPtr,myStud[SIZE];
myStudPtr=&myStud[SIZE];
int i;//declare variables
int count;
printf("How many students do you want to deal with?\n");
scanf("%d",&i);//number of entries
for(count=1;count<=i;count++) {
printf("Enter student's name\n");
scanf("%s",&(*myStudPtr).name);
printf("Enter the student's registration number\n");
scanf("%s",&(*myStudPtr).RegNo);
printf("Enter the course unit\n");
scanf("%s",&(*myStudPtr).CourseUnit);
printf("Enter the marks scored\n");
scanf("%d",&(*myStudPtr).score);
}
printf("NAME\tREGISTRATION\t\tCOURSE UNIT\tSCORE\t\tGRADE\n");//tabulates the output
printf("%s\t", myStudPtr->name);
printf("%s\t\t", myStudPtr->RegNo);
printf("%s\t", myStudPtr->CourseUnit);
printf("%d\t\t", myStudPtr->score);
if(myStudPtr->score>100) {
printf("Invalid\n");
} else if(myStudPtr->score<40) {
printf("FAIL\n");
} else if(myStudPtr->score<50) {
printf("D\n");
} else if(myStudPtr->score<60) {
printf("C\n");
} else if(myStudPtr->score<70) {
printf("B\n");
} else if(myStudPtr->score>70) {
printf("A\n");
} else {
printf("Invalid");
}
return 0;
}
请assist.Thanks提前。
Kindly assist.Thanks in advance.
推荐答案
您有一个<一个href=\"http://stackoverflow.com/questionhttp://stackoverflow.com/questions/671703/array-index-out-of-bound-in-cs/671703/array-index-out-of-bound-in-c\">index-out-of势必错误导致未定义行为:
myStudPtr = &myStud[SIZE];
// ^^^^^
// wrong
据申报结构同学myStud [SIZE];
,最大索引值可以是尺寸 - 1
。记住数组的索引与 0
开始。
According to declaration struct students myStud[SIZE];
, Maximum index value can be SIZE - 1
. Remember array index starts with 0
.
修改
至于我能理解,你声明结构的一个数组,想要阅读的使用指向struct用户学生的信息 I
号码。但也有在$ C $一些问题,例如Ç在for循环中,你总是访问的相同的结构元素:
As I can understand, you have declared an array of struct, and wants to read i
number of student's information from user using pointer to struct. But there are some more problems in your code e.g. in for loop you always access same struct element:
for(count = 1; count <= i; count++)
scanf("%s", &(*myStudPtr).name);
// ^
// points to same struct element in array
这个错误是在每一个 scanf()的
和的printf()
语句present。
This mistake is present in every scanf()
and printf()
statements.
正确的将是如下:
-
初始化指针指向第一个元素的数组地址:
Initialize pointer to the address of first element in array:
myStudPtr = &myStud[0];
// ^ first element at 0th index
通过指针你可以简单地访问结构的元素在以下两种方式中的任何一个:
Via pointer you can simply access struct's elements in any one of following two ways:
第一:例如扫描评分
学生的值:
First: for example to scan score
value of a student:
scanf("%d", &myStudPtr[count].score);
注意: precedence 的 []
'数组下标运算符高则。
,所以你做'通过对象名称运营商成员选择不需要()
括号。另外$的对$ pcedence
经营者高于&放大器;
符号运算符所以即使你不需要任何圆括号获取地址(例如及(myStudPtr [统计] .score)
不需要)。
Note: precedence of []
'array subscript operator' is higher then .
'member selection via object name operator' so you do not need ()
parenthesis. Also precedence of .
operator is higher then &
ampersand operator so even you do not need any parenthesis to get address(for example &(myStudPtr[count].score)
not needed).
二:采用指针学生的扫描评分
值和 - &GT;
运营商:
Second: to scan score
value of a student using pointer and ->
operator:
scanf("%d", &(myStudPtr + count)->score);
注意: +
加运算具有较低的precedence所以我们需要括号覆盖precedence。和precedence - &GT;
成员选择通过指针操作者高于&安培;
符号运算符所以在这里你也不需要像&放任何括号((myStudPtr +计数) - GT;分)
。
Note: +
plus operator has lower precedence so we need parenthesis to overwrite precedence.
And precedence of ->
member selection via pointer operator higher then &
ampersand operator so here also you do not need any parenthesis like &((myStudPtr + count)->score)
.
重要的是注意事项:
-
您应该由用户检查
I
的输入值必须小于为尺寸
(大小strcut数组),否则,你可能在你的code未定义的行为。
You should check entered value of
i
by user that must be less toSIZE
(the size of strcut array) otherwise you may have undefined behavior in your code.
要读取字符串使用安全与fgets()
函数,而不是scanf函数,以避免缓冲区溢出。阅读:<一href=\"http://stackoverflow.com/questions/17294809/reading-a-line-using-scanf-not-good/17294869#17294869\">\"Reading使用 scanf()的行
不是很好吗?
To read string use safe fgets()
function instead of scanf to avoid buffer overflow. Read: "Reading a line using scanf()
not good?"
一个侧面说明:
-
由于你是新的C程序员(我觉得)你应该阅读:缩进C活动它的快速教程学习实践压痕。
As you are new C programmer(I feel) you should read: Indenting C Programs its a quick tutorial to learn indentation practice.
您应该始终保持在空间
和;
,让您的code可读,出于同样的原因,一个前pression像计数&LT; = I
应该写成计数&LT; = I
。比较你的for循环:
You should always keep space after ,
and ;
to make your code readable, and for the same reason an expression like count<=i
should be written as count <= i
. Compare your for loop:
for(count=1;count<=i;count++)
和下面,我想建议你:
for(count = 1; count <= i; count++){
// code after one tab
}
提高code:
我也想建议你提高的if-else编码风格,你的if-else code的一部分,可以写成如下:
I would also like to suggest you to improve if-else coding style, your if-else part of code can be written as follows:
score = myStudPtr->score;
if(0 <= score && score <= 100){
if(score > 70)
printf("A");
if(60 <= score && score < 69)
printf("B");
if(50 <= score && score < 59)
printf("C");
if(40 <= score && score < 49)
printf("D");
if(score < 40)
printf("FAIL");
}
else
printf("Error: Invalid Entry!");
printf("\n");
- 我删除了许多
其他
语句,代替和放大器;&放;. - 删除冗余
{...}
括号对。 - 用于本地
评分
变量,而不是myStudPtr-&GT;评分
来保持code看起来很简单。 - 删除
\\ n
每个pritf
语句,而不是添加一个新的的printf
末(不是非常重要)。 - I removed many
else
statement, instead used &&. - Removed redundant
{..}
braces pairs. - Used local
score
variable insteadmyStudPtr->score
to keep code look simple. - Remove
\n
from eachpritf
statement instead add a newprintf
at the end (not very important).
过去的错误:
要打印的每个学生记录你需要,你叫的printf中的一个新的循环()
功能,包括的if-else逻辑来评价学生成绩。
To print each students record you need a new loop within that you call printf()
function and includes if-else logic to evaluate student grades.
这篇关于指针结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!