x,t2.next->y,t2.next->z,t2.next->str,t2.next->str2,t2.next->str3); printf(“%d%d%d%s%s%s”,t1->x,t1->y,t1->z,t1->str,t1->str2,t1->str3); printf(“%d%d%d%s%s%s”,t1->next->x,t1->next->y,t1->next->z,t1->next->str,t1->next->str2,t1->next->str3); printf(“我活下来了!”); },c++,pointers,C++,Pointers" /> x,t2.next->y,t2.next->z,t2.next->str,t2.next->str2,t2.next->str3); printf(“%d%d%d%s%s%s”,t1->x,t1->y,t1->z,t1->str,t1->str2,t1->str3); printf(“%d%d%d%s%s%s”,t1->next->x,t1->next->y,t1->next->z,t1->next->str,t1->next->str2,t1->next->str3); printf(“我活下来了!”); },c++,pointers,C++,Pointers" />

通过C+中的指针设置/获取值+; 我试图学习指针如何在C++中工作,除了最后一个小时我才设法使它崩溃。 #include<string> // a sample structure typedef struct test_obj_type_s{ // with some numbers int x; float y; double z; std::string str; // a standard string... char * str2; // a variable-length string... char str3[2048]; // a fixed-length string... struct test_obj_type_s *next; // and a link to another similar structure } test_obj_type_t; // a sample pointer test_obj_type_t * last_test_obj; test_obj_type_t obj; // let's go int main(){ // let's assign some demo values obj.x = 12; obj.y = 15.15; obj.z = 25.1; obj.str = "test str is working"; obj.str2 = "test str2 is working"; strcpy_s(obj.str3, "test str3 is working"); // let's also assign some circular references obj.next = &obj; // now... last_test_obj = &obj; test_obj_type_t * t1 = last_test_obj; test_obj_type_t t2 = obj; // now let's have some fun; printf("%d %d %d %s %s %s", t2.x, t2.y, t2.z, t2.str, t2.str2, t2.str3); printf("%d %d %d %s %s %s", t2.next->x, t2.next->y, t2.next->z, t2.next->str, t2.next->str2, t2.next->str3); printf("%d %d %d %s %s %s", t1->x, t1->y, t1->z, t1->str, t1->str2, t1->str3); printf("%d %d %d %s %s %s", t1->next->x, t1->next->y, t1->next->z, t1->next->str, t1->next->str2, t1->next->str3); printf("I survived!"); } #包括 //样本结构 类型定义结构测试对象类型{ //有一些数字 int x; 浮动y; 双z; std::string str;//标准字符串。。。 char*str2;//长度可变的字符串。。。 char str3[2048];//一个固定长度的字符串。。。 struct test_obj_type_s*next;//以及到另一个类似结构的链接 }测试对象类型; //示例指针 测试对象类型*最后测试对象; 测试对象类型对象; //我们走吧 int main(){ //让我们分配一些演示值 obj.x=12; 目标y=15.15; 目标z=25.1; obj.str=“测试str正在工作”; obj.str2=“测试str2正在工作”; strcpy_s(obj.str3,“测试str3正在工作”); //让我们也分配一些循环引用 obj.next=&obj; //现在。。。 最后一次测试\u obj=&obj; 测试对象类型t*t1=最后一次测试对象; 测试对象类型t2=对象; //现在,让我们玩一玩; printf(“%d%d%d%s%s%s”,t2.x,t2.y,t2.z,t2.str,t2.str2,t2.str3); printf(“%d%d%d%s%s%s”,t2.next->x,t2.next->y,t2.next->z,t2.next->str,t2.next->str2,t2.next->str3); printf(“%d%d%d%s%s%s”,t1->x,t1->y,t1->z,t1->str,t1->str2,t1->str3); printf(“%d%d%d%s%s%s”,t1->next->x,t1->next->y,t1->next->z,t1->next->str,t1->next->str2,t1->next->str3); printf(“我活下来了!”); }

通过C+中的指针设置/获取值+; 我试图学习指针如何在C++中工作,除了最后一个小时我才设法使它崩溃。 #include<string> // a sample structure typedef struct test_obj_type_s{ // with some numbers int x; float y; double z; std::string str; // a standard string... char * str2; // a variable-length string... char str3[2048]; // a fixed-length string... struct test_obj_type_s *next; // and a link to another similar structure } test_obj_type_t; // a sample pointer test_obj_type_t * last_test_obj; test_obj_type_t obj; // let's go int main(){ // let's assign some demo values obj.x = 12; obj.y = 15.15; obj.z = 25.1; obj.str = "test str is working"; obj.str2 = "test str2 is working"; strcpy_s(obj.str3, "test str3 is working"); // let's also assign some circular references obj.next = &obj; // now... last_test_obj = &obj; test_obj_type_t * t1 = last_test_obj; test_obj_type_t t2 = obj; // now let's have some fun; printf("%d %d %d %s %s %s", t2.x, t2.y, t2.z, t2.str, t2.str2, t2.str3); printf("%d %d %d %s %s %s", t2.next->x, t2.next->y, t2.next->z, t2.next->str, t2.next->str2, t2.next->str3); printf("%d %d %d %s %s %s", t1->x, t1->y, t1->z, t1->str, t1->str2, t1->str3); printf("%d %d %d %s %s %s", t1->next->x, t1->next->y, t1->next->z, t1->next->str, t1->next->str2, t1->next->str3); printf("I survived!"); } #包括 //样本结构 类型定义结构测试对象类型{ //有一些数字 int x; 浮动y; 双z; std::string str;//标准字符串。。。 char*str2;//长度可变的字符串。。。 char str3[2048];//一个固定长度的字符串。。。 struct test_obj_type_s*next;//以及到另一个类似结构的链接 }测试对象类型; //示例指针 测试对象类型*最后测试对象; 测试对象类型对象; //我们走吧 int main(){ //让我们分配一些演示值 obj.x=12; 目标y=15.15; 目标z=25.1; obj.str=“测试str正在工作”; obj.str2=“测试str2正在工作”; strcpy_s(obj.str3,“测试str3正在工作”); //让我们也分配一些循环引用 obj.next=&obj; //现在。。。 最后一次测试\u obj=&obj; 测试对象类型t*t1=最后一次测试对象; 测试对象类型t2=对象; //现在,让我们玩一玩; printf(“%d%d%d%s%s%s”,t2.x,t2.y,t2.z,t2.str,t2.str2,t2.str3); printf(“%d%d%d%s%s%s”,t2.next->x,t2.next->y,t2.next->z,t2.next->str,t2.next->str2,t2.next->str3); printf(“%d%d%d%s%s%s”,t1->x,t1->y,t1->z,t1->str,t1->str2,t1->str3); printf(“%d%d%d%s%s%s”,t1->next->x,t1->next->y,t1->next->z,t1->next->str,t1->next->str2,t1->next->str3); printf(“我活下来了!”); },c++,pointers,C++,Pointers,我错过了什么? 或者我做错了什么?当程序将std::string传递给printf时,程序会显示未定义的行为%s格式说明符需要一个常量字符*作为参数。在您的printf语句系列中,无论什么地方都有something.str或something->str,请将其替换为something.str.c_str() 类似地,%d期望将int作为参数,但您传递的是double。要打印something.y和something.z,请改用%f说明符。当程序将std::string传递给printf时,会显示

我错过了什么?
或者我做错了什么?

当程序将
std::string
传递给
printf
时,程序会显示未定义的行为<代码>%s格式说明符需要一个
常量字符*
作为参数。在您的
printf
语句系列中,无论什么地方都有
something.str
something->str
,请将其替换为
something.str.c_str()


类似地,
%d
期望将
int
作为参数,但您传递的是
double
。要打印
something.y
something.z
,请改用
%f
说明符。

当程序将
std::string
传递给
printf
时,会显示未定义的行为<代码>%s格式说明符需要一个
常量字符*
作为参数。在您的
printf
语句系列中,无论什么地方都有
something.str
something->str
,请将其替换为
something.str.c_str()


类似地,
%d
期望将
int
作为参数,但您传递的是
double
。要打印<代码>某物。y < /> >和<代码>某物。z < /> >使用<代码> %f> /Cult>说明符。

您可以移动到C++流输出,这是做这些事情的更简单的方式:

// now let's have some fun;
std::cout<<t2.x<<t2.y<<t2.z<<t2.str<<t2.str2<<t2.str3<<std::endl;
std::cout<<t2.next->x<<t2.next->y<<t2.next->z<<t2.next->str<<t2.next->str2<<t2.next->str3<<std::endl;
std::cout<<t1->x<<t1->y<<t1->z<<t1->str<<t1->str2<<t1->str3<<std::endl;
std::cout<<t1->next->x<<t1->next->y<<t1->next->z<<t1->next->str<<t1->next->str2<<t1->next->str3<<std::endl;
//现在让我们玩一玩;

STD::CUT

你可以移动到C++流输出,这是做这些事情的更简单的方式:

// now let's have some fun;
std::cout<<t2.x<<t2.y<<t2.z<<t2.str<<t2.str2<<t2.str3<<std::endl;
std::cout<<t2.next->x<<t2.next->y<<t2.next->z<<t2.next->str<<t2.next->str2<<t2.next->str3<<std::endl;
std::cout<<t1->x<<t1->y<<t1->z<<t1->str<<t1->str2<<t1->str3<<std::endl;
std::cout<<t1->next->x<<t1->next->y<<t1->next->z<<t1->next->str<<t1->next->str2<<t1->next->str3<<std::endl;
//现在让我们玩一玩;

STD:为什么你在C++中使用<代码> Prtuff <代码> EdHeal,我应该使用IOSWATOR-- <代码> STD::CUT >我试图学习指针如何在C++中工作,除了最后一个小时我才设法使它崩溃。-并且它将在你的余下的职业中保持这样的状态。因为即使是资深软件开发人员也会创建指针错误。为什么你在C++中使用<代码> Prtff<代码> EdHeal,我应该使用IOSWATOR-- <代码> STD::CUT >我试图学习指针如何在C++中工作,除了最后一个小时我才设法使它崩溃。-并且它将在你的余下的职业中保持这样的状态。因为即使是经验丰富的高级软件开发人员也会创建指针错误。如果OP的答案中的
%d%d%s%s“
实际上来自用户定义的语言相关设置,会发生什么
std::cout
和friends不能总是弥补格式字符串的灵活性。Boo.Frand试图将格式字符串的灵活性与C++的类型安全结合起来。如果在OP的答案中,<>“%D%%D%S%S%s”<代码>实际上是来自用户定义的语言依赖设置,那么会发生什么?code>std::cout
和friends不能总是弥补格式字符串的灵活性。Boo.Frand试图将格式字符串的灵活性与C++的类型安全结合起来。