Structure Member Operator Error on Simple C Program - c

I'm studying about structures, and having some error. I cannot figure out why. Please help me out.
#include <stdio.h>
struct data{
int x;
float y;
float z;
} info;
info.x = 100;
struct data *ptr;
ptr = &info;
(*ptr).y = 5.5;
(*ptr).z = 1.0;
int main(void)
{
printf("The first member's value is %d.\n", info.x);
printf("The second member's value is %.1f.\n", info.y);
printf("The third member's value is %.1f.\n", info.z);
return 0;
}
The compiler error is
exercise112.c:10:5: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘.’ token
info.x = 100;
^
exercise112.c:13:1: warning: data definition has no type or storage class
ptr = &info;
^~~
exercise112.c:13:1: warning: type defaults to ‘int’ in declaration of ‘ptr’ [-Wimplicit-int]
exercise112.c:13:1: error: conflicting types for ‘ptr’
exercise112.c:12:14: note: previous declaration of ‘ptr’ was here
struct data *ptr;
^~~
exercise112.c:13:7: warning: initialization makes integer from pointer without a cast [-Wint-conversion]
ptr = &info;
^
exercise112.c:13:7: error: initializer element is not computable at load time
exercise112.c:15:7: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘.’ token
(*ptr).y = 5.5;
^
exercise112.c:16:7: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘.’ token
(*ptr).z = 1.0;
^
Did I do definition, declaration, initialization or anything wrong?
I cannot figure out what the error messages are saying. what do they
mean?

Related

Syntax error when adding a bool field to a struct (expected ‘:’, ‘,’, ‘;’, ‘}’ or ‘__attribute__’ before ‘=’ token) [duplicate]

This question already has answers here:
How to initialize a struct in accordance with C programming language standards
(16 answers)
Closed 4 years ago.
typedef struct {
char fielda[ 2 ][ FIELD_A_MAX + 1 ];
bool fieldb = false;
bool fieldc = false;
sem_t fieldd;
} Set;
I get the error:
error: expected ‘:’, ‘,’, ‘;’, ‘}’ or ‘__attribute__’ before ‘=’ token
bool fieldb = false;
What's my mistake here?
You cannot initialize fields inside a type definition. All initialization has to happen at the time of declaring a variable of the defined type:
typedef struct {
char fielda[ 2 ][ FIELD_A_MAX + 1 ];
bool fieldb;
bool fieldc;
sem_t fieldd;
} Set;
...
Set s = {.fieldb = false, .fieldc = false};
Unfortunately, the initializing sequence needs to be repeated each time. To avoid this, you could make a function to initialize Set:
void init_Set(Set* s) {
s->fieldb = false;
s->fieldc = false;
...
}
Now the initialization code is in a single place. You need to invoke this code for each Set structure that you allocate.
In C is not possible to initialize members in structures.

structure member accessing error

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<stdbool.h>
typedef struct {
int tos;
char stackarr[];
}STACK;
STACK paren;
paren.tos = -1;
void push()
{
paren.tos++;
paren.stackarr[tos] = '(';
}
This is giving me the following error:
error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘.’ token
paren.tos = -1;
^
In function ‘push’:
error: ‘tos’ undeclared (first use in this function)
I'm a beginner and have no idea why I'm getting this error. Any ideas?
You cannot perform an assignment outside a function; only initialization is allowed (demo):
STACK paren = {.tos = -1};
With this part out of the way, your approach is not going to work: flexible members, i.e. char stackarr[] at the end of the struct, do not work in statically allocated space; you need to use dynamic allocation with them. See this Q&A for an illustration of how to use flexible struct members.
Alternatively, you can pre-allocate the max number of elements to stackarr, i.e.
typedef struct {
int tos;
char stackarr[MAX_STACK];
} STACK;
STACK paren = {.tos = -1};
The obvious limitation to this approach is that the stack cannot grow past its preallocation limit.

how to use memalign with struct in c correctly?

I wrote the following code to allocate a chunk of memory for 10 of my struct barrier_t, but I keep getting the errors. What does it mean?and how to fix it? Thanks a lot!
test2.c:19:16: error: expected declaration specifiers or ‘...’ before ‘(’ token
test2.c:19:36: error: expected declaration specifiers or ‘...’ before numeric constant
test2.c:19:40: error: expected declaration specifiers or ‘...’ before ‘(’ token
struct barrier_t {
int count;
bool local_sense;
};
struct barrier_t* barrier = NULL;
posix_memalign((void **) &barrier, 32, (sizeof(struct barrier_t)) * 10);

Compiler Error when using a struct

I'm getting a strange compiler error initializing a struct.
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
struct RadarData
{
unsigned int messageID : 32;
unsigned int time : 32;
float az;
float el;
};
struct RadarData sendData;
sendData.az = 25;
sendData.el = 10;
sendData.messageID = 1;
sendData.time = 100;
This looks fine to me according to a few different tutorials, but on two different machines, I'm getting the following error when compiling:
testserver.c:15:9: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘attribute’ before ‘.’ token
testserver.c:16:9: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘attribute’ before ‘.’ token
testserver.c:17:9: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘attribute’ before ‘.’ token
testserver.c:18:9: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘attribute’ before ‘.’ token
Why am I getting this error?
sendData.az = 25;
Statements like this must be inside a function. If you want to initialize the struct, there's a different syntax for that:
struct RadarData sendData = { 25, 10, 1, 100 };
If I'm looking at your code right (and that's the complete relevant code), then you're placing statements outside of a function. That's not right.

help with pointers, files and functions in c

throw me 3 errors when compiling I am using Dev-C + +.
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
struct agenda{
char nombre [50];
char telefono[25];
char email[50];
}
struct nodo{
struct agenda dato;
struct nodo *proximo;
}
struct nodo *nuevonodo
int colavacia(struct nodo *)
struct nodo *creacola (struct nodo *, struct agenda);
void mostrar (struct nodo *);
void main()
{
struct nodo *pri=NULL, *ult=NULL;
struct agenda x;
printf ("ingrese nombre: ");
gets(x.nombre);
while (strcmpi(x.nombre, "fin"))
{
printf ("ingrese telefono: ");
gets (x.telefono);
printf ("ingrese mail: ");
gets(x.mail);
ult=creacola(ult,x);
if (pri==NULL) pri=ult; //si es la 1 pasada pongo en pri el valor del primer nodo
printf ("ingrese nombre: ");
gets(x.nombre);
}
if (colavacia(pri)==1)
{
printf ("No se ingresaron registros");getch();
}
else mostrar(pri);
}
struct nodo *nuevonodo()
{
struct nodo *p;
p=(struct nodo *)malloc(sizeof(struc nodo));
if(p==NULL)
{
printf ("memoria ram llena");
getch();
exit(0);
}
return p;
}
struct nodo *creacola(struct nodo *ult, struct agenda x)
{
struct nodo *p;
p=nuevonodo();
(*P).dato=x;
(*p).proximo=NULL;
if(ult!=NULL) (*ult).proximo=p; //si hay nodo anterior en prox pongo la direccion del nodo actual
return p;
}
int colavacia(struct nodo *pri)
{
if(pri==NULL) return 1;
else
return 0;
}
void mostrar (struct nodo *pri)
{
struct nodo *aux;
while(pri!=NULL)
{
printf("Nombre: %s - telefono: %s - Mail: %s \n",
pri->dato.nombre,pri->dato.telefono,pri->dato.mail);
aux=pri;
pri=(*pri).proximo;
free(aux);
}
getch();
}
You must end a struct definition with a semicolon:
struct agenda{
char nombre [50];
char telefono[25];
char email[50];
}; // <- HERE
Similarly for variable and function declarations:
struct nodo *nuevonodo;
int colavacia(struct nodo *);
And the return value of main has type int.
5587527.c:2:19: fatal error: conio.h: No such file or directory
I've commented out the offending #include and tried again
5587527.c:11:5: error: two or more data types in declaration specifiers
5587527.c:16:5: error: two or more data types in declaration specifiers
5587527.c:17:12: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘int’
cc1: warnings being treated as errors
5587527.c:22:10: error: function declaration isn’t a prototype
5587527.c:22:10: error: return type of ‘main’ is not ‘int’
5587527.c: In function ‘main’:
5587527.c:22:10: error: old-style function definition
5587527.c:29:9: error: implicit declaration of function ‘strcmpi’
5587527.c:29:9: error: nested extern declaration of ‘strcmpi’
5587527.c:35:15: error: ‘struct agenda’ has no member named ‘mail’
5587527.c:36:9: error: implicit declaration of function ‘creacola’
5587527.c:36:9: error: nested extern declaration of ‘creacola’
5587527.c:36:12: error: assignment makes pointer from integer without a cast
5587527.c:41:5: error: implicit declaration of function ‘colavacia’
5587527.c:41:5: error: nested extern declaration of ‘colavacia’
5587527.c:43:5: error: implicit declaration of function ‘getch’
5587527.c:43:5: error: nested extern declaration of ‘getch’
5587527.c: At top level:
5587527.c:48:18: error: function declaration isn’t a prototype
5587527.c: In function ‘nuevonodo’:
5587527.c:48:18: error: old-style function definition
5587527.c:51:40: error: ‘struc’ undeclared (first use in this function)
5587527.c:51:40: note: each undeclared identifier is reported only once for each function it appears in
5587527.c:51:46: error: expected ‘)’ before ‘nodo’
5587527.c: At top level:
5587527.c:61:18: error: conflicting types for ‘creacola’
5587527.c:36:13: note: previous implicit declaration of ‘creacola’ was here
5587527.c: In function ‘creacola’:
5587527.c:65:11: error: ‘P’ undeclared (first use in this function)
5587527.c: In function ‘mostrar’:
5587527.c:85:54: error: ‘struct agenda’ has no member named ‘mail’
I've added a ; to lines 10, 14, 16, 17 and tried again
cc1: warnings being treated as errors
5587527.c:22:10: error: function declaration isn’t a prototype
5587527.c:22:10: error: return type of ‘main’ is not ‘int’
5587527.c: In function ‘main’:
5587527.c:22:10: error: old-style function definition
5587527.c:29:9: error: implicit declaration of function ‘strcmpi’
5587527.c:29:9: error: nested extern declaration of ‘strcmpi’
5587527.c:35:15: error: ‘struct agenda’ has no member named ‘mail’
5587527.c:43:5: error: implicit declaration of function ‘getch’
5587527.c:43:5: error: nested extern declaration of ‘getch’
5587527.c: At top level:
5587527.c:48:18: error: function declaration isn’t a prototype
5587527.c:48:18: error: ‘nuevonodo’ redeclared as different kind of symbol
5587527.c:16:18: note: previous declaration of ‘nuevonodo’ was here
5587527.c: In function ‘nuevonodo’:
5587527.c:48:18: error: old-style function definition
5587527.c:51:40: error: ‘struc’ undeclared (first use in this function)
5587527.c:51:40: note: each undeclared identifier is reported only once for each function it appears in
5587527.c:51:46: error: expected ‘)’ before ‘nodo’
5587527.c: In function ‘creacola’:
5587527.c:65:11: error: ‘P’ undeclared (first use in this function)
5587527.c: In function ‘mostrar’:
5587527.c:85:54: error: ‘struct agenda’ has no member named ‘mail’
Fix main in line 22 (to int main(void))
5587527.c: In function ‘main’:
5587527.c:29:9: error: implicit declaration of function ‘strcmpi’
5587527.c:29:9: error: nested extern declaration of ‘strcmpi’
5587527.c:35:15: error: ‘struct agenda’ has no member named ‘mail’
5587527.c:43:5: error: implicit declaration of function ‘getch’
5587527.c:43:5: error: nested extern declaration of ‘getch’
5587527.c: At top level:
5587527.c:48:18: error: function declaration isn’t a prototype
5587527.c:48:18: error: ‘nuevonodo’ redeclared as different kind of symbol
5587527.c:16:18: note: previous declaration of ‘nuevonodo’ was here
5587527.c: In function ‘nuevonodo’:
5587527.c:48:18: error: old-style function definition
5587527.c:51:40: error: ‘struc’ undeclared (first use in this function)
5587527.c:51:40: note: each undeclared identifier is reported only once for each function it appears in
5587527.c:51:46: error: expected ‘)’ before ‘nodo’
5587527.c: In function ‘creacola’:
5587527.c:65:11: error: ‘P’ undeclared (first use in this function)
5587527.c: In function ‘mostrar’:
5587527.c:85:54: error: ‘struct agenda’ has no member named ‘mail’
Changed strcmpi in line 29 to strcmp
5587527.c: In function ‘main’:
5587527.c:35:15: error: ‘struct agenda’ has no member named ‘mail’
cc1: warnings being treated as errors
5587527.c:43:5: error: implicit declaration of function ‘getch’
5587527.c:43:5: error: nested extern declaration of ‘getch’
5587527.c: At top level:
5587527.c:48:18: error: function declaration isn’t a prototype
5587527.c:48:18: error: ‘nuevonodo’ redeclared as different kind of symbol
5587527.c:16:18: note: previous declaration of ‘nuevonodo’ was here
5587527.c: In function ‘nuevonodo’:
5587527.c:48:18: error: old-style function definition
5587527.c:51:40: error: ‘struc’ undeclared (first use in this function)
5587527.c:51:40: note: each undeclared identifier is reported only once for each function it appears in
5587527.c:51:46: error: expected ‘)’ before ‘nodo’
5587527.c: In function ‘creacola’:
5587527.c:65:11: error: ‘P’ undeclared (first use in this function)
5587527.c: In function ‘mostrar’:
5587527.c:85:54: error: ‘struct agenda’ has no member named ‘mail’
...
I've done a few correct errors / recompile / get more errors for you. Now it's your turn.

Resources