在C中取消对结构字段的引用
我有: 因此,我有一个方法的参数,如下所示:在C中取消对结构字段的引用,c,data-structures,struct,dereference,C,Data Structures,Struct,Dereference,我有: 因此,我有一个方法的参数,如下所示: typedef struct table { int size; } Table; 但当我这样做的时候: Table **table 或: 它不起作用,我的标志给了我一个错误:在非结构或联合中请求成员“大小” 请帮忙 解引用运算符(*)的优先级较低,因此: *table->size = 5; 评估结果如下: *table->size 由于table是指向指针的指针,指针不能有成员,因此会出现错误。您需要的是: *(table-
typedef struct table {
int size;
} Table;
但当我这样做的时候:
Table **table
或:
它不起作用,我的标志给了我一个错误:在非结构或联合中请求成员“大小”
请帮忙 解引用运算符(*
)的优先级较低,因此:
*table->size = 5;
评估结果如下:
*table->size
由于table
是指向指针的指针,指针不能有成员,因此会出现错误。您需要的是:
*(table->size)
现在,首先计算*表
,生成指向表
的指针<代码>->然后可以应用于它
无关的:
我注意到,您对结构名及其typedef使用了不同的标识符。这是没有必要的。您只需使用:
(*table)->size = 5;
为了避免所有这些奇怪的间接操作,更容易使用局部变量并执行以下操作:
typedef struct Table {
int size;
} Table;
但正如其他人所指出的,(*table)->size=5
之类的东西也会做同样的事情
如果需要修改指向的内容,则:
void myfunc(Table ** my_table) {
Table * ptable = *my_table;
ptable->size = 5;
/* etc */
}
下面是后者的一个例子:
void myfunc(Table ** my_table) {
Table * ptable = malloc(sizeof(*ptable));
/* Do stuff with new table, then update argument */
*my_table = ptable;
}
#包括
#包括
typedef结构表{
整数大小;
}表;
int create_表格(表格**表格,int大小){
表*新表=malloc(sizeof(*新表));
if(新表==NULL){
返回-1;
}
新建表格->大小=大小;
*表=新的_表;
返回0;
}
内部主(空){
表*我的表;
if(创建_表(&my_表,5)=-1){
fprintf(stderr,“无法为新表分配内存。\n”);
返回退出失败;
}
printf(“新表格大小为%d\n”,我的表格->大小);
免费(我的桌子);
返回0;
}
当然,您可以让
create_table()
将table*
返回到新创建的表中,但在您的情况下,该函数已声明为返回int
。可能有很多原因,但在上面我只是假设它返回一个错误代码。我们知道,C中的函数只能返回一个值,因此如果它返回的是int
,它也不能返回表*
,因此获取新指针的唯一方法是修改参数,如果要修改表*
,则必须传递该表*
的地址,因此,如果使用Table\t**mytable,函数必须接受Table**
do(*mytable)->大小=5代码>
#include <stdio.h>
#include <stdlib.h>
typedef struct table {
int size;
} Table;
int create_table(Table ** table, int size) {
Table * new_table = malloc(sizeof(*new_table));
if ( new_table == NULL ) {
return -1;
}
new_table->size = size;
*table = new_table;
return 0;
}
int main(void) {
Table * my_table;
if ( create_table(&my_table, 5) == -1 ) {
fprintf(stderr, "Couldn't allocate memory for new table.\n");
return EXIT_FAILURE;
}
printf("New table size is %d\n", my_table->size);
free(my_table);
return 0;
}
如果使用table\u t*mytable
domytable->size=5代码>
#include <stdio.h>
#include <stdlib.h>
typedef struct table {
int size;
} Table;
int create_table(Table ** table, int size) {
Table * new_table = malloc(sizeof(*new_table));
if ( new_table == NULL ) {
return -1;
}
new_table->size = size;
*table = new_table;
return 0;
}
int main(void) {
Table * my_table;
if ( create_table(&my_table, 5) == -1 ) {
fprintf(stderr, "Couldn't allocate memory for new table.\n");
return EXIT_FAILURE;
}
printf("New table size is %d\n", my_table->size);
free(my_table);
return 0;
}
不,我需要Table**Table作为此项目的参数参数这与问题无关:-需要通过引用获得Table*
的函数参数需要使用Table**
(对于任何类型foo
,如果需要通过引用传递,则需要使用foo*
)这就是问题所在。然后他所要做的就是添加一个额外的“*”,然后你得到table\t**mytable
,你回到原来的问题,没有回答任何问题:-PAnd更改main()
中的所有其他行,除了返回0
.Btw,\u t
是某些操作系统(如Mac OS X和Linux,通常是尝试支持POSIX的所有操作系统)上的保留后缀,除非您知道自己不在这些系统上,否则不应使用。确定Table**Table参数作为参数传入以创建表。例如:int create\u Table(Table**Table,int size,等等){在这种情况下,为什么表有一个双指针?它没有用吗?假设您正在向它传递一个指针的地址,我将快速举出一个例子。@user2817240:添加了一个例子和一个解释来说明为什么这种类型的东西没有用。
typedef struct /* table */ {
int size;
} table_t; //conventional naming
int main() {
table_t *mytable = malloc((sizeof *mytable)); //avoid "Access Violation", by allocating memory
mytable->size = 5; //set size to '5'
return 0; //conventional return
}