I want to makes a struct that include a matrix of string.
Like |0|1|2|..|10 each one of this position should have strings like this: hello, world, 1234, ...
I want to add string unless I get the limit (= SO_BLOCK_SIZE), so I create a function to know how many string I already added. I got some errors like these:
error: expected declaration specifiers or ‘...’ before numeric constant #define SO_REGISTRY_SIZE 10
note: in expansion of macro ‘SO_REGISTRY_SIZE’ char (*matrice)(SO_REGISTRY_SIZE);
warning: no semicolon at end of struct or union
error: ‘libroMastro’ {aka ‘struct libroMastro’} has no member named ‘matrice’ if((libro->matrice[i][j]) == NULL)
Here is my code:
#include <stdio.h>
#include <stdlib.h>
#define BUF_SIZE 64
#define SO_REGISTRY_SIZE 10
#define SO_BLOCK_SIZE 5
typedef struct libroMastro{
char (*matrice)(SO_REGISTRY_SIZE);
}libroMastro;
int whatIndex(libroMastro *libro){
int i = 0;
int j = 0;
for(i; i < SO_REGISTRY_SIZE; i ){
for(j; j < SO_BLOCK_SIZE; j ){
if((libro->matrice[i][j]) == NULL)
return j;
}
}
return j;
}
int main(){
libroMastro *libro;
whatIndex(libro);
}
CodePudding user response:
Your code is invalid in may places.
I would implement it this way:
typedef struct libroMastro
{
size_t nmessages;
char *matrice[];
}libroMastro;
libroMastro *addString(libroMastro *lm, const char *str)
{
if(str)
{
size_t newsize = lm ? lm -> nmessages 1 : 1;
lm = realloc(lm, sizeof(*lm) newsize * sizeof(lm -> matrice[0]));
if(lm)
{
if((lm -> matrice[newsize - 1] = strdup(str)))
{
lm -> nmessages = newsize;
}
else
{
lm -> nmessages = newsize - 1;
}
}
}
return lm;
}
size_t how_many(libroMastro *lm)
{
if(lm) return lm -> nmessages;
}
int main(){
libroMastro *libro = NULL;
libro = addString(libro, "Hello");
libro = addString(libro, "World");
libro = addString(libro, "Next");
printf("Libro contains %zu strings\n", libro -> nmessages);
}