difference between an object and pointer variable

I have following code:

include <assert.h>

include <errno.h>

include <pthread.h>

include <stdio.h>

include <unistd.h>

include "linkedlist.h"

typedef struct node
{
pthread_mutex_t m;
int data;
struct node *next;
} node_t;

void init_node(node_t *n)
{
pthread_mutex_init(&(n->m),NULL); //NULL为默认的互斥锁
n->data = 6;
n->next = NULL;
};

int main()
{
node_t F; //F is a node_t type of object, but not a pointer variable.
init_node(&F);

node_t *p; //p is a node_t type pointer.
p = malloc(sizeof(node_t));
init_node(p);

return 0;
}

Apart from F is a node_t type of object variable and p is a same type of pointer variable, which F might take more memory space than the pointer variable, from linked list usage(inserting, searching, deleting, etc) point of view, what else the differences are there between above 2 variables?

回答

(My English is bad, so perhaps there are some grammar mistakes, and please forgive me.)
F is a normal variable and p is a pointer, so you must use malloc to allocate some memory for variable p.
If you want to give the variable as a argument of a function:
void f1(node_t p){
//we use “p.data” to get data
}
//we can use: f1(F);f1(*p); to call the functions.
void f2(node_t p){
//we use “p->data” or “(
p).data” to get data
}
//we can use: f2(&F);f2(p); to call the functions.
Because of the method of C to pass arguments, if we need to change values of F or p, we must use pointers.
Function f2 can use *F or *p to change data in the variable.

以上是difference between an object and pointer variable的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>