简体   繁体   English

为什么这个 C 程序的输出是 0?

[英]Why is the output of this C program 0?

In this code I am trying to pass a pointer to the structure and then use this pointer to work on the structure members, but why is the output always showing 0?在这段代码中,我试图传递一个指向结构的指针,然后使用这个指针来处理结构成员,但为什么输出总是显示 0?

#include<stdio.h>
#include<string.h> 
struct student
{
    int go;
} s;
void m();
void main()
{
    m(&s);  
    printf("%d",s.go);
}

void m(struct student *ptr)  
{
    ptr->go;
}
struct student
{
   int go;
}s;

creates a global variable.创建一个全局变量。 Global variables are zero initialized unless they are initialized explicitly.除非显式初始化全局变量,否则它们都是零初始化的。 That statement is equivalent to:该语句等效于:

struct student
{
   int go;
};

struct student s = {0};

Hence the value of s.go is 0 .因此s.go值为0 The call to m does not change the values of any variables.m的调用不会改变任何变量的值。 It has no effect on the output.它对输出没有影响。 The output will be 0 even if the call to m(&s) is removed.即使删除了对m(&s)的调用,输出也将为0

The global variable s is initialized with all members 0 .全局变量s被初始化为所有成员0

Nothing changes the value of s.go , so the output is 0 . s.go的值没有任何变化,因此输出为0

  • When you declare your structure as global , their members are always initialize with its default value, if int than '0' and if char or string than '\\0' .当您将结构声明为global 时,它们的成员总是以其默认值初始化,如果int大于'0'并且如果char 或 string大于'\\0' So you are getting Value 0 .所以你得到的是 Value 0
 struct student { int go; } s;

Your question has all sorts of errors, here they are, in no particular order:你的问题有各种各样的错误,它们在这里,没有特定的顺序:

  • you don't need #include <string.h> if you aren't using any "string" functions如果您不使用任何“字符串”函数,则不需要#include <string.h>
  • you shouldn't use global variables (if you did, it would eliminate the need to pass the pointer to s to functions to access the struct);你不应该使用全局变量(如果你这样做了,它将消除将指向s的指针传递给函数来访问结构的需要); you should remove the global variable (ie, make it local) and continue to pass the pointer-to-struct as you are to m() in order to be able to access it outside of main() (where you should declare it)您应该删除全局变量(即,使其成为本地变量)并继续将指向结构的指针传递给m()以便能够在main()之外访问它(您应该在那里声明它)
  • your signature for main() is incorrect as I pointed out in a comment to OP, and naturally you are missing the return 0;正如我在对 OP 的评论中指出的那样,您对main()签名不正确,自然而然您错过了return 0; statement in main() because of this main()语句因此
  • you are missing a newline in your printf()您在printf()中缺少换行符
  • you aren't actually doing anything with ptr->go in m() ;你实际上并没有对ptr->go in m()做任何事情; you aren't assigning anything to it or otherwise using it.您没有为其分配任何内容或以其他方式使用它。 It is printing zero because, as others have pointed out, global variables (because they are static ,) are (default-)initialized它打印零,因为正如其他人指出的那样,全局变量(因为它们是static ,)是(默认)初始化的

Here is an example with corrections (note you can initialize s as described by others if you wish to use it's value before you modify/set it):这是一个带有更正的示例(请注意,如果您希望在修改/设置之前使用它的值,则可以按照其他人的描述初始化s ):

#include <stdio.h>

struct student
{
        int go;
};

void m();

int main(void)
{
        struct student s;

        m(&s);
        printf("%d\n", s.go);

        return 0;
}

void m(struct student *ptr)  
{
        ptr->go = 5;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM