![]() |
|
#1
|
|||
|
|||
throwing an struct(with an array) through a functionProblem:
I need to pass an struct to a Function. within the struct is an array, the array is two dimensional.And the structure should handle different kinds of array sizes (char items[][] = not always of the same size) Here is some code I have come up with till now.... if there is someone who could please shed some light on it for me..... first I create an struct typedef struct { int aantal; char titel[15]; char **items; // } TMenu; Then I do this: TMenu hoofdmenu, dagmenu; Next I Fill hoofdmenu who is an struct of TMenu: hoofdmenu.items [][]= { {"1 Nieuwe dag "}, {"2 Communicatie"}, {"3 Versie info "}, {"4 beheer "} }; Lateron I pass that one to an Function: TMenu toonmenu(TMenu menu ) // { } Any help would be welcome...... greets roel |
|||
|
#2
|
||||
|
||||
|
HI Roel. Welcome to the forums.
I don't see a problem with anything that you have done except for this here: (at least this is where my compiler grumbles): CPP / C++ / C Code:
I don't think this is valid because you have already defined what hoofdmenu.items is. I think that you are going to have to assign the values in a function in line and allocate your memory at the same time. I haven't checked this syntax, but you may need to do something like(c-syntax): CPP / C++ / C Code:
It's ugly, but I am not sure of any other way to do this. Anyone else got any thoughts? Also, one thing about the forums here. You can highlight your code in C syntax with the c and /c switch inside of []. |
|
#3
|
|||
|
|||
|
hi thanks for your reply.
I will look into it today or tomorrow... greets roel |
|
#4
|
||||
|
||||
|
Hopefully you don't mind my posting unsolicited advice in this thread. I was thinking about your problem and how in all of the projects and programs I have done I rarely see a double pointer. It is not that I use arrays either. Generally, it is because I use a list in most cases. Your sample is a perfect application (IMO) for a list. You can implement this list anyway you want, but I have posted a header file that includes a full definition for a linked list here. Take a look at the posted code for how to handle this easier with a linked list:
CPP / C++ / C Code:
You do have to do a bit of typecasting and it does use some C++ style syntax (ie - a class), but IMO it is much more dynamic and easier to use. |
|
#5
|
|||
|
|||
thanks for the helpthanks for the help so far.
but I have decided to use an other way... Instead of doing it al with an dynamic array I just give the number of array elements it has with an extra parameter.... It's not the stylish way I know but in the whole project it's needed about 8 times so far..... I also think that list thingy isn't a good option for me, cause I am restricted to C and only C. because of the compiler, its not for ordinary computer but different kind of hardware. Thanks for your help roel |
|
#6
|
||||
|
||||
|
Yep. This is sometimes the best way to go. I was considering rewriting my list implementation to use c-syntax only anyway. If you want to consider using a list, I could do this pretty quickly.
Anyway you go, good luck! |
Recent GIDBlog
Programming ebook direct download available by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| c: array comparison | jack | C Programming Language | 7 | 26-Jan-2004 12:21 |
| Sorting a 2D array c++ | mike3340 | C++ Forum | 4 | 15-Dec-2003 14:35 |
| Extra null element in an array | samtediou | MySQL / PHP Forum | 2 | 11-Dec-2003 12:52 |
| i/o: set value x (long) before it is used by a function | gmn | C Programming Language | 1 | 18-Nov-2003 02:12 |
| deleting from an array | ionyka | C++ Forum | 1 | 02-Sep-2003 04:09 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The