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

时间:2015-08-07 09:24:26

标签: c struct global local

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

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

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

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

6 个答案:

答案 0 :(得分:4)

好吧,首先替换它:

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

答案 1 :(得分:2)

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

@IBOutlet weak var button0: UIButton!
@IBOutlet weak var button1: UIButton!
@IBOutlet weak var button2: UIButton!
@IBOutlet weak var button3: UIButton!
@IBOutlet weak var button4: UIButton!
@IBOutlet weak var button5: UIButton!
@IBOutlet weak var button6: UIButton!
@IBOutlet weak var button7: UIButton!
@IBOutlet weak var button8: UIButton!
var buttons = [[UIButton]]()

override func viewDidLoad() {

    buttons = [
        [button0,button1,button2],
        [button3,button4,button5],
        [button6,button7,button8]
    ]

}

而是写下这个:

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_sstudent的声明分开。这更具可读性。

答案 2 :(得分:1)

全局变量是一个变量,它在任何函数之外声明,并且可以由变量声明后定义的任何函数使用:

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已被f2f3修改。

局部变量只能用于&#34;范围&#34;声明的地方:

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对应于函数的参数,而不是全局变量。

答案 3 :(得分:0)

没有多少。只需在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;
}

答案 4 :(得分:-2)

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

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

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

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

int main()
{
    int i;

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

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

答案 5 :(得分:-3)

使用数组:

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