我是 C 语言的新手,我的代码有一个奇怪的问题。
我正在尝试创建一个 struct
数组作为另一个 struct
数组的一部分。
当 printf
函数的数量不同时,我得到不同的输出。我有两种情况,一种是正确的,另一种是不正确的。
我不明白为什么一个额外的 printf
的简单调用会改变结果。
这是我的代码,结果不正确,这里我得到“x velocity=-nan”
#include<stdlib.h>
#include<stdio.h>
struct vect3d1
{
double *x,*y,*z;
};
struct block
{
int ibl;
struct vect3d1 *velocity;
};
void create_time(struct block *blocks,int Nsteps,int nb);
int main()
{
struct block *blocks;
int nb,i,t,Nsteps;
Nsteps=30;
nb=3;
blocks=calloc(nb, sizeof(struct block));
for (i=0;i<nb;i++){
for(t=0;t<Nsteps;t++){
blocks[i].velocity=(struct vect3d1 *)malloc(Nsteps*sizeof(struct vect3d1));
blocks[i].velocity[t].x=NULL;
blocks[i].velocity[t].y=NULL;
blocks[i].velocity[t].z=NULL;
}
}
create_time(blocks,Nsteps,nb);
free(blocks);
}
void create_time(struct block *blocks,int Nsteps,int nb){
int i,t;
double u;
for (i=0;i<nb;i++){
for(t=0;t<Nsteps;t++){
u=0.5+t;
blocks[i].velocity[t].x=&u;
// printf("u %lf \n",u);
printf("velocity x=%lf \n",blocks[i].velocity[t].x);
}
}
}
你可以注意到函数 create_time
中的一行被注释了,当它没有注释时结果是正确的。
澄清一下,如果函数 create_time
是:
void create_time(struct block *blocks,int Nsteps,int nb){
int i,t;
double u;
for (i=0;i<nb;i++){
for(t=0;t<Nsteps;t++){
u=0.5+t;
blocks[i].velocity[t].x=&u;
// printf("u %lf \n",u);
printf("velocity x=%lf \n",blocks[i].velocity[t].x);
}
}
}
我得到:
"velocity x=-nan"
当函数为:
void create_time(struct block *blocks,int Nsteps,int nb){
int i,t;
double u;
for (i=0;i<nb;i++){
for(t=0;t<Nsteps;t++){
u=0.5+t;
blocks[i].velocity[t].x=&u;
printf("u %lf \n",u);
printf("velocity x=%lf \n",blocks[i].velocity[t].x);
}
}
}
我得到:
"u 0.5"
"velocity x=0.5"
...
等等。
我添加这行只是为了验证变量 u
,然后我意识到添加它会更改 printf
的输出。
发生了什么?为什么 printf
的输出改变了?
最佳答案
这里
printf("velocity x=%lf \n",blocks[i].velocity[t].x);
您传递的不是double
,而是指向double
的指针,尽管double
是预期的。这会调用臭名昭著的未定义行为。不要这样做。
另外我想知道为什么编译器没有警告你这个。您可能想提高编译器的衰减级别。对于 GCC,在编译时添加选项 -Wall -Wextra -pedantic
。
要解决此问题,请将其更改为
printf("velocity x=%lf \n", *blocks[i].velocity[t].x);
所以回答你的问题:
What is happen? Why the output of printf is changed?
欢迎来到未定义行为的神秘世界。 ;)
除此之外,请注意这一行
blocks[i].velocity[t].x=&u;
是危险的,因为您将变量 local 的地址分配给函数到一个指针,该指针很可能会在函数离开后使用。
一旦函数离开,它不再指向有效内存。
然后取消引用它会调用未定义的行为,小心。
作为最后的友情提示:帮您自己和您的编码员同事一个忙,他们必须阅读您的代码并正确地缩进代码。这是免费调试。我有一个强烈的印象 this bug由于代码的缩进困惑,如图所示。
关于c - 不同次数的 printf 调用的不同输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54291531/