将变量从全局变为本地 - C

时间:2022-06-16 16:49:56

I have written the following code for a project however it fails a singular test asking for two variables to not be global, and instead be local to main(). Modify structexample1.c so that the variables student and anotherStudent are not global but are local to main. I vaguely understand the local and global concept but I am unsure how to implement what the question is asking into the code I have written.

我为一个项目编写了以下代码,但它没有通过单一测试,要求两个变量不是全局的,而是在main()的本地。修改structexample1.c,使变量student和anotherStudent不是全局变量,而是main的本地变量。我模糊地理解了本地和全球的概念,但我不确定如何在我编写的代码中实现问题。

#include <stdio.h>
#include <stdlib.h>
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;
}

I know I will need to define these variables inside main() but I am unsure how to implement them in a way that does not entirely break my code. The output of the code should remain as follows:

我知道我需要在main()中定义这些变量,但我不确定如何以不完全破坏我的代码的方式实现它们。代码的输出应保持如下:

Agnes McGurkinshaw (97), height 1.64 m
Jingwu Xiao (21), height 1.83 m

6 个解决方案

#1


4  

Well, first replace this:

好吧,首先替换这个:

struct student_s {
    char* name;
    int age;
    double height;
    struct student_s* next;   
} student;

With:

附:

struct student_s {
    char* name;
    int age;
    double height;
    struct student_s* next;   
};

(That is, remove student in the last line).

(也就是说,在最后一行删除学生)。

This change is necessary because you want to define the structure type so that you can later define variables of type struct student_s, but you don't want to define the student variable here, as that would make it global.

此更改是必要的,因为您要定义结构类型,以便稍后可以定义struct student_s类型的变量,但您不希望在此处定义学生变量,因为这会使其成为全局变量。

Then remove this line:

然后删除此行:

struct student_s anotherStudent;

And finally, declare both variables in main() before using them:

最后,在使用它们之前在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;
}

#2


2  

Not directly related to your question, but anyway you should avoid this:

与您的问题没有直接关系,但无论如何您应该避免这种情况:

struct student_s {
    char* name;
    int age;
    double height;
    struct student_s* next;   
} student;

struct student_s anotherStudent;

But rather write this:

而是写下这个:

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

Both methods are actually equivalent, but with the second method you clearly separate the definition of struct student_s from the declaration of the two variables studentand anotherStudent. This is more readable.

这两种方法实际上是等价的,但是使用第二种方法,您可以清楚地将struct student_s的定义与两个变量student和anotherStudent的声明分开。这更具可读性。

#3


1  

A global variable is a variable that you declare outside any functions and which can be used by any functions defined after the variable declaration:

全局变量是在任何函数外部声明的变量,可以由变量声明后定义的任何函数使用:

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?
}

Most of the time, you don't want to use global variable because you do not really know what happens to them when calling various functions (see the example above where you don't know if the value of a has been modified by f2 or f3).

大多数情况下,你不想使用全局变量,因为在调用各种函数时你并不真正知道它们会发生什么(参见上面的例子,你不知道a的值是否已被f2修改或F3)。

A local variable can only be used in the "scope" where it has been declared:

局部变量只能在声明它的“范围”中使用:

void f1 () {
    int a ;
}

int main () {
    // a is not accessible here
}

In your case, you are declaring two global variables, but you do not use them globally (because you declare variable with the same name locally, see below), so you should simply declare them locally inside your main:

在您的情况下,您声明了两个全局变量,但是您没有全局使用它们(因为您在本地声明了具有相同名称的变量,请参见下文),因此您应该在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 ;
    /* ... */
}

Note that when you did:

请注意,当你这样做时:

struct student_s {
    char* name;
    int age;
    double height;
    struct student_s* next;   
} student ;

You were declarting type struct student_s and in the same time the variable student, while if you remove the student at the end, you are still declaring the struct student_s type but not the student variable any more.

您声明了类型struct student_s并且同时声明了变量学生,而如果您最后删除了学生,则仍然会声明结构student_s类型而不是学生变量。

In your code, you were doing the following:

在您的代码中,您正在执行以下操作:

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) ;
}

The output will be 5, not 8, because in f the name a correspond to the parameter of the function, not to the global variable.

输出将是5而不是8,因为在f中,名称a对应于函数的参数,而不是全局变量。

#4


0  

There is not much to it. Just declare them in main. There are no tricks.

没有多少。只需在main中声明它们即可。没有技巧。

// ConsCPP.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

#include <stdio.h>
#include <stdlib.h>
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;
}

#5


-2  

At the top of the file you have a struct student_s {...} student;.

在文件的顶部,你有一个struct student_s {...} student;。

This both defines a structure, and allocates one variable of it, student.

这既定义了一个结构,又分配了一个变量,即student。

The next line, struct student_s anotherStudent; allocates another variable of it.

下一行,struct student_s anotherStudent;分配它的另一个变量。

Because they are not declared inside a function, they are global. To make them local, they must be declared in a function, for example:

因为它们未在函数内声明,所以它们是全局的。要使它们成为本地的,必须在函数中声明它们,例如:

int main()
{
    int i;

which defines a local integer variable i.

它定义了一个局部整数变量i。

I will not provide the code; it is your homework, but I hope I gave enough clarification for you.

我不会提供代码;这是你的功课,但我希望我给你足够的澄清。

#6


-3  

Using array:

使用数组:

#include <stdio.h>
#include <stdlib.h>

#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;
}

#1


4  

Well, first replace this:

好吧,首先替换这个:

struct student_s {
    char* name;
    int age;
    double height;
    struct student_s* next;   
} student;

With:

附:

struct student_s {
    char* name;
    int age;
    double height;
    struct student_s* next;   
};

(That is, remove student in the last line).

(也就是说,在最后一行删除学生)。

This change is necessary because you want to define the structure type so that you can later define variables of type struct student_s, but you don't want to define the student variable here, as that would make it global.

此更改是必要的,因为您要定义结构类型,以便稍后可以定义struct student_s类型的变量,但您不希望在此处定义学生变量,因为这会使其成为全局变量。

Then remove this line:

然后删除此行:

struct student_s anotherStudent;

And finally, declare both variables in main() before using them:

最后,在使用它们之前在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;
}

#2


2  

Not directly related to your question, but anyway you should avoid this:

与您的问题没有直接关系,但无论如何您应该避免这种情况:

struct student_s {
    char* name;
    int age;
    double height;
    struct student_s* next;   
} student;

struct student_s anotherStudent;

But rather write this:

而是写下这个:

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

Both methods are actually equivalent, but with the second method you clearly separate the definition of struct student_s from the declaration of the two variables studentand anotherStudent. This is more readable.

这两种方法实际上是等价的,但是使用第二种方法,您可以清楚地将struct student_s的定义与两个变量student和anotherStudent的声明分开。这更具可读性。

#3


1  

A global variable is a variable that you declare outside any functions and which can be used by any functions defined after the variable declaration:

全局变量是在任何函数外部声明的变量,可以由变量声明后定义的任何函数使用:

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?
}

Most of the time, you don't want to use global variable because you do not really know what happens to them when calling various functions (see the example above where you don't know if the value of a has been modified by f2 or f3).

大多数情况下,你不想使用全局变量,因为在调用各种函数时你并不真正知道它们会发生什么(参见上面的例子,你不知道a的值是否已被f2修改或F3)。

A local variable can only be used in the "scope" where it has been declared:

局部变量只能在声明它的“范围”中使用:

void f1 () {
    int a ;
}

int main () {
    // a is not accessible here
}

In your case, you are declaring two global variables, but you do not use them globally (because you declare variable with the same name locally, see below), so you should simply declare them locally inside your main:

在您的情况下,您声明了两个全局变量,但是您没有全局使用它们(因为您在本地声明了具有相同名称的变量,请参见下文),因此您应该在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 ;
    /* ... */
}

Note that when you did:

请注意,当你这样做时:

struct student_s {
    char* name;
    int age;
    double height;
    struct student_s* next;   
} student ;

You were declarting type struct student_s and in the same time the variable student, while if you remove the student at the end, you are still declaring the struct student_s type but not the student variable any more.

您声明了类型struct student_s并且同时声明了变量学生,而如果您最后删除了学生,则仍然会声明结构student_s类型而不是学生变量。

In your code, you were doing the following:

在您的代码中,您正在执行以下操作:

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) ;
}

The output will be 5, not 8, because in f the name a correspond to the parameter of the function, not to the global variable.

输出将是5而不是8,因为在f中,名称a对应于函数的参数,而不是全局变量。

#4


0  

There is not much to it. Just declare them in main. There are no tricks.

没有多少。只需在main中声明它们即可。没有技巧。

// ConsCPP.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

#include <stdio.h>
#include <stdlib.h>
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;
}

#5


-2  

At the top of the file you have a struct student_s {...} student;.

在文件的顶部,你有一个struct student_s {...} student;。

This both defines a structure, and allocates one variable of it, student.

这既定义了一个结构,又分配了一个变量,即student。

The next line, struct student_s anotherStudent; allocates another variable of it.

下一行,struct student_s anotherStudent;分配它的另一个变量。

Because they are not declared inside a function, they are global. To make them local, they must be declared in a function, for example:

因为它们未在函数内声明,所以它们是全局的。要使它们成为本地的,必须在函数中声明它们,例如:

int main()
{
    int i;

which defines a local integer variable i.

它定义了一个局部整数变量i。

I will not provide the code; it is your homework, but I hope I gave enough clarification for you.

我不会提供代码;这是你的功课,但我希望我给你足够的澄清。

#6


-3  

Using array:

使用数组:

#include <stdio.h>
#include <stdlib.h>

#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;
}