| | |
Couple Beginner questions
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Apr 2008
Posts: 61
Reputation:
Solved Threads: 1
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.
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).
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.
I am using GCC btw. Thanks you for any help you can provide.
Question 1: I found the following code on another forum.
C Syntax (Toggle Plain Text)
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).
C Syntax (Toggle Plain Text)
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.
C Syntax (Toggle Plain Text)
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:
>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>.
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:
C Syntax (Toggle Plain Text)
typedef struct Node { /* ... */ } Node; struct Node *foo; /* Okay */ Node *bar; /* Also okay */
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.
![]() |
Similar Threads
- Arrays (Two easy questions) (C++)
- A Couple of Web Questions (Python)
- My solution to 2 beginner questions (C++)
- VBScript Beginner Questions (Visual Basic 4 / 5 / 6)
- Couple of CSS questions (HTML and CSS)
- beginner (C++)
- Beginner's questions: C++ and databases (C++)
Other Threads in the C Forum
- Previous Thread: error: invalid operands to binary ^ (have ‘float’ and ‘double’)
- Next Thread: Can anyone help me..
| Thread Tools | Search this Thread |
* ansi api array arrays bash binarysearch calculate centimeter changingto char character convert copyanyfile copypdffile createcopyoffile createprocess() csyntax directory dynamic fflush file floatingpointvalidation fork forloop frequency getlasterror getlogicaldrivestrin givemetehcodez graphics gtkgcurlcompiling gtkwinlinux hardware highest homework i/o ide inches initialization intmain() iso km license linked linkedlist linux linuxsegmentationfault list logical_drives loopinsideloop. lowest match matrix microsoft motherboard mqqueue multi mysql oddnumber odf open opendocumentformat openwebfoundation pdf pointer pointers posix power program programming pyramidusingturboccodes read recursion recv recvblocked repetition reversing scanf scheduling segmentationfault send shape single socketprogramming stack standard strchr string strings suggestions test testautomation unix urboc user variable whythiscodecausesegmentationfault win32api windows.h windowsapi






