473,804 Members | 3,196 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problems with realloc on array of pointers

8 New Member
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 8722
Banfa
9,065 Recognized Expert Moderator Expert
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 menuStructPoint er) to remove a level of *s.
Apr 27 '07 #2
roguefeebo
8 New Member
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 Recognized Expert Contributor
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
roguefeebo
8 New Member
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
roguefeebo
8 New Member
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 initSearchNameM enu function.

Can anyone give me any insight as to why the program is crashing on the third call to addMenuItem within initSearchNameM enu?
Apr 28 '07 #6
roguefeebo
8 New Member
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 Recognized Expert Moderator Expert
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
5314
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 use std :: vector or any other container, cause I've got two more "lists" which keep pointers to the elements of `array'.
4
4988
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 strings won't be known until just before the function returns. The problem is that in the calling function, I need to know both the address of the array, and the number of strings. My first thought was to pass a pointer to the array into the...
12
1718
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
4074
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 linked list and use malloc to create LinkedListNode as needed and use free() to destroy a node. But another easier way would be to use the realloc() function call to resize a memory block in the heap so that all the "nodes" are now consecutive...
4
4071
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 trying to catch up!). I'm working on the following program to try to figure this out. Now, I'm at the point where I'm filling up plines with (I belive) a list of pointers, each of which is pointing to a string ("foo 1", "foo 2", and so forth). ...
36
2836
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
4180
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 that was freed by realloc(), but the only way explicitly mentioned in the C89 standard to free memory via realloc() is to realloc() it down to 0 bytes. I had always assumed it would automatically free the previous memory, but is the behaviour...
18
1615
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 loop, and after the allocation, when i "assert" the allocation, the assertion fails!!! void printStrings(s1, ...){ //ellipses function ....
6
2326
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 one- after-last thingy) is undefined behaviour. Actually I was trying to associate a function pointer with a key, through an AVL tree that managed void* data. Function pointers can't be stored in void* (that is, the standard does not garantee...
0
9704
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9571
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10318
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10302
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10069
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7608
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5505
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5639
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2976
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.