I've been working on this project for a while and I wanted to test it, but I keep getting this error and I have no idea what to do and I am very confused. Here is my code:
typedef struct{
int nr_pages;
int id;
int a,b;
int aux;
}information;
int main(){
int i;
i = information.b;
//and more stuff happens
}
The error that I am always getting is "Expected expression before 'information'" exactly where I declare i = information.b
What am I doing wrong?
You need to instantiate the structure before using it. Try:
typedef struct{
int nr_pages;
int id;
int a,b;
int aux;
}information;
int main(){
information info;
info.b = 0;
info.a = 0;
...
etc
...
int i = information.b;
//and more stuff happens
}
You declare information as a type, not a variable. This is what the typedef is for.
And i = ... in the C terminology is not a declaration, but an assignment.
Related
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int busqueda_indexada(int a[], int n, int x) {
int elementos[3]; int indice[3];
int g; int i;
int set=0; int ind=0;
for (i=0; i<n-1; i+=3) {
elementos[ind].nombre=a[i]);
elementos[ind].indice = i;
i+=3;
ind++;
}
if (x<elementos[0].boleto) {
return -1;
} else {
for (i=1; i<g-1; i++)
if (x<elementos[i].elem) {
int ini = elementos[i-1].indice;
int fin = elementos[i].indice;
set = 1;
break;
}
}
if (set==0) {
int ini = elementos[G-1].indice;
int fin = n-1;
}
}
struct elementos {
int indice;
char nombre[100];
int boleto;
} elementos a[3];
int main(int argc, char *argv[]) {
struct elementos a[3] = {"marco", 1, "sin asignar", 2, "pedro", 3};
printf("%s y %d", a[2].nombre, a[2].boleto);
busqueda_indexada(a, n, x)
return 0;
}
I don't know how the indexed search can read my structure. I tried everything and always shows
[Error] request for member '' in something not a structure or union
every time I tried to call a structure. Maybe I defined bad my struct or I call it in the wrong way?
With elementos[ind].nombre You try to access a field on elementos[ind], but that itself is a int. .something in only allowed, if elementos[ind] would eithet be a struct or an union.
You use elementos for two different things:
In busqueda_indexada() for a local array of int;
Outside any function for a structure.
The error (among a lot more) is given the first time for elementos[ind].nombre. This is the indth element of the local array, an int, which is clearly no structure.
Please raise the warning level of your compiler to the maximum and correct your code until all errors and warnings are gone.
Use descriptive names for structures and variables.
//Program to understand Nested structs in C
#include <stdio.h>
typedef union test {
float tet;
struct {
int bite;
} p;
} U_test;
//union test U_test;
struct U_test.p = {.bite=150};
int main()
{
printf("%d\n", U_test.p.bite);
U_test.tet = 0.0;
printf("%d", U_test.p.bite);
return 0;
}
Here is the error code I keep seeing below. Not sure I am doing something wrong here or am I typing something wrong?Please advise. Thanks in advance.
error: expected identifier or ‘(’ before ‘.’ token
struct U_test.p = {.bite=150};
struct U_test.p = {.bite=150};
That is not the correct syntax to create a variable of the defined union type. It should be:
U_test u_test = { .p.bite = 150 };
After making that change all the references in main which are currently U_test need to be changed to u_test.
Here is the full program corrected:
#include <stdio.h>
typedef union test{
float tet;
struct{
int bite;
}p;
}U_test;
//union test U_test;
U_test u_test = { .p.bite=150 };
int main()
{
printf("%d\n", u_test.p.bite);
u_test.tet = 0.0;
printf("%d", u_test.p.bite);
return 0;
}
Hi,
I'm using Diab 5.8.0 as a compiler for my C source code.
I've recognized that if I have a program like this:
typedef struct
{
int field_1;
int field_2;
int field_3;
} Struct_Type;
int main()
{
Struct_Type temp_st = {1,1,1};
return temp_st.field_1;
}
It seems to be converted to this (the return value is 0 instead of 1)
typedef struct
{
int field_1;
int field_2;
int field_3;
} Struct_Type;
int main()
{
Struct_Type temp_st;
return temp_st.field_1;
}
There is same problem with array initialization,
This:
int main()
{
int array[3]={1,1,1};
return array[0];
}
is converted to this:
int main()
{
int array[3];
return array[0];
}
I think the issue is from the optimization of Diab Compiler so I do not use any optimize-flag when compile code but the issue is still there.
Could you help me if you know the root cause of this and the solution if I want to init value for struct and array right at the declaration?
I have created a file msgbuf.h which is as follows:
//msgbuf.h
typedef struct msgbuf1
{
long mtype;
M1 *m;
} message_buf;
typedef struct msgclient
{
int msglen;
int msgtype;
char cp[100];
}M1;
And a program as check.c. Below prog is giving error that there is no M1. Why is it so? What mistake am I doing?
I think the contents of file "msgbuf.h" should get copied in the prog check.c and the program should run fine. Please let me know this.
//check.c
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include"msgbuf.h"
int main()
{
message_buf *sbuf;
sbuf=malloc(sizeof(sbuf));
sbuf->m=malloc(sizeof(M1));
sbuf->m->msglen=10;
printf("\n%d",sbuf->m->msglen);
printf("\n %d",sizeof(sbuf->m));
return 0;
}
Thanks :)
Simple, declare M1 before message_buf; .
typedef struct msgclient
{
int msglen;
int msgtype;
char cp[100];
}M1;
typedef struct msgbuf1
{
long mtype;
M1 *m;
} message_buf;
And read the keltar's comments below the question too.
You should declare M1 before using it:
//msgbuf.h
typedef struct msgclient
{
int msglen;
int msgtype;
char cp[100];
}M1;
typedef struct msgbuf1
{
long mtype;
M1 *m;
} message_buf;
I am unsure if my use of malloc is correct, but what bother's me is the inability to pass the struct into the put_age() function pointer. It looks right to me but apparently it isn't.
#include <stdio.h>
#include <stdlib.h>
typedef struct{
int age;
// NPC methods
int (*put_age)(NPC *character, int age);
} NPC;
////////////////////////////////////
int set_age(NPC *character, int age);
int main(){
NPC *zelda = malloc(sizeof(NPC));
zelda->put_age = set_age;
zelda->put_age(zelda, 25);
printf("Zelda's age is %d\n", zelda->age);
return 0;
}
int set_age(NPC *character, int age){
character->age = age;
return 0;
}
COMPILER OUTPUT:
$ gcc ~/test.c
/test.c:7:21: error: expected ‘)’ before ‘*’ token
/test.c:8:1: warning: no semicolon at end of struct or union
/test.c: In function ‘main’:
/test.c:16:8: error: ‘NPC’ has no member named ‘put_age’
/test.c:17:8: error: ‘NPC’ has no member named ‘put_age’
Your problem is that NPC isn't the name of a type until the declaration of the struct typedef is complete. You can change this by giving the struct a name, e.g.
typedef struct tagNPC {
int age;
// NPC methods
int (*put_age)(struct tagNPC *character, int age);
} NPC;
or
typedef struct tagNPC NPC;
struct tagNPC {
int age;
// NPC methods
int (*put_age)(NPC *character, int age);
};
I don't think you can use typedef "NPC" inside the struct def. That's because until the compiler has not seen the closing "}" , it has no idea what NPC is.
Please try changing:
typedef struct{
int age;
// NPC methods
int (*put_age)(NPC *character, int age);
} NPC;
to:
typedf struct node_npc NPC;
struct node_npc
{
int age;
int (*put_age)(NPC *character, int age);
};
Try changing this:
int set_age(NPC *character, int age){
zelda->age = age;
return 0;
}
To:
int set_age(NPC *character, int age){
character->age = age;
return 0;
}
In set_age(), your variable's name is character, not zelda, so the code should be:
int set_age(NPC *character, int age){
character->age = age;
return 0;
}
I had that problem, when I had a constant defined someware in my code that was named like a member of a struct. Ex.
#define N 10
struct my_struct{
int N;
};