473,387 Members | 1,542 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Problems with realloc on array of pointers

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
7 8677
Banfa
9,065 Expert Mod 8TB
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
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
844 Expert 512MB
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
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
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
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
9,065 Expert Mod 8TB
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

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

Similar topics

6
by: Andreas Bauer | last post by:
I've got an array in the vein of MyType* array; arry = new MyType; This of course works fine, but I have the need to dynamically grow the boundaries of that array at runtime. I can't seem to...
4
by: Anthony | last post by:
Hello, I am writing a function that populates an array of pointers to strings. Both the number of strings in the array, and the lengths of the strings, are dynamic; in particular, the number of...
12
by: Franz | last post by:
Greetings, I have created the following types: typedef struct _tbl_certificate{ int cert_id; char *cert_name; }tbl_certificate; typedef struct _cameraObj{ tbl_camera camera;
9
by: Goh, Yong Kwang | last post by:
I'm currently doing a project that has a array that supposed to be determined only at runtime. Originally, the prototype I did as a proof of theory (or a quick hack) So one method is to create a...
4
by: John | last post by:
I'm trying (struggling) to use realloc to grow a list of strings. The number of strings is not known (this is a subset of an assignment to write a recursive ls program... my BS was in EE, so I'm...
36
by: Roy | last post by:
Hi all : My code below : #include <stdio.h> #include <string.h> #include <stdlib.h> char *cat(char *s, const char *t) { char *tmp;
86
by: Walter Roberson | last post by:
If realloc() finds it necessary to move the memory block, then does it free() the previously allocated block? The C89 standard has some reference to undefined behaviour if one realloc()'s memory...
18
by: hyperboogie | last post by:
Hello all I'm pretty new to C, so please accept my apologies in advance :-) I'm trying to allocate space for an array of pointers to strings (which are accepted as ellipses) inside a while...
6
by: lithiumcat | last post by:
Hi, maybe you remember me, some time ago I asked about how to store an integer value into a void*, and I learned that doing pointer arithmetic yeilding a pointer outside of an object (except the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.