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);
Related
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?
#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.
What is the proper way to access the struct variables in a function that is called by pthread_creation. This is how I am trying to do
void *add_first_quat(void *a){
struct thread1Struct *myArray = (struct thread1Struct *)a;
int i ;
for(i= *myArray>start; i < *myArray>end; i++){
sum+= *myArray>th1Array[i];
}
/* the function must return something - NULL will do */
return NULL;
}
And in my struct I am defining two variables and pointer to a globally defined array
struct thread1Struct{
int start = 0;
int end = 25;
int *th1Array = myArray;
};
This is how I am calling pthread_create function
(pthread_create(&inc_first_quater_thread, NULL, add_first_quat, (void*) &th1StrObj))
Why my code is not working? I am getting following errors
main.c: In function ‘add_first_quat’:
main.c:14:9: error: dereferencing pointer to incomplete type
for(i= *myArray>start; i < *myArray>end; i++){
^
main.c:14:18: error: ‘start’ undeclared (first use in this function)
for(i= *myArray>start; i < *myArray>end; i++){
^
main.c:14:18: note: each undeclared identifier is reported only once for each function it appears in
main.c:14:29: error: dereferencing pointer to incomplete type
for(i= *myArray>start; i < *myArray>end; i++){
^
main.c:14:38: error: ‘end’ undeclared (first use in this function)
for(i= *myArray>start; i < *myArray>end; i++){
^
main.c:15:9: error: dereferencing pointer to incomplete type
sum+= *myArray>th1Array[i];
^
main.c:15:18: error: ‘th1Array’ undeclared (first use in this function)
sum+= *myArray>th1Array[i];
^
main.c: At top level:
main.c:34:12: error: expected ‘:’, ‘,’, ‘;’, ‘}’ or ‘__attribute__’ before ‘=’ token
int start = 0;
^
First problem (syntax):
Try myArray->start or (*myArray).start or myArray[0].start. The first syntax would be my preference in this case.
Second problem:
dereferencing pointer to incomplete type
You need to provide the full declaration before referencing any fields.
Solve the problem by moving the full declaration of the struct to the top of your code file, or put it in a .h file that you #include in all source files that use the struct.
This: *myArray>start is not the proper syntax for accessing members of a pointer to a struct.
You could do this: (*myArray).start, which dereferences the pointer so *myArray is of type struct thread1Struct, then use . for member access.
The preferred way is myArray->start, where the -> operator does member access for a pointer to struct.
The problem lies in the way you're accessing the elements of the structure. Your expression *myArray>start makes no sense to the compiler. As you know, myArray is a pointer to a struct. You can access the data members in two ways :
You could use the indirection operator (eg. (*myArray).start )
You could use the arrow operator (eg. myArray->start)
This is how you access data members of any struct pointer. It does not pertain p-threads alone.
It's an extremely simple and useless piece of practice code I'm working with in what is starting to seem like and extremely useless book. I was doing a struct exercise, and upon code compilation I received a handful of errors.
Here's the offending code:
struct fish = {
const char *name;
const char *species;
int teeth;
int age;
};
void catalog(struct fish f)
{
printf("%s is a %s with %i teeth. He is %i.\n", f.name, f.species, f.teeth, f.age);
}
int main()
{
struct fish snappy = {"Snappy", "piranha", 69, 4};
catalog(snappy);
return 0;
}
This is the exact code from the book, minus the struct definition above catalog. I ended up just copy pasting because I started to suspect this book was just dead wrong. The book claimed that the above code should compile and run without the struct even being defined. I've tried putting the struct definition into a header file, and I've tried removing it or adding it to different parts of the code. I get the same exact errors:
snappy.c:8:13: error: expected identifier or ‘(’ before ‘=’ token
struct fish = {
^
snappy.c:16:26: error: parameter 1 (‘f’) has incomplete type
void catalog(struct fish f)
^
snappy.c: In function ‘main’:
snappy.c:24:12: error: variable ‘snappy’ has initializer but incomplete type
struct fish snappy = {"Snappy", "piranha", 69, 4};
^
snappy.c:24:12: warning: excess elements in struct initializer
snappy.c:24:12: warning: (near initialization for ‘snappy’)
snappy.c:24:12: warning: excess elements in struct initializer
snappy.c:24:12: warning: (near initialization for ‘snappy’)
snappy.c:24:12: warning: excess elements in struct initializer
snappy.c:24:12: warning: (near initialization for ‘snappy’)
snappy.c:24:12: warning: excess elements in struct initializer
snappy.c:24:12: warning: (near initialization for ‘snappy’)
snappy.c:24:17: error: storage size of ‘snappy’ isn’t known
struct fish snappy = {"Snappy", "piranha", 69, 4};
struct fish = { is wrong in struct declaration. It should be struct fish {. Remove = sign.
I am getting strange compilation errors from trying to create a struct in C.
Here is my code:
#define ALIGNMENT 8
/* rounds up to the nearest multiple of ALIGNMENT */
#define ALIGN(size) (((size) + (ALIGNMENT-1)) & ~0x7)
#define SIZE_T_SIZE (ALIGN(sizeof(size_t)))
#define BLK_HDR_SIZE ALIGN(sizeof(blockHdr))
typdef struct header {
size_t size;
blockHdr *next_p;
blockHdr *prior_p;
} blockHdr;
This is the error message:
mm.c:49:8: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘struct’
typdef struct header {
^
make: *** [mm.o] Error 1
I am baffled by this error. Is there something wrong with my code, or is there a more serious issue?
You have a typo in typdef. Next, you'll get an error about blockHdr not being defined.
The correct definition is:
typedef struct header {
size_t size;
struct header *next_p;
struct header *prior_p;
} blockHdr;
You can't use the typedef before it is declared. You have to use the actual structure name.
I think that that instead of
typdef struct header
it should be
typedef struct header