GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 12-Feb-2004, 04:23
knakworstje knakworstje is offline
New Member
 
Join Date: Feb 2004
Posts: 3
knakworstje is on a distinguished road

throwing an struct(with an array) through a function


Problem:
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; // is this correct !?!?
} 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  
Old 12-Feb-2004, 08:29
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
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:
hoofdmenu.items [][]= {
		{"1 Nieuwe dag  "},
		{"2 Communicatie"},
		{"3 Versie info "},
		{"4 beheer      "}
  };

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:
hoofdmenu.items = (char**) malloc(4*sizeof(char*));
hoofdmenu.items[0] = (char*) malloc(strlen("1 Nieuwe dag")+1);
strcpy(hoofdmenu.items[0], "1 Nieuwe dag ");

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  
Old 12-Feb-2004, 09:06
knakworstje knakworstje is offline
New Member
 
Join Date: Feb 2004
Posts: 3
knakworstje is on a distinguished road
hi thanks for your reply.
I will look into it today or tomorrow...
greets roel
  #4  
Old 13-Feb-2004, 08:19
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
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:
#include <string.h>

#include "list1.h"

typedef struct {
	int aantal;
	char titel[15];
	list*	items;
} TMenu;

TMenu hoofdmenu, dagmenu;

TMenu toonmenu(TMenu menu ) //
{
	printf("%s\n",hoofdmenu.titel);
	printf("----------------\n");
    menu.items->home();
	do{
		printf("%s\n",(char*)hoofdmenu.items->get_data());
	}while(menu.items->next());

}


int main(){

	hoofdmenu.items = new list;

	hoofdmenu.items->add_data((void*)"1 Nieuse dag");
	hoofdmenu.items->add_data((void*)"2 Communicatie");
	hoofdmenu.items->add_data((void*)"3 Versie info");
	hoofdmenu.items->add_data((void*)"4 beheer");

	strcpy(hoofdmenu.titel,"Main");

	toonmenu(hoofdmenu);
	return 0;
}

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  
Old 15-Feb-2004, 16:33
knakworstje knakworstje is offline
New Member
 
Join Date: Feb 2004
Posts: 3
knakworstje is on a distinguished road

thanks for the help


thanks 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  
Old 15-Feb-2004, 17:20
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
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 GIDBlogProgramming ebook direct download available by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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

All times are GMT -6. The time now is 05:13.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.