Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Data structure problem

I made a structure like so:

struct ponto {
    int x;
    int y;
    int z;
};

1) Can I initialize the int's with a default value? int var = value; doesn't seem to work, compiler says "syntax error before '=' token" or something of sorts.

2) I need to work with several of these like in a array of structures, but I only know how many I need after the application starts up, after reading a file. How can I malloc this?

Thanks in advance

EDIT: So many answers, I'm grateful. Sadly I can only mark one

like image 273
Qosmo Avatar asked Jun 19 '26 02:06

Qosmo


1 Answers

a) You can initalise with

struct pronto p = {1,2,3};

In recent compilers (not sure how portable this is, think it's C99?)

b) You can allocate an array with malloc:

struct pronto *array = malloc(sizeof(struct pronto) * NUMBER);
like image 198
Flexo Avatar answered Jun 20 '26 18:06

Flexo