Couple Beginner questions

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Apr 2008
Posts: 61
Reputation: chunalt787 is an unknown quantity at this point 
Solved Threads: 1
chunalt787 chunalt787 is offline Offline
Junior Poster in Training

Couple Beginner questions

 
0
  #1
Sep 19th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,733
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 737
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Couple Beginner questions

 
2
  #2
Sep 19th, 2009
>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>.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 61
Reputation: chunalt787 is an unknown quantity at this point 
Solved Threads: 1
chunalt787 chunalt787 is offline Offline
Junior Poster in Training

Re: Couple Beginner questions

 
0
  #3
Sep 20th, 2009
O wow I didn't even see the typedef keyword there. My brain is a little ovewhelmed.

Thanks
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC