C++ 在c+中的结构中显示垃圾值+;
在Visual Studio代码上编译(在linux操作系统上)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
我应该怎么做才能获得正确的输出。因为您使用的是未初始化的
结构体:
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。