将变量从全局变为本地 – C
我为一个项目编写了以下代码,但它没有通过单一测试,要求两个变量不是全局的,而是在main()
本地。 修改structexample1.c
,使变量student
和anotherStudent
不是全局变量,而是main的本地变量。 我模糊地理解了本地和全球的概念,但我不确定如何在我编写的代码中实现问题。
#include #include struct student_s { char* name; int age; double height; struct student_s* next; } student; struct student_s anotherStudent; void printOneStudent(struct student_s student) { printf("%s (%d) %s %s %.2lf %s\n", student.name, student.age, ",", "height", student.height, " m"); } void printStudents(const struct student_s* student) { while (student != NULL) { printOneStudent(*student); student = student->next; } } int main(void) { student.name = "Agnes McGurkinshaw"; student.age = 97; student.height = 1.64; student.next = &anotherStudent; anotherStudent.name = "Jingwu Xiao"; anotherStudent.age = 21; anotherStudent.height = 1.83; anotherStudent.next = NULL; printStudents(&student); return EXIT_SUCCESS; }
我知道我需要在main()
定义这些变量,但我不确定如何以不完全破坏我的代码的方式实现它们。 代码的输出应保持如下:
Agnes McGurkinshaw (97), height 1.64 m Jingwu Xiao (21), height 1.83 m
好吧,首先替换这个:
struct student_s { char* name; int age; double height; struct student_s* next; } student;
附:
struct student_s { char* name; int age; double height; struct student_s* next; };
(也就是说,在最后一行删除student
)。
此更改是必要的,因为您要定义结构类型,以便稍后可以定义struct student_s
类型的变量,但您不希望在此处定义student
变量,因为这会使其成为全局变量。
然后删除此行:
struct student_s anotherStudent;
最后,在使用它们之前在main()
声明两个变量:
int main(void) { struct student_s student; struct student_s anotherStudent; student.name = "Agnes McGurkinshaw"; student.age = 97; student.height = 1.64; student.next = &anotherStudent; anotherStudent.name = "Jingwu Xiao"; anotherStudent.age = 21; anotherStudent.height = 1.83; anotherStudent.next = NULL; printStudents(&student); return EXIT_SUCCESS; }
与您的问题没有直接关系,但无论如何您应该避免这种情况:
struct student_s { char* name; int age; double height; struct student_s* next; } student; struct student_s anotherStudent;
而是写下这个:
struct student_s { char* name; int age; double height; struct student_s* next; } ; // defines the structure struct student_s student; // declares variable struct student_s anotherStudent; // declares variable
这两种方法实际上是等价的,但是使用第二种方法,您可以清楚地将struct student_s
的定义与两个变量student
和anotherStudent
的声明anotherStudent
。 这更具可读性。
全局变量是在任何函数外部声明的变量,可以由变量声明后定义的任何函数使用:
void f1 () { } // Cannot access a int a ; void f2 () { } // Can get and set the value of a void f3 () { } // Can also get and set the value of a int main () { // Same as f1 and f2 f2 () ; f3 () ; printf("%d\n", a) ; // What is the output? }
大多数情况下,你不想使用全局变量,因为在调用各种函数时你并不真正知道它们会发生什么(参见上面的例子,你不知道a的值是否已被f2
修改或f3
)。
局部变量只能在声明它的“范围”中使用:
void f1 () { int a ; } int main () { // a is not accessible here }
在您的情况下,您声明了两个全局变量,但是您没有全局使用它们(因为您在本地声明了具有相同名称的变量,请参见下文),因此您应该在main中本地声明它们:
struct student_s { char* name; int age; double height; struct student_s* next; } ; // Not student here // No declaration here void printOneStudent(struct student_s student) { /* ... */ } void printStudents(const struct student_s* student) { /* ... */ } int main (void) { /* Declare student and anotherStudent here */ struct student_s student, anotherStudent ; /* ... */ }
请注意,当你这样做时:
struct student_s { char* name; int age; double height; struct student_s* next; } student ;
您声明了类型struct student_s
并且同时声明了变量student
,而如果您最后删除了student
,则仍然会声明struct student_s
类型而不是student
变量。
在您的代码中,您正在执行以下操作:
struct student_s { /* ... */ } student ; // student is a global variable void printOneStudent (struct student_s student) { // Here student correspond to the argument, not to the global variable } // Example: int a = 8 ; void f (int a) { printf("%d\n", a) ; } int main (void) { f(5) ; }
输出将是5
而不是8
,因为在f
,名称a
对应于函数的参数,而不是全局变量。
没有多少。 只需在main中声明它们即可。 没有技巧。
// ConsCPP.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include #include struct student_s { char* name; int age; double height; struct student_s* next; }; void printOneStudent(struct student_s student) { printf("%s (%d) %s %s %.2lf %s\n", student.name, student.age, ",", "height", student.height, " m"); } void printStudents(const struct student_s* student) { while (student != NULL) { printOneStudent(*student); student = student->next; } } int main(void) { struct student_s student; struct student_s anotherStudent; student.name = "Agnes McGurkinshaw"; student.age = 97; student.height = 1.64; student.next = &anotherStudent; anotherStudent.name = "Jingwu Xiao"; anotherStudent.age = 21; anotherStudent.height = 1.83; anotherStudent.next = NULL; printStudents(&student); return EXIT_SUCCESS; }
在文件的顶部,你有一个struct student_s {...} student;
。
这既定义了一个结构,又分配了一个变量,即student
。
下一行, struct student_s anotherStudent;
分配它的另一个变量。
因为它们未在函数内声明,所以它们是全局的。 要使它们成为本地的,必须在函数中声明它们,例如:
int main() { int i;
它定义了一个局部整数变量i
。
我不会提供代码; 这是你的功课,但我希望我给你足够的澄清。
使用数组:
#include #include #define NUM_OF_STUDENTS 2 struct student_s { char* name; int age; double height; struct student_s* next; }; void printOneStudent(struct student_s student) { printf("%s (%d) %s %s %.2lf %s\n", student.name, student.age, ",", "height", student.height, " m"); } void printStudents(const struct student_s* student) { int i; for (i = 0; i < NUM_OF_STUDENTS; i++) { printOneStudent(student[i]); } } int main(void) { struct student_s student[NUM_OF_STUDENTS]; student[0].name = "Agnes McGurkinshaw"; student[0].age = 97; student[0].height = 1.64; student[1].name = "Jingwu Xiao"; student[1].age = 21; student[1].height = 1.83; printStudents(student); return EXIT_SUCCESS; }