开发者

Passing a pointer to a structure

开发者 https://www.devze.com 2023-03-06 21:24 出处:网络
Hi I have a function A ( xy * abc) that takes a pointer to a structure. typedef struct { int a; char * b;

Hi I have a function A ( xy * abc) that takes a pointer to a structure.

typedef struct
{
int a;
char * b;
} xy;

typedef struct
{
xy c;
xy d;
} uv;

uv *sha;

If i need to call the function A for c and d usin开发者_运维问答g uv how should I pass the argument? I am calling function A by using this:

A (&sha->c);
A (&sha->d);

Is this call correct?

Kindly help me


If uv is a struct, and not a pointer to struct, you need to do A(&uv.c), but in your case, uv is a struct type, not an actual struct, you need to have a variable of type uv:

uv somevar;
A(&somevar.c);


Create a variable of type uv then pass it to the function:

uv var; 
A(&(var.c)); 

A(xy*) --> takes addres of an object of type xy
var.c --> returns object of type xy
&var.c --> returns address of returned xy object

uv *sha;
A(&(sha->c));
A(&(sha->d));

A(xy*) --> takes addres of an object of type xy
sha->c --> returns object of type xy
&(sha->c) --> returns address of returned xy object


Although it seems correct, but I will do it like this:

A (&(sha->c));
A (&(sha->d));

Note the additional parantheses; these are there to add more verbosity although compiler probably won't need those.

0

精彩评论

暂无评论...
验证码 换一张
取 消