user2467899 user2467899 - 6 days ago 5
C Question

Use a struct in a header file [ C - Linux ]

I tried to use an external struct but when I compile my c code I obtained this message:

subscripted value is neither array nor pointer nor vector
.

Why?

messaggio.h

struct Request {
struct {
u_int data_len;
float *data_val;
} data;
bool_t last;
};
typedef struct Request Request;


main.c

#include "messaggio.h"

int main(void){
struct Request x;
x.data[0] = 4.6;
printf("%f\n",x.data[0]);
return 0;
}

Answer

The x.data is a struct, so you cannot use [] with it. Maybe you want x.data.data_val[0].

Try this code:

struct Request x;
x.data.data_len = 5; // initialize the length, use any value you need
x.data.data_val = (float *) malloc(x.data.data_len * sizeof(float));
x.data.data_val[0] = 4.6