By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,173 Members | 1,079 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,173 IT Pros & Developers. It's quick & easy.

Problems with realloc on array of pointers

P: 8
I'm very new to programming so be gentle, :(

Essentially what I would like to do is to have a single function to be able to create a dynamic array of pointers to a struct so that I can have a modular menu system up and running. My menus will have several varying numbers of options. This is the first time I've worked with something like this and just can't get it worked out.

I've snipped a lot of unnecessary code to this smaller piece so I can work out the data flow. These are the exact same kinds of errors I get in the larger program though.

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. struct menuStruct
  5. {
  6.   char question[100];
  7.   void *menuCommand;
  8.   struct menuStruct *nextMenu[];
  9. }; //end menuStruct
  10. typedef struct menuStruct *searchNameMenu[1];
  11.  
  12.  
  13.  
  14. void addMenuItem(menuStruct **menu[], char question[], void menuCommand(), menuStruct **nextMenu[])
  15. {
  16. int i = 0;
  17. menuStruct *newOption;
  18.  
  19. newOption = malloc(sizeof(menuStruct));
  20. strncpy(newOption->question, question, 100);
  21. newOption->menuCommand = menuCommand;
  22. newOption->nextMenu = &nextMenu;
  23.  
  24. menu = realloc(menu, ((sizeof(menu[]) + sizeof(menuStruct*)));
  25.  
  26. while(menu[i] != NULL)
  27.   i++;
  28.  
  29. *menu[i] = newOption;
  30. } //end function addMenuItem
  31.  
  32.  
  33.  
  34. void initSearchNameMenu()
  35. {
  36.  
  37. addMenuItem(searchNameMenu, "Search names by name.", searchNameDB_Name, NULL);
  38. addMenuItem(searchNameMenu, "Return to Previous Menu.", returnPrevMenu, NULL);
  39. addMenuItem(searchNameMenu, "Return to Main Menu.", returnMainMenu, NULL);
  40. } //end function initSearchNameDBMenu
As you can tell from the errors, I'm fairly lost. The errors I get are:



Expand|Select|Wrap|Line Numbers
  1. c:12: error: syntax error before '*' token
  2. c:12: error: syntax error before '*' token
  3. c:15: warning: data definition has no type or storage class
  4. c:17: error: conflicting types for 'newOption'
  5. c:15: error: previous declaration of 'newOption' was here
  6. c:17: warning: initialization makes integer from pointer without a cast
  7. c:17: error: initializer element is not constant
  8. c:17: warning: data definition has no type or storage class
  9. c:18: error: syntax error before '->' token
  10. c:18: warning: conflicting types for built-in function 'strncpy'
  11. c:18: warning: data definition has no type or storage class
  12. c:19: error: syntax error before '->' token
  13. c:22: error: syntax error before ']' token
  14. c:22: error: syntax error before ')' token
  15. c:22: error: syntax error before ';' token
  16. c:22: warning: data definition has no type or storage class
  17. c:27: error: `i' undeclared here (not in a function)
  18. c:27: warning: data definition has no type or storage class
  19. c:28: error: syntax error before '}' token
  20. c: In function `initSearchNameMenu':
  21. c:35: error: syntax error before "searchNameMenu"
  22. c:36: error: syntax error before "searchNameMenu"
  23. c:37: error: syntax error before "searchNameMenu"
  24. c: At top level:
  25. c:12: error: storage size of `menuStruct' isn't known
  26. c:27: error: storage size of `menu' isn't known
I'm lost in a maze of array, pointers, and void pointers, :( As far as the error on line 27, I'm not sure why 'i' is undeclared as I have it and use it in the function before that.

Thanks for any help you can give!
Apr 27 '07 #1
Share this Question
Share on Google+
7 Replies


Banfa
Expert Mod 5K+
P: 8,916
Expand|Select|Wrap|Line Numbers
  1. typedef struct menuStruct *searchNameMenu[1];
  2.  
  3. void addMenuItem(menuStruct **menu[], char question[], void menuCommand(), menuStruct **nextMenu[])
  4. {
  5. ...
  6. }
  7.  
The first 2 obvious things I would say are that

1. I think you are probably trying to create a variable called searchNameMenu (that is how you use it later in the code) in the first line here but because of the typedef at the start of the line you actually are definiting a new type.

In the addMenuItem function the definition of the first parameter causes it to have a type of menuStruct ***, I do not think I have ever seen a situation that required a pointer to a pointer to a pointer and I suspect this is an error.


As a tip if you are getting lost in pointers I find thinking in 'int's, or typedefing the pointer can help. i.e. you have a menuStruct ** to work with, this is a pointer to a type, in this case it is a pointer to menuStruct * but how would the code work if it was just a pointer to and int (i.e. replace menuStruct * with int in you head) or alternatively typedef menuStruct * to something (say menuStructPointer) to remove a level of *s.
Apr 27 '07 #2

P: 8
Thanks a bunch, that helped a lot!
Based on your suggestions I changed the code to:

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. struct menuStruct
  5. {
  6.   char question[100];
  7.   void *menuCommand;
  8.   struct menuStruct *nextMenu[];
  9. }; //end menuStruct
  10. typedef struct menuStruct *menuStructPtr;
  11. menuStructPtr searchNameMenu[1];
  12.  
  13. void searchNameDB_Name();
  14. void addMenuItem(menuStructPtr menu[], char question[], void menuCommand(), menuStructPtr nextMenu[]);
  15.  
  16. int main()
  17. {
  18.  
  19. }
  20.  
  21. void addMenuItem(menuStructPtr menu[], char question[], void menuCommand(), menuStructPtr nextMenu[])
  22. {
  23. int i = 0;
  24. menuStructPtr newOption;
  25.  
  26. newOption = malloc(sizeof(menuStructPtr));
  27. strncpy(newOption->question, question, 100);
  28. newOption->menuCommand = menuCommand;
  29. newOption->nextMenu = nextMenu;
  30.  
  31. menu = realloc(menu, ((sizeof(menu) + sizeof(menuStructPtr)));
  32.  
  33. while(menu[i] != NULL)
  34.   i++;
  35.  
  36. menu[i] = newOption;
  37. } //end function addMenuItem
  38.  
  39.  
  40.  
  41. void initSearchNameMenu()
  42. {
  43.  
  44. addMenuItem(searchNameMenu, "Search names by name.", searchNameDB_Name, NULL);
  45. } //end function initSearchNameDBMenu
  46.  
  47. void searchNameDB_Name()
  48. {
  49.  
  50.      }
  51.  
And now the only errors I get that I can't figure out are:
Expand|Select|Wrap|Line Numbers
  1. In function `addMenuItem':
  2. c:29: error: invalid use of flexible array member
  3. c:31: error: syntax error before ';' token
Thanks again!
Apr 27 '07 #3

ilikepython
Expert 100+
P: 844
Thanks a bunch, that helped a lot!
Based on your suggestions I changed the code to:

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. struct menuStruct
  5. {
  6.   char question[100];
  7.   void *menuCommand;
  8.   struct menuStruct *nextMenu[];
  9. }; //end menuStruct
  10. typedef struct menuStruct *menuStructPtr;
  11. menuStructPtr searchNameMenu[1];
  12.  
  13. void searchNameDB_Name();
  14. void addMenuItem(menuStructPtr menu[], char question[], void menuCommand(), menuStructPtr nextMenu[]);
  15.  
  16. int main()
  17. {
  18.  
  19. }
  20.  
  21. void addMenuItem(menuStructPtr menu[], char question[], void menuCommand(), menuStructPtr nextMenu[])
  22. {
  23. int i = 0;
  24. menuStructPtr newOption;
  25.  
  26. newOption = malloc(sizeof(menuStructPtr));
  27. strncpy(newOption->question, question, 100);
  28. newOption->menuCommand = menuCommand;
  29. newOption->nextMenu = nextMenu;
  30.  
  31. menu = realloc(menu, ((sizeof(menu) + sizeof(menuStructPtr)));
  32.  
  33. while(menu[i] != NULL)
  34.   i++;
  35.  
  36. menu[i] = newOption;
  37. } //end function addMenuItem
  38.  
  39.  
  40.  
  41. void initSearchNameMenu()
  42. {
  43.  
  44. addMenuItem(searchNameMenu, "Search names by name.", searchNameDB_Name, NULL);
  45. } //end function initSearchNameDBMenu
  46.  
  47. void searchNameDB_Name()
  48. {
  49.  
  50.      }
  51.  
And now the only errors I get that I can't figure out are:
Expand|Select|Wrap|Line Numbers
  1. In function `addMenuItem':
  2. c:29: error: invalid use of flexible array member
  3. c:31: error: syntax error before ';' token
Thanks again!
For the second you need to add another parenthesis in:
Expand|Select|Wrap|Line Numbers
  1. menu = realloc(menu, ((sizeof(menu) + sizeof(menuStructPtr))));
  2.  
or
Expand|Select|Wrap|Line Numbers
  1. menu = realloc(menu, (sizeof(menu) + sizeof(menuStructPtr)));
  2.  
Apr 27 '07 #4

P: 8
Excellent. I have no idea how I missed it, I guess I was questioning my use of realloc on the array more than looking at the simple stuff.

I'm trying to research the first error more without luck so far. I'll see if I can find something that explains it pretty well, but feel free to point something to me.
Apr 27 '07 #5

P: 8
I've got a couple more questions at the bottom you may want to read first so you know what you're looking for. I have changed the code a bit from yesterday:
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. struct menuStruct
  5. {
  6.   char question[100];
  7.   void *menuCommand;
  8.   struct menuStruct *nextMenu;
  9. }; //end menuStruct
  10. typedef struct menuStruct *menuStructPtr;
  11. menuStructPtr searchNameMenu[0];
  12.  
  13. void initSearchNameMenu();
  14. void searchNameDB_Name();
  15. void addMenuItem(menuStructPtr menu[], char question[], void menuCommand(), menuStructPtr nextMenu[]);
  16.  
  17. int main()
  18. {
  19.     initSearchNameMenu();
  20.  
  21.     return 0;
  22. }
  23.  
  24. void addMenuItem(menuStructPtr menu[], char question[], void menuCommand(), menuStructPtr nextMenu[])
  25. {
  26.   int i = 0;
  27.   menuStructPtr newOption;
  28.  
  29.   newOption = malloc(sizeof(menuStructPtr));
  30.   strncpy(newOption->question, question, 100);
  31.   newOption->menuCommand = menuCommand;
  32.   //newOption->nextMenu = nextMenu;
  33.  
  34.   printf("%d", i);
  35.   printf("%s", searchNameMenu[i]->question);
  36.   system("pause");
  37.  
  38.   menu = realloc(menu, (sizeof(menu) + sizeof(menuStructPtr)));
  39.  
  40.   while(menu[i] != NULL)
  41.     i++;
  42.  
  43.   printf("%d", i);
  44.   printf("%s", searchNameMenu[i]->question);
  45.   system("pause");
  46.  
  47.   menu[i] = newOption;
  48.  
  49.   printf("%d", i);
  50.   printf("%s", searchNameMenu[i]->question);
  51.   system("pause");
  52. } //end function addMenuItem
  53.  
  54.  
  55.  
  56. void initSearchNameMenu()
  57. {
  58.   addMenuItem(searchNameMenu, "Search names by name.", searchNameDB_Name, NULL);
  59.   addMenuItem(searchNameMenu, "Search names by percentage occurrence.", searchNameDB_Name, NULL);
  60.   addMenuItem(searchNameMenu, "Search names by length.", searchNameDB_Name, NULL);
  61. } //end function initSearchNameDBMenu
  62.  
  63. void searchNameDB_Name()
  64. {
  65.  
  66. }
  67.  
So no more error regarding flexible array types by making nextMenu a pointer to another variable of type menuStruct * instead of an undefined array.

I now get a warning of incompatible pointer types in the line

Expand|Select|Wrap|Line Numbers
  1. //newOption->nextMenu = nextMenu;
when it's not commented out. I thought it was assigning a menuStruct * to a menuStruct * typedef so they would be the same type, but I can't seem to work out a way to get rid of the warning. Are they really a compatible type that the compiler isn't recognizing due to not being able to use the typedef within the struct definition?

The bigger problem right now is that when the line is commented out, the code compiles fine and when running it, it prints out the first two items and then flat out crashes (without an error report pop-up from windows like a crash usually does). You can see I've tried debugging it, testing those variables, but the variables are being printed out correctly until the crash on the third statement in the initSearchNameMenu function.

Can anyone give me any insight as to why the program is crashing on the third call to addMenuItem within initSearchNameMenu?
Apr 28 '07 #6

P: 8
I found my "bigger" problem that was just a malloc on the pointer instead of a struct variable type. I still can't figure out the warning for incompatible pointer types, but it runs for now!
Apr 28 '07 #7

Banfa
Expert Mod 5K+
P: 8,916
Your structure defines

void *menuCommand;

which is a data pointer but you assign a function pointer to it, try

void (*menuCommand)();
Apr 29 '07 #8

Post your reply

Sign in to post your reply or Sign up for a free account.