cancel
Showing results for 
Search instead for 
Did you mean: 

invalid initializer error

msalaz03
Associate

Hello,

My first time posting on this forum so apologies in advance. I am working with an LCD screen that will have multiple menu's so I designed this struct with multiple pointers. However, I am running into this error where I cannot seem to use malloc function. It just keeps saying 'invalid initalizer'

My Code:

 

#include "main.h" /* Private includes ----------------------------------------------------------*/ /* USER CODE BEGIN Includes */ #include "lcd8.h" #include <stdint.h> #include "stm32f411xe.h" #include <string.h> #include <stdlib.h> #include<stdio.h> /* USER CODE END Includes */ /* Private typedef -----------------------------------------------------------*/ /* USER CODE BEGIN PTD */ /* USER CODE END PTD */ /* Private define ------------------------------------------------------------*/ /* USER CODE BEGIN PD */ /* USER CODE END PD */ /* Private macro -------------------------------------------------------------*/ /* USER CODE BEGIN PM */ /* USER CODE END PM */ /* Private variables ---------------------------------------------------------*/ /* USER CODE BEGIN PV */ /* USER CODE END PV */ /* Private function prototypes -----------------------------------------------*/ void SystemClock_Config(void); static void MX_GPIO_Init(void); /* USER CODE BEGIN PFP */ /* USER CODE END PFP */ /* Private user code ---------------------------------------------------------*/ /* USER CODE BEGIN 0 */ /* USER CODE END 0 */ /** * @brief The application entry point. * @retval int */ int main(void) { /* USER CODE BEGIN 1 */ /* USER CODE END 1 */ /* MCU Configuration--------------------------------------------------------*/ /* Reset of all peripherals, Initializes the Flash interface and the Systick. */ HAL_Init(); /* USER CODE BEGIN Init */ /* USER CODE END Init */ /* Configure the system clock */ SystemClock_Config(); /* USER CODE BEGIN SysInit */ /* USER CODE END SysInit */ /* Initialize all configured peripherals */ MX_GPIO_Init(); /* USER CODE BEGIN 2 */ typedef struct menu{ char *menuName; struct menu *prev; struct menu *next; struct menu *parent; struct menu *child; }menu; // // menu menu1; // menu menu2; // //menu initMenu(char* name, menu* prev, menu* next, menu* parent, menu *child){ menu newMenu = (menu*)malloc(sizeof(menu));
View more

 

 

Error:

msalaz03_0-1721930555970.png

 

1 ACCEPTED SOLUTION

Accepted Solutions

You perhaps can't initialize a variable from a function call? You could do it in two stages, and it needs to be a pointer

...

menu *newMenu;

newMenu = (menu*)malloc(sizeof(menu));

...

newMenu->menuName = strmalloc("Test Menu");

 

calloc() might be more appropriate

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

View solution in original post

3 REPLIES 3

Where, exactly, is that error reported? it says line 111, but the posted code only goes up to line 92.

 

Are there any other errors and/or warning?

Show the actual compiler output - it (usually) gives more detail.

A complex system that works is invariably found to have evolved from a simple system that worked.
A complex system designed from scratch never works and cannot be patched up to make it work.

Hey Andrew, 

Appreciate the quick response, I just posted this but immediately noticed the issue I was just missing a '*' on my definition of my variable.

Thank you.

You perhaps can't initialize a variable from a function call? You could do it in two stages, and it needs to be a pointer

...

menu *newMenu;

newMenu = (menu*)malloc(sizeof(menu));

...

newMenu->menuName = strmalloc("Test Menu");

 

calloc() might be more appropriate

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..