hey guys right so i have been at this problem for the last 6 hours and have been hitting google like mad to no avail. Right I need a pointer to an array. This array contains pointers to Linked lists. Im going to have to malloc it since I dont know the array size until runtime.
嘿伙计们,所以我在过去的6个小时里一直在这个问题,并一直在谷歌疯狂无济于事。对,我需要一个指向数组的指针。此数组包含指向链接列表的指针。我将不得不malloc它,因为我不知道数组大小,直到运行时。
LList **array
this was my first thought but this just gives me a pointer to an array of LList. Or atleast that is my understanding. Can someone give me a hand? Alex
这是我的第一个想法,但这只是给了我一个LList数组的指针。或者至少这是我的理解。有人可以帮我一把吗?亚历克斯
EDIT: ok some info on how it would be used. Im implementing a very basic hash table. there is a structure that contains a pointer to an array of pointers to linked lists. it needs to be a pointer to the array so that when i resize the table, i can just change the pointer to point to the larger table.
编辑:确定如何使用它的一些信息。我实现了一个非常基本的哈希表。有一个结构包含指向链表的指针数组的指针。它需要是一个指向数组的指针,这样当我调整表的大小时,我只需将指针更改为指向较大的表即可。
5 个解决方案
#1
5
It sounds like you're on the right track.
听起来你走在正确的轨道上。
LList **array;
array = malloc(num_ptrs * sizeof(LList*));
array
is now an array of pointers to LList
, and elements such as array[3]
will be a pointer to a LList
.
array现在是一个指向LList的指针数组,而array [3]之类的元素将是一个指向LList的指针。
Arrays and pointers are very similar in C (but not identical!), as shown by the classic example: *(array + 2)
is mostly equivalent to array[2]
.
数组中的数组和指针非常相似(但不完全相同!),如经典示例所示:*(数组+2)大部分等同于数组[2]。
Edit: When you need to resize the table, you'll just need to realloc
the additional space:
编辑:当您需要调整表格大小时,您只需要重新分配额外的空间:
LList **new_array;
new_array = realloc(old_array, new_size * sizeof(LList*));
new_array
and old_array
may or may not be the same pointer afterwards, but either way new_array
is guaranteed to be a pointer to enough space to hold the new array (or NULL
if the memory couldn't be allocated)
new_array和old_array之后可能是也可能不是同一个指针,但是无论哪种方式,new_array都保证是一个指向足够空间来保存新数组的指针(如果无法分配内存,则为NULL)
2nd Edit: As user411313 alluded, if you want the actual pointer to the array, you'll need to take the address of the array:
第二次编辑:正如user411313所提到的,如果你想要指向数组的实际指针,你需要获取数组的地址:
LList ***p_array;
p_array = &array;
#2
0
A pointer to an object, is basically the same as a pointer to an array.
指向对象的指针基本上与指向数组的指针相同。
int * blah; // an int pointer. It could point to an array of ints, or a single int.
int ** blah; // a pointer to an int pointer. It could point to something that points to an int, or it could be pointing to an array of pointers to single ints, or it could be a pointer that points to an array of ints.
It all depends on how you use it.
这一切都取决于你如何使用它。
#3
0
A pointer to a pointer can also be an array of pointers.
指向指针的指针也可以是指针数组。
int nLists; /* number of lists*/
LList **array;
array = (LList **)malloc(nLists * sizeof(LList *));
will make array
be an array of pointers to LList
. Then array[i]
will give you the pointer to the i-th linked list in the array.
将使数组成为一个指向LList的指针数组。然后array [i]会给你指向数组中第i个链表的指针。
#4
0
if you have to write your own linked list, you can do this.
如果你必须编写自己的链表,你可以这样做。
typedef struct LLNode {
LLNode* next;
int data;
} LLNode;
LLNode* linkedList = null; // a linked list
LLNode** linkedListArray = (LLNode**) malloc( arraySize* sizeof(LLNode*) );
LLNode*** pointerToLListArray = &linkedListArray;
with a linked list library:
使用链表库:
LList* linkedListArray = (LList*) malloc( arraySize* sizeof(LList) );
LList** pointerToLListArray = &linkedListArray;
#5
0
typedef struct LList LList;
struct LList {
int value;
LList *next; };
LList *(*p)[3]; /* pointer to an array of 3 pointers to LList */
LList ll1 = {11};
LList ll2 = {22};
LList ll3 = {33};
size_t sizeofarray = sizeof*p/sizeof**p; /* calc arraysize at runtime here */
p = malloc( sizeofarray * sizeof**p ); /* allocate space for each LList-pointer in array */
(*p)[0] = &ll1;
(*p)[1] = &ll2;
(*p)[2] = &ll3;
/* test output here: */
printf("\n%d\n%d\n%d", ((*p)[0])->value,((*p)[1])->value,((*p)[2])->value);
free(p);
#1
5
It sounds like you're on the right track.
听起来你走在正确的轨道上。
LList **array;
array = malloc(num_ptrs * sizeof(LList*));
array
is now an array of pointers to LList
, and elements such as array[3]
will be a pointer to a LList
.
array现在是一个指向LList的指针数组,而array [3]之类的元素将是一个指向LList的指针。
Arrays and pointers are very similar in C (but not identical!), as shown by the classic example: *(array + 2)
is mostly equivalent to array[2]
.
数组中的数组和指针非常相似(但不完全相同!),如经典示例所示:*(数组+2)大部分等同于数组[2]。
Edit: When you need to resize the table, you'll just need to realloc
the additional space:
编辑:当您需要调整表格大小时,您只需要重新分配额外的空间:
LList **new_array;
new_array = realloc(old_array, new_size * sizeof(LList*));
new_array
and old_array
may or may not be the same pointer afterwards, but either way new_array
is guaranteed to be a pointer to enough space to hold the new array (or NULL
if the memory couldn't be allocated)
new_array和old_array之后可能是也可能不是同一个指针,但是无论哪种方式,new_array都保证是一个指向足够空间来保存新数组的指针(如果无法分配内存,则为NULL)
2nd Edit: As user411313 alluded, if you want the actual pointer to the array, you'll need to take the address of the array:
第二次编辑:正如user411313所提到的,如果你想要指向数组的实际指针,你需要获取数组的地址:
LList ***p_array;
p_array = &array;
#2
0
A pointer to an object, is basically the same as a pointer to an array.
指向对象的指针基本上与指向数组的指针相同。
int * blah; // an int pointer. It could point to an array of ints, or a single int.
int ** blah; // a pointer to an int pointer. It could point to something that points to an int, or it could be pointing to an array of pointers to single ints, or it could be a pointer that points to an array of ints.
It all depends on how you use it.
这一切都取决于你如何使用它。
#3
0
A pointer to a pointer can also be an array of pointers.
指向指针的指针也可以是指针数组。
int nLists; /* number of lists*/
LList **array;
array = (LList **)malloc(nLists * sizeof(LList *));
will make array
be an array of pointers to LList
. Then array[i]
will give you the pointer to the i-th linked list in the array.
将使数组成为一个指向LList的指针数组。然后array [i]会给你指向数组中第i个链表的指针。
#4
0
if you have to write your own linked list, you can do this.
如果你必须编写自己的链表,你可以这样做。
typedef struct LLNode {
LLNode* next;
int data;
} LLNode;
LLNode* linkedList = null; // a linked list
LLNode** linkedListArray = (LLNode**) malloc( arraySize* sizeof(LLNode*) );
LLNode*** pointerToLListArray = &linkedListArray;
with a linked list library:
使用链表库:
LList* linkedListArray = (LList*) malloc( arraySize* sizeof(LList) );
LList** pointerToLListArray = &linkedListArray;
#5
0
typedef struct LList LList;
struct LList {
int value;
LList *next; };
LList *(*p)[3]; /* pointer to an array of 3 pointers to LList */
LList ll1 = {11};
LList ll2 = {22};
LList ll3 = {33};
size_t sizeofarray = sizeof*p/sizeof**p; /* calc arraysize at runtime here */
p = malloc( sizeofarray * sizeof**p ); /* allocate space for each LList-pointer in array */
(*p)[0] = &ll1;
(*p)[1] = &ll2;
(*p)[2] = &ll3;
/* test output here: */
printf("\n%d\n%d\n%d", ((*p)[0])->value,((*p)[1])->value,((*p)[2])->value);
free(p);