I have the following struct:
typedef struct vertex_tag{
int visited = 0;
int weight = FLT_MAX;
int prev;
}vertex_t;
It has the three members as indicated above.
I malloc the vertex like this:
vertex_t * vertex[G->vertices];
for(i=0; i < G->vertices; i++)
{
vertex[i] = (vertex_t*)malloc(sizeof(vertex_t));
}
So I create a matrix from the struct. I then call them throughout the function I created like this:
vertex[i]->visited
vertex[i]->weight
vertex[i]->prev
I keep getting the following error:
error: ‘vertex_t’ has no member named ‘visited’
error: ‘vertex_t’ has no member named ‘weight’
error: ‘vertex_t’ has no member named ‘prev’
Can anyone help me understand why I cant do this?
Okay so I can do it after the for loop in which I malloced it?
You'd do it better in the loop.
vertex_t *vertex[G->vertices];
for (i = 0; i < G->vertices; i++)
{
vertex[i] = malloc(sizeof(vertex_t));
vertex[i]->visited = 0;
vertex[i]->weight = FLT_MAX;
}
or according to Zeta's suggestion:
vertex_t vertex[G->vertices];
for (i = 0; i < G->vertices; i++)
{
vertex[i].visited = 0;
vertex[i].weight = FLT_MAX;
}
Related
This question already has answers here:
Passing 2D array of const size
(5 answers)
Closed 2 months ago.
This post was edited and submitted for review 2 months ago and failed to reopen the post:
Original close reason(s) were not resolved
I have a struct like:
struct oda {
char isim[10];
int id[1];
};
and 2d table created with this type:
struct oda* tab[X][Y];
This table should be allocated dynamically on memory, so if we have product placement on x and y tab[X][Y] should point to our struct, otherway value of pointer tab[X][Y] = NULL
I have created a fonction for init this table:
void init_tab_empty(struct oda** ptr_tab)
{
int i, j;
for (i = 0; i < X; i++) {
for (j = 0; j < Y; j++) {
ptr_tablo[i][j] = NULL;
}
}
}
But this is not working, i have:
Cannot assign a value of type void * to an entity of type struct oda
Can you help me please?
I played with *'s but i can't understand what can i do more
it seems correct for me but not working
If I understood you well, you want to pass a table to your function. Then the following change will make it okay:
void init_tab_empty(struct oda ***tab)
{
int i, j;
for (i = 0; i < DIM_X; i++) {
for (j = 0; j < DIM_Y; j++) {
tab[i][j] = NULL;
}
}
}
This tells the function that you will input a table of pointers to struct oda data type.
There, you have:
struct oda *my_tab[DIM_X][DIM_Y];
...
init_tab_empty(my_tab);
I'm here asking for your help, not because I have an error, but simply because of this solution that in my head seemed quite credible despite not working.
I basically have a structure to make an appointment and I created a variable of this temporary structure to change the values so that they are in ascending order, however when I show the query table in this case, but the queries appear in the order I registered them in the program.
My Struct:
typedef struct Consulta {
char nomeUtente[70];
int numSNS;
int dia;
int mes;
int ano;
int horasInicio;
int minutosInicio;
int horasFim;
int minutosFim;
} consulta;
My function that should order the values:
void organizarAgenda(int membroEscolhido, consulta agenda[][50][50], int clinicaSelecionada, int *nFuncionarios, int *nAgendas)
{
int i, j;
boolean substituir;
consulta temp;
for (i = 0; i < nAgendas[membroEscolhido]; i++)
{
for (j = 0; j < nAgendas[membroEscolhido]; j++)
if (agenda[j][membroEscolhido][clinicaSelecionada].ano > agenda[i][membroEscolhido][clinicaSelecionada].ano)
substituir = true;
if (agenda[j][membroEscolhido][clinicaSelecionada].ano == agenda[i][membroEscolhido][clinicaSelecionada].ano
&& (agenda[j][membroEscolhido][clinicaSelecionada].mes > agenda[i][membroEscolhido][clinicaSelecionada].mes))
substituir = true;
if (agenda[j][membroEscolhido][clinicaSelecionada].ano == agenda[i][membroEscolhido][clinicaSelecionada].ano
&& (agenda[j][membroEscolhido][clinicaSelecionada].mes == agenda[i][membroEscolhido][clinicaSelecionada].mes)
&& (agenda[j][membroEscolhido][clinicaSelecionada].dia > agenda[i][membroEscolhido][clinicaSelecionada].dia))
substituir = true;
if (agenda[j][membroEscolhido][clinicaSelecionada].ano == agenda[i][membroEscolhido][clinicaSelecionada].ano
&& (agenda[j][membroEscolhido][clinicaSelecionada].mes == agenda[i][membroEscolhido][clinicaSelecionada].mes)
&& (agenda[j][membroEscolhido][clinicaSelecionada].dia == agenda[i][membroEscolhido][clinicaSelecionada].dia
&& agenda[j][membroEscolhido][clinicaSelecionada].horasInicio >= agenda[i][membroEscolhido][clinicaSelecionada].horasInicio))
substituir = true;
if (substituir == true)
{
//Igualar a variavel temporario á variável agenda em i
temp.ano = agenda[i][membroEscolhido][clinicaSelecionada].ano;
temp.mes = agenda[i][membroEscolhido][clinicaSelecionada].mes;
temp.dia = agenda[i][membroEscolhido][clinicaSelecionada].dia;
temp.horasInicio = agenda[i][membroEscolhido][clinicaSelecionada].horasInicio;
temp.minutosInicio = agenda[i][membroEscolhido][clinicaSelecionada].minutosInicio;
temp.horasFim = agenda[i][membroEscolhido][clinicaSelecionada].horasFim;
temp.minutosFim = agenda[i][membroEscolhido][clinicaSelecionada].minutosFim;
//Igualar a variável agenda em i á variável agenda em j
agenda[i][membroEscolhido][clinicaSelecionada].ano = agenda[j][membroEscolhido][clinicaSelecionada].ano;
agenda[i][membroEscolhido][clinicaSelecionada].mes = agenda[j][membroEscolhido][clinicaSelecionada].mes;
agenda[i][membroEscolhido][clinicaSelecionada].dia = agenda[j][membroEscolhido][clinicaSelecionada].dia;
agenda[i][membroEscolhido][clinicaSelecionada].horasInicio = agenda[j][membroEscolhido][clinicaSelecionada].horasInicio;
agenda[i][membroEscolhido][clinicaSelecionada].minutosInicio = agenda[j][membroEscolhido][clinicaSelecionada].minutosInicio;
agenda[i][membroEscolhido][clinicaSelecionada].horasFim = agenda[j][membroEscolhido][clinicaSelecionada].horasFim;
agenda[i][membroEscolhido][clinicaSelecionada].minutosFim = agenda[j][membroEscolhido][clinicaSelecionada].minutosFim;
//Igualar a variável agenda em j á variavel temporaria
agenda[j][membroEscolhido][clinicaSelecionada].ano = temp.ano;
agenda[j][membroEscolhido][clinicaSelecionada].mes = temp.mes;
agenda[j][membroEscolhido][clinicaSelecionada].dia = temp.dia;
agenda[j][membroEscolhido][clinicaSelecionada].horasInicio = temp.horasInicio;
agenda[j][membroEscolhido][clinicaSelecionada].minutosInicio = temp.minutosInicio;
agenda[j][membroEscolhido][clinicaSelecionada].horasFim = temp.horasFim;
agenda[j][membroEscolhido][clinicaSelecionada].minutosFim = temp.minutosFim;
}
}
Thank you all in advance!
substituir is unitialized. You need to set it to false immediately after the for statement for j.
Your for loop for j is missing a trailing { so it will only iterate over the first if and not the others [as you would probably like]
As I mentioned in my comment, simplify [please ;-)]. Use pointers to simplify the code.
Your indexing is quite complex, so I can only guess at things.
I changed the comparison logic to something I understand.
Here's a simplified version. I just coded it, so it may not compile. But, it should give you some ideas how to proceed:
typedef struct Consulta {
char nomeUtente[70];
int numSNS;
int dia;
int mes;
int ano;
int horasInicio;
int minutosInicio;
int horasFim;
int minutosFim;
} consulta;
void
organizarAgenda(int membroEscolhido, consulta agenda[][50][50],
int clinicaSelecionada, int *nFuncionarios, int *nAgendas)
{
int i;
int j;
int lim = nAgendas[membroEscolhido];
int dif;
consulta temp;
for (i = 0; i < lim; i++) {
consulta *iptr = &agenda[i][membroEscolhido][clinicaSelecionada];
for (j = 0; j < lim; j++) {
consulta *jptr = &agenda[j][membroEscolhido][clinicaSelecionada];
do {
dif = iptr->ano - jptr->ano;
if (dif)
break;
dif = iptr->mes - jptr->mes;
if (dif)
break;
dif = iptr->dia - jptr->dia;
if (dif)
break;
} while (0);
if (dif <= 0)
continue;
temp = *iptr;
*iptr = *jptr;
*jptr = temp;
}
}
}
I'm [still] guessing but I think you can get a [significant] speedup by changing the for loop for j.
And, I think the for loop for i goes one too far.
So, consider:
for (i = 0; i < (lim - 1); i++) {
consulta *iptr = &agenda[i][membroEscolhido][clinicaSelecionada];
for (j = i + 1; j < lim; j++) {
consulta *jptr = &agenda[j][membroEscolhido][clinicaSelecionada];
UPDATE:
I didn't understand how a 3d array with only a 2d array assignment works int lim = nAgendas[membroEscolhido];
The value of nAgendas[membroEscolhido] is invariant across the function, so it can be "cached". I did this to simplify the code [mostly] but it also can help the compiler generate more efficient code.
I didn't notice the (-) in the middle of this line, and the -> works because it is a pointer pointing to the struct, right?
Right. The arrow operator (->) is a very powerful way to access individual struct members if you have a pointer to the given struct instance.
Note that the compiler's optimizer might be able to see that all the variables of the form: array[x][y][z].whatever could be reduced.
But, when we use intermediate pointers we're giving it a [better] clue as to what we want. And, the code is more readable by humans, so it has two good reasons to do it.
I don't understand why you put while (0)
This is a bit of a trick [of mine] to replace an if/else ladder with something that is cleaner.
It forms a "once through" loop. It would be the equivalent of:
while (1) {
if (something)
break;
if (something_else)
break;
break; // make the loop execute _only_ once
}
For a more detailed explanation, see my answer: About the exclusiveness of the cases of an if block
Hi im building a simple game in c (im new to the language). Im using the following structs:
typedef struct
{
int adjacent_mines;
bool revealed;
bool is_mine;
} Tile;
struct GameState
{
Tile tiles[NUM_TILES_X][NUM_TILES_Y];
};
typedef struct GameState GameState;
Im wondering how to properly call and set the structs? I have the following code where i would like to set state of each Tile.
void intialize_mines(){
for (int i =0; i < NUM_TILES_X; i++){
for (int j =0; j < NUM_TILES_Y; j++){
tiles[i,j].revealed = false;
}
}
}
But according to my console output i have done this incorrectly. How would i go about setting this correctly?
struct GameState just declares a type (just as int is just a type). You have to create a real struct in memory with GameState foo; similar to a normal variable (int foo;). And you can not access the contents of a struct without referencing the struct like foo.tiles. tiles on it's own is not known in this scope.
Afterwards you can access the struct with foo.tiles[i][j].revealed.
But to have access to this struct in your function you either have to pass it to the function as a pointer or declare the struct in filescope ( also called global). I would prefer the first version it is clearer an more function like.
Your function would look like this:
void intialize_mines( GameState *foo){
for (int i =0; i < NUM_TILES_X; i++){
for (int j =0; j < NUM_TILES_Y; j++){
foo->tiles[i][j].revealed = false; // -> is a special operator for pointers to structs. It's the same as (*foo).
}
}
}
the corresponding function call would be:
GameSate bar;
intialize_mines( GameState &bar);
You should also check how to use multidimensional arrays. You declared it correctly with two seperate [] but in your function you use [x,y] which is not correct in C. It would be the same as in the declaration tiles[i][j]
For a multi-dimensional array like tiles, you have to specify array subscript of each dimension within [] like this:
tiles[i][j].revealed = false;
This means that revealed belonging to jth column of ith row of tiles is set to false.
And you will have to define a structure variable of the type GameState before performing any operations on it.
GameState initGS;
void intialize_mines(){
for (int i =0; i < NUM_TILES_X; i++){
for (int j =0; j < NUM_TILES_Y; j++){
initGS.tiles[i][j].revealed = false;
}
}
}
You just missed to instantiate a GameState structure.
GameState gs;
void initialize_mines() {
for (int i =0; i < NUM_TILES_X; i++) {
for (int j =0; j < NUM_TILES_Y; j++) {
gs.tiles[i][j].revealed = false;
}
}
}
I am trying to create a three dimensional struct array using malloc in c on MSVC. It compiles without error but when i debug it it gives an error after initializing some elements.
declaration:
typedef struct table_info
{
unsigned long size;
char code[33];
char path[300];
}table_info;
table is a global variable and is defined as:
struct table_info ***table=NULL;
malloc and initialize table:
char garb[33] = { '\0' };
char garb_path[300] = { '\0' };
table = (table_info***)malloc(ROWS* sizeof(**table));
for (int m = 0; m < ROWS; m++)
{
table[m] = (table_info**)malloc(COLS* sizeof(*table[m]));
for (int j = 0; j < COLS; ++j)
{
table[m][j] = (table_info*)malloc(DEPTH * sizeof(table[m][j]));
for (int k = 0; k < DEPTH; ++k)
{
table[m][j][k].size = 0;
strcpy_s(table[m][j][k].code, sizeof(table[m][j][k].code), garb);
memcpy(table[m][j][k].path, garb_path, sizeof(garb_path));
}
}
}
Am I initializing it correctly? or what should I correct to make it work?
The size passed to malloc is incorrect in the following line:
table[m][j] = (table_info*)malloc(DEPTH * sizeof(table[m][j]));
sizeof(table[m][j]) is just sizeof(**table), which is sizeof(table_info *). It should be sizeof(table_info), or alternatively sizeof(*table_info[m][j]) or sizeof(***table_info).
You also don't need to cast the result of malloc, which is generally frowned upon today (at least in C). See this post for more info.
So the following should work (in C):
table[m][j] = malloc(DEPTH * sizeof(*table[m][j]));
Here is a piece of my code, I tried to make it simpler I am trying to assign a string to a pointer inside a struct that is inside an array, also I would like to initialize pointers to NULL so I can check whether or not there is already a doctor using the room...
I keep getting seg fault errors
I would appreciate any help
struct appointment{
char *SSN;
int status;//is appointment already taken?
};
struct room{
int doctorID;
char *doctorname;
struct appointment hours[10];
};
struct room clinic[5];
for(int i = 0; i < 5; i++){ //I was trying to initialize all pointers to NULL but didn't work
clinic[i].doctorID = 0;
for(int j = 0; i < 10; i++){
clinic[i].hours[j].status = 0;
}
}
for(int i = 0; i < 5; i++){
clinic[i].doctorname = malloc(sizeof(char) * 30); // Am I doing something wrong here?
*clinic[i].doctorname = "fernando";
printf("the name of the doctor on clinic %d is %s\n", i, clinic[i].doctorname
free(consultorios[i].medico);
}
return 0;
}
If you want to assign a string user strcpy instead.
Change your line
*clinic[i].doctorname = "fernando";
to
strcpy(clinic[i].doctorname, "fernando");