I am a C++ and Java programmer and I just have a couple simple syntax questions that I can't find a clear answer to. Yes I tried Google.

Question 1: I found the following code on another forum.

typedef struct List
{

char* Name;
char* Address;
BOOL is_present

}List_info;

List_info *List_count[10];

What is the difference between List and List_Info? I was under the impression that List_Info was an instantiated object of type List but it looks like the last line uses List_info as a type not an object. In short what is the diff between List and List_info.

Question 2:
I am trying to write a memory allocator and need an array of pointers that point to Linked List nodes. I wrote the following struct and instantiated the following object in the global space(outside any function definition).

typedef struct LinkedList {		//points to the management block
	int dManage;
	int size;
	struct LinkedList* prev;
	struct LinkedList* next;
};

struct LinkedList bucketlist[];

The compiler says that on line 8 bucket list has an incomplete type. All I am trying to do is create an Array of pointers to LinkedList structs in the global namespace so other functions can access them but when it needs to be initialized I want to resize the array to be of size numBlocks. I can't seem to find a problem.

extern unsigned int init_allocator(unsigned int _basic_block_size, 
								   unsigned int _length) {
	
	//Computer the max num of uniqe size blocks
	unsigned int numBlocks = (int)floor((log((double)_length)/log(2.0))-(log(_basic_block_size)/log(2.0)) + 1);
	blockBegin = malloc((size_t)_length);	//initialize the first block
	struct LinkedList * bucketListTemp[numBlocks];
	bucketList = bucketListTemp;	//initialize an array of pointers to LL's

	bucketList[numBlocks - 1] = blockBegin;	//Last element in bucket list is bigest block
	bucketList[numBlocks - 1] = bucketList[numBlocks - 1] | 0x01;	//set to free
	bucketList[numBlocks - 1] = bucketList[numBlocks - 1] | 0x02;		//set to no neigbor

I am using GCC btw. Thanks you for any help you can provide.

>What is the difference between List and List_Info?
The struct keyword. If you want to use List, the struct keyword is required. The whole point of typedefing a structure is to remove that requirement. Further, you can use the same name because structure tags and typedef names reside in different name spaces:

typedef struct Node {
  /* ... */
} Node;

struct Node *foo; /* Okay */
Node *bar; /* Also okay */

>The compiler says that on line 8 bucket list has an incomplete type.
The compiler is right. If you want a "resizable array", you can't use an array because arrays require a compile-time constant size[1]. Use a pointer instead, and simulate an array by allocating memory to it.

[1] C99 added variable length arrays. GCC supports them to some extent presently, but <insert the usual caveats about portability>.

O wow I didn't even see the typedef keyword there. My brain is a little ovewhelmed.

Thanks

This article has been dead for over six months. Start a new discussion instead.