[英]Struct, pointers, getting the field value by the pointer
I have something similar to this code in my application: 我的应用程序中有类似以下代码的内容:
#include <stdlib.h>
#include <stdio.h>
typedef struct STH
{
double *buff;
int size;
}STH;
void fun1(STH *s)
{
fun3(s->buff, s->size);
}
void fun3(double *buff, int n)
{
int i = 0;
printf("N = %d\n", n);
for(i=0; i<n; i++)
printf("%d\n", buff[i]);
}
void fun2()
{
STH s;
s.size = 4;
s.buff = malloc(sizeof(double) * s.size);
fun1(&s);
}
int main()
{
fun2();
return 0;
}
When I try to print out the buffer in fun3
, gdb
says that there is an error in fun3
. 当我尝试在打印出缓冲
fun3
, gdb
说有一个错误fun3
。 When I try to print out (in fun2
) the value of the s->size
, there is an error (instead of 4 it prints out strange numbers ....) 当我尝试打印(在
fun2
) s->size
的值时,出现错误(而不是4,它打印出了奇怪的数字....)
buff[i] is a floating point number, use %f format specifier to print it. buff [i]是浮点数,请使用%f格式说明符进行打印。 Ie use:
即使用:
printf("%f\n", buff[i]);
instead of 代替
printf("%d\n", buff[i]);
Also you have not initialized your array to some values. 另外,您还没有将数组初始化为某些值。 It will contain (and print) random values.
它将包含(并打印)随机值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.