Thread: Problem passing struct data as a function parameter

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    6

    Problem passing struct data as a function parameter

    Hi guys!
    I'm having a problem using a struct I defined as a function parameter. The error is probably quite basic, but I haven't been able to solve it. The code is something like

    Code:
    typedef struct menuOps {
    	int num;
    	char *string[];
    } menuOPS;
    
    // Is it ok to declare a menuOPS object like this?
    menuOPS opsplash = {3, { "string1" , "string2" , "string3" } };
    
    void menu(void (*menufunc)(void), menuOPS ops) {	
    	int i;
    	menufunc(); // don't mind the first parameter, just a function pointer that prints stuff. Everything working here
    	for(i=0;i<=ops.num;i++)
    		printf(" %d - %s\n",i+1,ops.string[i]);
    }
    
    void main() {
    	printf("%s\n",opsplash.string[0]); // printfs' fine, "string1"
    	getchar();
    	menu(&menu_splash,opsplash);
    	getchar();
    }
    The problem is when menu() prints opsplash.string[i], when all I get is gibberish. It prints stuff like
    Code:
    1 - [??
    2 - ????
    3 - [????O
    4 - ????
    Can't say it is random trash, because it's exactly the same set of characters everytime I run it.

    Thanks in advance for the help!

  2. #2
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    You probably want:

    Code:
    char *string[3];
    If you intend it to be an array of three char pointers. Which also means that you should use < instead of <= in the loop to avoid printing 4 items from your array of size 3.

    Code:
    for(i=0;i < ops.num;i++)

  3. #3
    Registered User
    Join Date
    Apr 2011
    Posts
    6
    Thanks for the reply!
    Quote Originally Posted by Subsonics View Post
    You probably want:

    Code:
    char *string[3];
    Actually I intend it to be an array of num char pointers, so I can declare a different number of menu options in each case as I see fit. That's why I added the int num to the struct.

    Quote Originally Posted by Subsonics View Post
    If you intend it to be an array of three char pointers. Which also means that you should use < instead of <= in the loop to avoid printing 4 items from your array of size 3.

    Code:
    for(i=0;i < ops.num;i++)
    Oops, you're right of course. This is just an example output but I didn't even see that fourth option being printed. Anyway, this is not what's causing the weird char print. I'm obviously doing something wrong here, but I don't get what it is.

  4. #4
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Quote Originally Posted by k16 View Post
    Actually I intend it to be an array of num char pointers, so I can declare a different number of menu options in each case as I see fit. That's why I added the int num to the struct.
    Well, you can't. That is why I replied that you need to give it a size.

    You can use malloc to allocate space for it however. But then your initialization has to change as well.

  5. #5
    Registered User
    Join Date
    Apr 2011
    Posts
    6
    Ok, I get it Thanks. I'm using malloc now to define the size of the array and trying to code some functions besides initialization to deal with its contents.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing an array to a function as a parameter
    By boxden in forum C Programming
    Replies: 1
    Last Post: 03-13-2010, 09:35 AM
  2. Problems passing parameter to function
    By HAssan in forum C Programming
    Replies: 5
    Last Post: 10-21-2008, 02:26 PM
  3. Passing array of struct as a function parameter
    By desmond5 in forum C Programming
    Replies: 5
    Last Post: 12-04-2007, 11:32 AM
  4. Problem passing double array as a function parameter
    By pppbigppp in forum C++ Programming
    Replies: 7
    Last Post: 06-06-2006, 03:08 AM
  5. typecasting data for parameter passing
    By daluu in forum C++ Programming
    Replies: 8
    Last Post: 12-08-2004, 02:58 PM

Tags for this Thread