Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

I want to make a struct that includes a matrix of string

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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);
    }

>Solution :

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);
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading