首页 新闻 搜索 专区 学院

difference between an object and pointer variable

0
悬赏园豆:5 [待解决问题]

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?

blogger2020的主页 blogger2020 | 菜鸟二级 | 园豆:204
提问于:2021-07-05 13:17
< >
分享
所有回答(1)
0

The key difference is that one is a variable with type node_t, and the other is a pointer which points to a anonymous variable with type node_t (which you created by malloc() )

从今往后^-^ | 园豆:206 (菜鸟二级) | 2021-07-05 23:15

Thanks for the answer, but this difference is known whenever the variable is declared.
As long as the variable is declared, like:
node_t F;
or
node_t *p;
we would/need understand the differences and its usages like your answer suggested.

My question is more about its usage. For example: when we have a F declared, any function calls that involve F would be an object passing, and the other would be considered as a reference or pointer passing. Like below:
push(node_t F) {
my code is here;
the function parameter is an object of node_t. which may take more memory than its counterpart.
}

However, when we have below:
push(node_t *p) {
my code is here.
p is a reference or pointer of node_t type, which may take less memory than the object.
}

push(node_t **p) {
my code is here.
when changing the header pointer in the linked list, this type of the parameter would be useful.
}

So, my question remains.
But, thanks for the answer anyway.

支持(0) 反对(0) blogger2020 | 园豆:204 (菜鸟二级) | 2021-07-06 11:45
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册