奇妙な間接化をすべて回避するには、ローカル変数を使用して移動する方が簡単です。
void myfunc(Table ** my_table) {
Table * ptable = *my_table;
ptable->size = 5;
/* etc */
}
しかし、他の人が指摘し(*table)->size = 5
たように、などは同じことをします。
指されているものを変更する必要がある場合は、次のようにします。
void myfunc(Table ** my_table) {
Table * ptable = malloc(sizeof(*ptable));
/* Do stuff with new table, then update argument */
*my_table = ptable;
}
後者の例を次に示します。
#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;
}
もちろん、新しく作成されたテーブルにa を返すだけでもかまいませんが、あなたの場合、その関数は return として宣言されています。さまざまな理由が考えられますが、上記では、エラー コードが返されると想定しています。私たちが知っているように、C の関数は 1 つの値しか返すことができません。、そのアドレスを渡す必要があるため、関数は.create_table()
Table *
int
int
Table *
Table *
Table *
Table **