Question

alsa_test.h header file includes the following code:

struct alsa_device;
typedef struct alsa_device alsa_device;    
alsa_device *create_alsa_array(size_t n);

alsa_test.c file includes:

struct alsa_device 
{
 int card;
 int dev;
 char info[80];
};
typedef struct alsa_device alsa_device;    

alsa_device *create_alsa_array(size_t n) { 
  alsa_device *new = malloc(n * sizeof *new); 
  return new; 
};

and my main.c file has the following:

#include "alsa_test.h"

int main (int argc, char *argv[]) {
     alsa_device *devices;
     create_alsa_array(devices,50);
     devices[0]->card=1;
}

and I get the following error

error: invalid use of undefined type 'struct alsa_device'

any ideas why ?

Was it helpful?

Solution 2

You have 2 choices:

  1. You wan't to expose types internal structure: you need then to define the struct in the interface (i.e. the alsa_test.h file).
  2. You don't want to expose types internal structure: add functions in the interface to access structures fields

add in alsa_test.h

void set_alsa_card(struct alsa_device * alsa_dev_list, int dev_id, int card);

and in alsa_test.c

void set_alsa_card(struct alsa_device * alsa_dev, int dev_id, int card)
{
    (alsa_dev + dev_id)->card = card;
}

main.c :

#include "alsa_test.h"

int main (int argc, char *argv[]) {
     alsa_device *devices;
     devices = create_alsa_array(50);
     set_alsa_card(devices, 0, 1);
}

OTHER TIPS

Your problem is in main(), here

     devices[0]->card=1;

At this time devices is a pointer to a struct with no definition.

If you want to keep the structure undefined, define a function in alsa_test.c that takes a pointer to it and a integer (remember to add a prototype in your header file)

void setcard(struct alsa_device *dst, size_t index, int card) {
    dst[index].card = card;
}

and call it from main()

setcard(devices, 0, 1); // set card of device 0 to 1
setcard(devices, 1, 42); // set card of device 1 to 42
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top