访问结构非数组元素
Getting access to structure non-array element
我在我的代码中声明了一个结构 struct Stdinfo
,其中包含学生的姓名和分数。
#include <stdio.h>
struct Stdinfo{
char name[30];
int score;
};
然后我在一个名为 struct Stdinfo CreatStruct()
的函数中创建了这个结构。还应该提到我的结构变量是数组。构造完成后,我将其地址存储在指针 struct Stdinfo *structPtr
中。然后我将地址传递给下面的函数:
void FindAverage(struct Stdinfo *structPtr ,size_t stdnum){
int j;
for(j = 1 ; j < stdnum ; j++ )
{
//Error line
if( (structPtr[j] -> score) < (structPtr[j] -> score ) ) {
/*...
*
* ?
*
*/
}
}
////printf(The answer);
}
我的全部代码:
#include <stdio.h>
#include <string.h>
struct Stdinfo{
char name[30];
int score;
};
struct Stdinfo CreatStruct();
void FindAverage(struct Stdinfo * ,size_t );
int main(void)
{
//Number of students ~stdnum
size_t stdnum;
int i;
puts("Input numbers of student(s) :") ;
scanf("%Iu" ,&stdnum);
struct Stdinfo student[stdnum] ;
//Filling array of structure
for(i=0 ; i < stdnum ; i++)
{
student[i] = CreatStruct() ;
}
struct Stdinfo *structPtr;
structPtr= student;
FindAverage(structPtr ,stdnum);
return 0;
}
struct Stdinfo CreatStruct() {
struct Stdinfo student;
getchar(); // Consume newline from previous input
printf("Input the name of the student : ") ;
fgets(student.name , sizeof (student.name) , stdin);
// remove trailing newline from 'fgets()'
student.name[strcspn(student.name, "\n")] = 0;
puts("Input his(her) score:") ;
scanf("%i" ,&student.score) ;
return student ;
}
void FindAverage(struct Stdinfo *structPtr ,size_t stdnum){
int j;
for(j = 1 ; j < stdnum ; j++ )
{
if( ( structPtr[j] -> score ) < ( structPtr[j] -> score ) ) { //Error line
/*...
*
* ?
*
*/
}
}
////printf(The answer);
}
我的目标 是打印最高分,但我不知道如何访问它。
如果有人帮助我解决这个问题,我将不胜感激。
将structPtr[j] ->
替换为structPtr[j].
您遇到的错误是因为您两次取消引用数组。 ->
运算符只是 (*x).
的语法糖,而 [N]
只是 *(x + N)
的语法糖,所以在您的情况下,structPtr[j]->
转换为 **(structPtr + j)
,当为了访问数组时,您只需要像这样取消引用它一次:*(structPtr + j)
.
我在我的代码中声明了一个结构 struct Stdinfo
,其中包含学生的姓名和分数。
#include <stdio.h>
struct Stdinfo{
char name[30];
int score;
};
然后我在一个名为 struct Stdinfo CreatStruct()
的函数中创建了这个结构。还应该提到我的结构变量是数组。构造完成后,我将其地址存储在指针 struct Stdinfo *structPtr
中。然后我将地址传递给下面的函数:
void FindAverage(struct Stdinfo *structPtr ,size_t stdnum){
int j;
for(j = 1 ; j < stdnum ; j++ )
{
//Error line
if( (structPtr[j] -> score) < (structPtr[j] -> score ) ) {
/*...
*
* ?
*
*/
}
}
////printf(The answer);
}
我的全部代码:
#include <stdio.h>
#include <string.h>
struct Stdinfo{
char name[30];
int score;
};
struct Stdinfo CreatStruct();
void FindAverage(struct Stdinfo * ,size_t );
int main(void)
{
//Number of students ~stdnum
size_t stdnum;
int i;
puts("Input numbers of student(s) :") ;
scanf("%Iu" ,&stdnum);
struct Stdinfo student[stdnum] ;
//Filling array of structure
for(i=0 ; i < stdnum ; i++)
{
student[i] = CreatStruct() ;
}
struct Stdinfo *structPtr;
structPtr= student;
FindAverage(structPtr ,stdnum);
return 0;
}
struct Stdinfo CreatStruct() {
struct Stdinfo student;
getchar(); // Consume newline from previous input
printf("Input the name of the student : ") ;
fgets(student.name , sizeof (student.name) , stdin);
// remove trailing newline from 'fgets()'
student.name[strcspn(student.name, "\n")] = 0;
puts("Input his(her) score:") ;
scanf("%i" ,&student.score) ;
return student ;
}
void FindAverage(struct Stdinfo *structPtr ,size_t stdnum){
int j;
for(j = 1 ; j < stdnum ; j++ )
{
if( ( structPtr[j] -> score ) < ( structPtr[j] -> score ) ) { //Error line
/*...
*
* ?
*
*/
}
}
////printf(The answer);
}
我的目标 是打印最高分,但我不知道如何访问它。
如果有人帮助我解决这个问题,我将不胜感激。
将structPtr[j] ->
替换为structPtr[j].
您遇到的错误是因为您两次取消引用数组。 ->
运算符只是 (*x).
的语法糖,而 [N]
只是 *(x + N)
的语法糖,所以在您的情况下,structPtr[j]->
转换为 **(structPtr + j)
,当为了访问数组时,您只需要像这样取消引用它一次:*(structPtr + j)
.