Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/visual-studio-code/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 在c+中的结构中显示垃圾值+;_C++_Visual Studio Code_Scope_G++_Global Namespace - Fatal编程技术网

C++ 在c+中的结构中显示垃圾值+;

C++ 在c+中的结构中显示垃圾值+;,c++,visual-studio-code,scope,g++,global-namespace,C++,Visual Studio Code,Scope,G++,Global Namespace,在Visual Studio代码上编译(在linux操作系统上) 我应该怎么做才能获得正确的输出。因为您使用的是未初始化的结构体: Displaying Information : Name : Roll : 21939 Marks : 2.39768e-36 它隐藏全局s 相反,在main中初始化它: struct student s; 因为您正在使用此未初始化的结构: Displaying Information : Name : Roll : 21939 Marks

在Visual Studio代码上编译(在linux操作系统上)
我应该怎么做才能获得正确的输出。

因为您使用的是未初始化的
结构体:

Displaying Information : 
Name  : 
Roll  : 21939
Marks : 2.39768e-36
它隐藏全局
s

相反,在
main
中初始化它:

struct student s; 

因为您正在使用此未初始化的
结构

Displaying Information : 
Name  : 
Roll  : 21939
Marks : 2.39768e-36
它隐藏全局
s

相反,在
main
中初始化它:

struct student s; 

您声明的两个类型为
student
的对象

第一个在全局命名空间中声明

student s = {"Karthik",1,95.3};
和初始化,第二个在函数main的块范围内

struct student
{
  char name [50];
  int roll;
  float marks;
}s = {"Karthik",1,95.3};
此外,这还没有初始化

在块范围中声明的对象隐藏在全局命名空间中声明的同名对象

删除本地声明或使用限定名指定在全局命名空间中声明的对象,例如

struct student s;

cout您声明的两个类型为
student
的对象

第一个在全局命名空间中声明

student s = {"Karthik",1,95.3};
和初始化,第二个在函数main的块范围内

struct student
{
  char name [50];
  int roll;
  float marks;
}s = {"Karthik",1,95.3};
此外,这还没有初始化

在块范围中声明的对象隐藏在全局命名空间中声明的同名对象

删除本地声明或使用限定名指定在全局命名空间中声明的对象,例如

struct student s;

coutObligatory提醒,
char name[50]不应作为C++中的字符串使用。我们有
std::string
s。必须提醒
char name[50]不应作为C++中的字符串使用。我们有
std::string
s。