Dereference pointer

How can I dereference a pointer when I put together a structure in a fill function and pass a pointer to send a dereferencing method? as i get a segmentation error in what i did

#include<stdio.h>
struct xxx
{
    int x;
    int y;
};

void fill(struct xxx *create)
{
    create->x = 10;
    create->y = 20;
    send(*create);
}


main()
{
    struct xxx create;
    fill(&create);
}

send(struct xxx *ptr)
{
    printf("%d\n",ptr->x);
    printf("%d\n", ptr->y);
}
+3
source share
3 answers

send(*create) will send the actual struct object, not the pointer.

send(create) will send the pointer you need.

When function declaration arguments contain an asterisk (*), a pointer to something is needed. When you pass this argument to another function that requires a different pointer, you need to pass the name of the argument, since it is already a pointer.

, . " , create", , .

+10

send(*create);

send(create);

, *

+2

, ( !). - . . , GCC

gcc -Wall yourcode.c

yourcode.c: In functionfill’:
yourcode.c: 11:5: warning: implicit declaration of functionsendyourcode.c: At top level:
yourcode.c:15:5: warning: return type defaults tointyourcode.c:22:5: warning: return type defaults tointyourcode.c: In functionsend’:
yourcode.c:26:5: warning: control reaches end of non-void function
yourcode.c: In functionmain’:
yourcode.c:19:5: warning: control reaches end of non-void function

, send . send, (, -, void, ). main int

return 0;

.

yourcode.c: In functionfill’:
yourcode.c:12:5: error: incompatible type for argument 1 ofsendyourcode.c.c:7:6: note: expectedstruct xxx *’ but argument is of typestruct xxx

and you will notice that you have one redundant * in

send(*create);

which casts your pointer. Note. You do NOT want to dereference your pointer, because you need to forward the pointer to send, not the value. Change the line to

send(create);

et Voilà.

+1
source

All Articles