What I'm trying to do is pretty straight forward in other languages. But I'm struggling with this in a C project and didn't find a right way to do it in my researches.
What I need to do is:
- Declare an global empty array of strings
- Inside a function I want to populate this global array with X new strings
- In another function, I want loop through all new elements of this array printing them out.
The code I have now is listed below.
#include <stdio.h>
const char *menu[] = {};
void populateMenu(){
// populate this menu with some itens
*menu = {
"New item A",
"New item B",
NULL
};
}
int main(int argc, const char * argv[])
{
// 1. print inicial menu
int menuAlen = sizeof(menu)/sizeof(*menu);
int i;
for(i = 0; i < menuAlen; i++){
printf("%s\n", menu[i]);
}
// 2. populate the menu
populateMenu();
// 3. print it again with new values
int menuBlen = sizeof(menu)/sizeof(*menu);
for(i = 0; i < menuBlen; i++){
printf("%s\n", menu[i]);
}
return 0;
}
I'm currently getting this error on build time.
main.c:16:16: Expected expression
Line 16 is the first line inside populateMenu function.
Can someone help me out with this? Thanks in advance.
Best. George.