0

My goal with this piece of code is to create an array of 1000 positions, containing a struct which contains an int (to be used as a counter) and a nested struct initialized as an array of 100 positions. Am I designing this correctly?

As in, I'm trying to achieve a bidimensional table of 1000 columns by 100 rows, where these 100 rows each have the before mentioned int to be used as a counter/index variable, and each position in the 100 rows array to be comprised by the nested struct! This is what I got so far:

#define DATA_MAX 1000
#define MAX_CHAR_TIPO_MOV 60
#define MAX_CHAR_DESCRICAO 60
#define MAX_MOVIMENTOS 100
#define BUFFLEN 1024

char buffer[BUFFLEN];

typedef struct{
    int montante;
    int data;
    int periodicidade;
    char tipo[MAX_CHAR_TIPO_MOV];
    char descricao[MAX_CHAR_DESCRICAO];
}sistema;

typedef struct{
int indice;
struct sistema tabela[MAX_MOVIMENTOS]; /* Compiler gives me an error here: array type has incomplete element type */
}movimentos;

movimentos c[DATA_MAX];

/* Function to initialize arrays/structs in order to eliminate junk */


void inicializarfc(movimentos c[])
{
    int i, j;
//percorre o vector estrutura
for(i=0; i<DATA_MAX; i++)
    for(j=0; j<MAX_MOVIMENTOS; j++)
{
    c[i].[j].data = -1;
    c[i].[j].montante = -1;
    c[i].[j].periodicidade = -1;
    memset((c[i].[j].tipo), ' ', sizeof(c[i].[j].tipo));
    memset((c[i].[j].descricao), ' ', sizeof(c[i].[j].descricao));
   }
}

If indeed it is possible to create what I'm asking, how should I go about to access the structure members? Compiling in Codeblocks 10.05 in W7 using GCC.

2 Answers 2

1

You don't need to use the struct keyword in front of a typedef.

Just say:

sistema tabela[MAX_MOVIMENTOS];

To access members, just say:

movimentos m;
/* initialize data */
int x = m.tabela[0].montante; // accesses montante field of tabela[0]
Sign up to request clarification or add additional context in comments.

Comments

0

Let's take a look at your declarations:

typedef struct{
   int montante;
   int data;
   int periodicidade;
   char tipo[MAX_CHAR_TIPO_MOV];
   char descricao[MAX_CHAR_DESCRICAO]; 
}sistema;  

typedef struct{ 
   int indice; 
   struct sistema tabela[MAX_MOVIMENTOS];
}movimentos;

The problem is that you're looking for a type named struct sistema, but you haven't declared a type like that; instead, you declared an anonymous struct type and created a typedef name sistema for it. To access a type named struct sistema, you would have to provide the struct tag in the definition:

struct sistema { ... };

The struct tag sistema and the typedef name sistema live in different namespaces; you could write

typedef struct sistema { ... } sistema;

and use both sistema and struct sistema interchangeably, although that would probably be confusing.

In this case, the simplest thing to do is to change

struct sistema tabela[MAX_MOVIMENTOS];

to

sistema tabela[MAX_MOVIMENTOS];

in the movimentos struct type.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.