943,747 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 500
  • C RSS
Sep 19th, 2009
0

Couple Beginner questions

Expand Post »
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.
  1. typedef struct List
  2. {
  3.  
  4. char* Name;
  5. char* Address;
  6. BOOL is_present
  7.  
  8. }List_info;
  9.  
  10. 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).
  1. typedef struct LinkedList { //points to the management block
  2. int dManage;
  3. int size;
  4. struct LinkedList* prev;
  5. struct LinkedList* next;
  6. };
  7.  
  8. 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.
  1. extern unsigned int init_allocator(unsigned int _basic_block_size,
  2. unsigned int _length) {
  3.  
  4. //Computer the max num of uniqe size blocks
  5. unsigned int numBlocks = (int)floor((log((double)_length)/log(2.0))-(log(_basic_block_size)/log(2.0)) + 1);
  6. blockBegin = malloc((size_t)_length); //initialize the first block
  7. struct LinkedList * bucketListTemp[numBlocks];
  8. bucketList = bucketListTemp; //initialize an array of pointers to LL's
  9.  
  10. bucketList[numBlocks - 1] = blockBegin; //Last element in bucket list is bigest block
  11. bucketList[numBlocks - 1] = bucketList[numBlocks - 1] | 0x01; //set to free
  12. bucketList[numBlocks - 1] = bucketList[numBlocks - 1] | 0x02; //set to no neigbor

I am using GCC btw. Thanks you for any help you can provide.
Similar Threads
Reputation Points: 39
Solved Threads: 1
Junior Poster in Training
chunalt787 is offline Offline
84 posts
since Apr 2008
Sep 19th, 2009
2

Re: Couple Beginner questions

>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:
  1. typedef struct Node {
  2. /* ... */
  3. } Node;
  4.  
  5. struct Node *foo; /* Okay */
  6. 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>.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Sep 20th, 2009
0

Re: Couple Beginner questions

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

Thanks
Reputation Points: 39
Solved Threads: 1
Junior Poster in Training
chunalt787 is offline Offline
84 posts
since Apr 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Understanding some function
Next Thread in C Forum Timeline: Can anyone help me..





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC