Lpaulson
Lpaulson

Reputation: 229

Initializing pointer member within structure

I'm trying to write a module that will read a specific address value from a micro controller, the initialization code has compiled on its own but when I try to compile it within a structure it seem to complain. Are there restriction on initializing pointers within a structure?

#include <stdio.h>
#include <esper.h>
#include <stdlib.h>
#include <stdint.h>
#include "lmk04800.h"

#define NUM_MSS_GPIO 32

#define SEL_NIM     3
#define SOURCE_SEL  4
#define SEL_EXT     5
#define CLK0_3_EN   6
#define CLK4_7_EN   7
#define CLK8_11_EN  8
#define CLK12_15_EN 9
#define CLK16_19_EN 10
#define CLK20_23_EN 11
#define LMK_SYNC    12

typedef struct {
  int fd_gpio[NUM_MSS_GPIO];
  uint8_t data_gpio[NUM_MSS_GPIO];
  uint32_t esata_freqCounter;
  uint32_t ext_freqCounter;
  uint32_t esata_trgCounter;
  uint32_t ext_trgCounter;

  tLMK04800 settings;
  volatile uint32_t *freqCounter_addr;
  volatile uint32_t *trgCounter_addr;
  volatile uint32_t *manSync_addr;
  freqCounter_addr = (volatile uint32_t *)0x30000000;
  trgCounter_addr = (volatile uint32_t *)0x30000100;
  manSync_addr = (volatile uint32_t *)0x30000200;

 } tESPERModuleTest;

The compilation error I get is:

test/mod_test.h:32: error: expected specifier-qualifier-list before 'freqCounter_addr'

Upvotes: 0

Views: 811

Answers (1)

Keith Thompson
Keith Thompson

Reputation: 263617

You can't define initial values for struct members when you define the type. It could be useful if it were permitted, and the initial values would be applied automatically to any objects of the type. C just doesn't permit it.

You can only initialize objects.

You've defined a type named tESPERModuleTest. If you want to define and initialize an object of that type, you can do something like this after defining the type (untested code):

tESPERModuleTest obj = {
    .freqCounter_addr = (volatile uint32_t *)0x30000000,
    .trgCounter_addr = (volatile uint32_t *)0x30000100,
    .manSync_addr = (volatile uint32_t *)0x30000200
};

(The other members will be default zero-initialized, or you can provide values for them.)

Upvotes: 1

Related Questions