2024-07-25 11:03 AM
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));
Error:
Solved! Go to Solution.
2024-07-25 11:16 AM - edited 2024-07-25 11:18 AM
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
2024-07-25 11:09 AM
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.
2024-07-25 11:13 AM
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.
2024-07-25 11:16 AM - edited 2024-07-25 11:18 AM
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