q*****z 发帖数: 191 | 1 Can anyone tell me what is wrong with this little C program? I am learning it
how to pass structure pointer back and forth in functions. This program compil
es ok but when excuting it, it cannot give me the right result. Thanks in adva
nce.
Here is the program:
#include
#include
struct number{
double r;
double c;
};
struct number *addtwocom(struct number *a, struct number *b)
{
struct number *c;
c->r = a->r+b->r;
c->c = a->c+b->c;
re |
t*s 发帖数: 1504 | 2 your variable c is just a pointer
you need to use malloc(sizeof(number)) to allocate space first
【在 q*****z 的大作中提到】 : Can anyone tell me what is wrong with this little C program? I am learning it : how to pass structure pointer back and forth in functions. This program compil : es ok but when excuting it, it cannot give me the right result. Thanks in adva : nce. : Here is the program: : #include : #include : struct number{ : double r; : double c;
|
f***h 发帖数: 52 | 3 yes
warning C4700: local variable 'c' used without having been
initialized
warning C4700: local variable 'a' used without having been
initialized
warning C4700: local variable 'b' used without having been
initialized
add this line after each declaration.
a=(struct number *)malloc(sizeof(struct number));
【在 t*s 的大作中提到】 : your variable c is just a pointer : you need to use malloc(sizeof(number)) to allocate space first
|