Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

dereference a pointer

Tags:

c

pointers

How can I dereference the pointer as i pack the structure in fill function and pass the pointer to send how to dereference it? as i get segmentation fault in what i have done

#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);
}
like image 927
pradeep Avatar asked Mar 02 '26 03:03

pradeep


1 Answers

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

send(create) will send the pointer, which is what you need.

When your function declaration's arguments contain an asterisk (*), a pointer to something is needed. When you then pass that argument on to another function requiring another pointer, you need to pass the name of the argument, since it is already a pointer.

When you used the asterisk, you dereferenced the pointer. That actually sent the "cell of memory that create points to," the actual struct and not a pointer.

like image 142
Evan Mulawski Avatar answered Mar 04 '26 20:03

Evan Mulawski