C 按引用传递,但参数的值为null
我正在用C语言编程 我有一个结构:C 按引用传递,但参数的值为null,c,function,struct,parameter-passing,pass-by-reference,C,Function,Struct,Parameter Passing,Pass By Reference,我正在用C语言编程 我有一个结构: struct MY_TYPE { boolean flag; short int value; double stuff; }; 我有一个函数,它将指向MY_TYPE指针的指针作为参数: getData(struct MY_TYPE ** m_type) { // I initialise an object of MY_TYPE struct MY_TYPE a = {.value = 123};
struct MY_TYPE {
boolean flag;
short int value;
double stuff;
};
我有一个函数,它将指向MY_TYPE
指针的指针作为参数:
getData(struct MY_TYPE ** m_type) {
// I initialise an object of MY_TYPE
struct MY_TYPE a = {.value = 123};
// I assign the address of above object to a pointer of MY_TYPE
struct MY_TYPE *p = &a;
// I assign the address of above pointer to parameter
m_type = &p;
}
在我的主程序中,我调用上述函数:
struct MY_TYPE *my_param;
getData(&my_param);
// I set a break pointer here, and it shows me my_param is NULL, why?
getData(struct MY_TYPE ** m_type) {
// I initialize an object of MY_TYPE
struct MY_TYPE a = {.value = 123};
// I make a copy into dynamic memory
struct MY_TYPE *copy = malloc(sizeof(struct MY_TYPE));
memcpy(copy, &a);
// I assign the address of above pointer to parameter
*m_type = copy;
}
调用getData(…)后,我传入的参数为NULL,为什么?您使用的是局部变量,此变量的生存期以函数结束,请使用
malloc
以保留其值
struct MY_TYPE *p = malloc(sizeof *p);
if (p != NULL) {
p->value = 123;
*m_type = p; /* Dereference the passed pointer to pointer */
}
您正在使用一个局部变量,该变量的生存期以函数结束,请使用
malloc
以保留其值
struct MY_TYPE *p = malloc(sizeof *p);
if (p != NULL) {
p->value = 123;
*m_type = p; /* Dereference the passed pointer to pointer */
}
这是一种可能未定义的行为,不会发生,因为您正在分配一个按值传递的指针
- 调用方将忽略对
内部getData
所做的任何更改。您需要分配m_type
,以使更改产生任何差异*m_type
- 有了这个更改,您将开始获得未定义的行为,因为只要
返回,getData
就会超出范围struct a
struct MY_TYPE *my_param;
getData(&my_param);
// I set a break pointer here, and it shows me my_param is NULL, why?
getData(struct MY_TYPE ** m_type) {
// I initialize an object of MY_TYPE
struct MY_TYPE a = {.value = 123};
// I make a copy into dynamic memory
struct MY_TYPE *copy = malloc(sizeof(struct MY_TYPE));
memcpy(copy, &a);
// I assign the address of above pointer to parameter
*m_type = copy;
}
呼叫方需要释放从呼叫中接收的内存:
struct MY_TYPE *my_param;
getData(&my_param);
... // Use my_param here.
// Now that I am done with my_param...
free(my_param);
这是一种可能未定义的行为,不会发生,因为您正在分配一个按值传递的指针
- 调用方将忽略对
内部getData
所做的任何更改。您需要分配m_type
,以使更改产生任何差异*m_type
- 有了这个更改,您将开始获得未定义的行为,因为只要
返回,getData
就会超出范围struct a
struct MY_TYPE *my_param;
getData(&my_param);
// I set a break pointer here, and it shows me my_param is NULL, why?
getData(struct MY_TYPE ** m_type) {
// I initialize an object of MY_TYPE
struct MY_TYPE a = {.value = 123};
// I make a copy into dynamic memory
struct MY_TYPE *copy = malloc(sizeof(struct MY_TYPE));
memcpy(copy, &a);
// I assign the address of above pointer to parameter
*m_type = copy;
}
呼叫方需要释放从呼叫中接收的内存:
struct MY_TYPE *my_param;
getData(&my_param);
... // Use my_param here.
// Now that I am done with my_param...
free(my_param);
您是否了解局部作用域变量…..无论如何,变量“a”是getData()函数的局部变量,因此您不能返回它(或者指向它的指针,或者更糟糕的是指向同时也是局部变量的指针)。您是否了解局部作用域变量…..无论如何,变量“a”是getData()函数的局部变量,因此您不能返回它(或者指向它的指针,或者更糟糕的是指向也是局部变量的指针的指针)。