Defining struct containing array in C51 not allowed? - c

Structs containing arrays in C51 are not allowed? After defining a simple structure in my C51 program,
struct RingBuffer
{
int zero;
int size;
int capacity;
char data[10];
};
I got error:..\SOURCE\MYRINGBUFFER.H(25): error C141: syntax error near '['. It is clear that error is on the line with a char array defined in the struct(no errors after commenting).
Am I doing something wrong here? If not, is there anyway I can achieve what I'm attempting to do?
EDIT: All the code.
#ifndef __MY_RING_BUFFER_H__
#define __MY_RING_BUFFER_H__
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct RingBuffer
{
int zero;
int size;
int capacity;
char data[10];
};
#endif

I figured out what's wrong. data is a keyword in C51.

Related

expected specifier-qualifier-list before 'typedef' - one H file with a typedef of a struct while its fields are written in another H

i'm receiving the next error:
expected specifier-qualifier-list before 'typedef' (in challenge_room_system_fields.h):
This is challenge_system.h:
typedef struct SChallengeRoomSystem
{
#include "challenge_room_system_fields.h"
} ChallengeRoomSystem;
This is challenge_room_system_fields.h:
#include "challenge_system.h"
typedef struct SChallengeRoomSystem //this is where i get the error
{
char* system_name;
struct Challenge* challenges;
int numOfChallenges;
struct ChallengeRoom* rooms;
int numOfRooms;
int timeOfLastAction;
} ChallengeRoomSystem;
Can someone help me figure out what's wrong?
I know its not the best way to deal with structs but as a school assignment i'm required not to change the challenge_system.h. I am allowed to change only challenge_room_system_fields.h
Thanks in advance....
i should've just write the following inside challenge_room_system_fields.h :
char* system_name;
struct Challenge* challenges;
int numOfChallenges;
struct ChallengeRoom* rooms;
int numOfRooms;
int timeOfLastAction;
The reason is that the #include by definition takes the included file and Copy-Pastes it instead of the line I wrote #include...
so eventually challenge_system.h will look as follows:
typedef struct SChallengeRoomSystem
{
// this is where previously i had: #include "challenge_room_system_fields.h"
//now i will have the following:
char* system_name;
struct Challenge* challenges;
int numOfChallenges;
struct ChallengeRoom* rooms;
int numOfRooms;
int timeOfLastAction;
} ChallengeRoomSystem;
And now it works:)

Compiling: [Error] array type has incomplete element type

I'm having a problem with a program I am trying to complete, as far as I was aware, i had defined the structs correctly, but when compiling I get this error code 3 times, once for each struct
Error message:
34 18 - [Error] array type has incomplete element type
My code is shown below, any advice would be extremely appreciated, there's approx 300 lines of code in the full program, let me know if you need to see it, I didn't want to overload you guys with it. Thanks again.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void fileRead();
void readGrades();
void readResults();
void pointCompute();
void sortGrades();
void print();
typedef struct{
char subject[15];
char level[2];
char grade[3];
int points[3];
} temp;
typedef struct{
char subject[15];
char level[2];
char grade[3];
int points[3];
} grades;
typedef struct{
char subject[15];
char level[2];
char grade[3];
int points[3];
} results;
struct temp temp[50];
struct grades grades[50];
struct results results[50];
As they are typedefs, remove the struct from the variable definition lines. You put struct if the types are actual structs, not typedefs. Also, the variable names cannot be the same as the type names.
temp temp_var[50];
grades grades_var[50];
results results_var[50];

Find packed size of a structure

Is there a way to find the packed size of a structure defined and declared without packed attribute in GCC compiler?
Example:
struct Name
{
int a;
char ch;
}
any function or macro like get_packed_size(Name) should return 5
Define your struct using a macro that provides the required information. For example (though there are other possible implementations):
#define DEFINE_STRUCT_WITH_KNOWN_PACKED_SIZE(StructName, StructBody)\
struct StructName StructBody\
struct __attribute__ ((__packed__)) StructName##_packed StructBody
#define GET_PACKED_SIZE(StructName) sizeof (struct StructName##_packed)
DEFINE_STRUCT_WITH_KNOWN_PACKED_SIZE(Name, {
int a;
char ch;
};)
#include <stdio.h>
int main() {
printf("%lu", GET_PACKED_SIZE(Name));
}
No, there's no way... you only have sizeof(), and you need to take care of the padding...

New to C... struct can't find a variable I identified

I'm new to this whole C thing, but I keep getting this error with my code
UArray2.c:19:error: request for member ā€˜iā€™ in something not a structure or union
It's obviously the uarray.i in my main function, but I don't get why it isn't seeing it.
This is my .h file. Not too interesting...
//UArray2.h
#include <stdlib.h>
#include <stdio.h>
#ifndef UARRAY2_INCLUDED
#define UARRAY2_INCLUDED
#define T UArray2_T
typedef struct T *T;
#undef T
//#undef UARRAY2_INCLUDED //undef?
#endif
This is my .c file. Pretty simple stuff.
//UArray.c
#include <stdlib.h>
#include <stdio.h>
#include "UArray2.h"
#define T UArray2_T
struct T{
int i;
};
int main()
{
UArray2_T uarray;
uarray.i=0;
return 0;
}
#undef T
So, does anyone have any idea as to why I'm getting this compile error? It's likely something stupid that I did.
In the header file you have
typedef struct T *T;
This means that when you declare the variable uarray you are actually declaring a pointer. So you should initialize the i member as
uarray->i = 0;
This will however most likely crash, as the pointer is uninitialized and can point to any location in memory. Either allocate memory for the pointer
UArray2_T uarray = malloc(sizeof(*uarray));
Or make it point to another structure
struct UArray2_T real_uarray;
UArray2_T uarray = &real_uarray;
I think there is a problem with the initialization as you are using the pointer in the header file.
typedef struct T *T;
You are actually pointing to the memory location by declaring uarray.
Try to rectify this error.

Having trouble accessing struct elements returned by a function - dereferencing pointer to incomplete type

I am new to C.This are the files and codes that I am working on. I am trying to call a function (refineMatch) implemented in a separate file from the main function. function refineMatch returns a struct. I am having problems in compiling the code which is related to accessing elements in the returned struct. The compile error occurs in main.c file. Code below shows where the error happens.
refine.h
#include <cv.h>
#include <cxcore.h>
#include <highgui.h>
struct matchingpair{
CvPoint p1, p2;
};
struct matchingpair_array{
struct matchingpair* elements;
int length;
};
struct matchingpair_array *refineMatch(struct matchingpair* pairs,int pointcount, int bestpair);
refine.c
#include "refine.h"
#include "utils.h"
#include <stdlib.h>
struct matchingpair_array *refineMatch(struct matchingpair* pairs,int pointcount, int bestpoint){
struct matchingpair_array refinedPairs;
refinedPairs.elements=malloc(incount*sizeof(struct matchingpair));
int *in=malloc(pointcount*sizeof(int)), i=0,incount=8;
// several statements - including filling in[] with data
for(i=0;i<incount;i++){
refinedPairs.elements[i]=pairs[in[i]];
fprintf(stderr,"%d\n",in[i]);
}
refinedPairs.length=incount;
free(in);
// several other free() operations non include refinedPairs or elements
return &refinedPairs;
}
main.c
#include "refine.h"
#include <stdio.h>
int main( int argc, char** argv ){
struct matchingpair* pairs;
int matchcount=0,bestpair;
pairs=(struct matchingpair*)malloc(pairArrSize*sizeof(struct matchingpair));
//values are assigned to pairs, matchcount and bestpair
struct matcingpair_array* result=(struct matcingpair_array*)refineMatch(pairs,matchcount,bestpair); /*(casting removed this warining)
warning: initialization from incompatible pointer type*/
fprintf(stderr,"%d \n",result->length); //error: dereferencing pointer to incomplete type
//some other code
}
Please explain me what I am doing wrong here. I am using gcc.
Thank you.
refineMatch() does not return a struct. It returns a pointer to a struct matchingpair_array.
and matcingpair_array is not the same as mathcingpair_array: it is missing an h

Resources