简体   繁体   English

指向链表指针数组的指针

[英]Pointer to an array of pointers to Linked lists

I have been at this problem for the last 6 hours and have been hitting google like mad to no avail.我一直在这个问题在过去6小时已经创下谷歌像疯了一样无济于事。

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.我将不得不对其进行 malloc,因为直到运行时我才知道数组大小。

LList **array

This was my first thought but this just gives me a pointer to an array of LList.这是我的第一个想法,但这只是给了我一个指向 LList 数组的指针。 Or atleast that is my understanding.或者至少这是我的理解。 Can someone give me a hand?有人可以帮我一把吗?

EDIT: Some info on how it would be used: I am 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.我可以将指针更改为指向更大的表格。

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] .数组和指针在 C 中非常相似(但不完全相同!),如经典示例所示: *(array + 2)大部分等价于array[2]

Edit: When you need to resize the table, you'll just need to realloc the additional space:编辑:当您需要调整表,你只需要realloc额外的空间:

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_arrayold_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;

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.这一切都取决于你如何使用它。

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;

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 .将使array成为指向LList的指针数组。 Then array[i] will give you the pointer to the i-th linked list in the array.然后array[i]会给你指向数组中第 i 个链表的指针。

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM