my gcc (9.1.0) struggle with following structure definition
struct foo_v1 : foo_v2 { a; };
i guess it could be a matter of coding version such as ansi or c99.
reading 9.4.0 i could not find <struct a : b { c; };> notation.
please could anyone give me a hint where to find standard defining such a notation or how to compile with gcc.
have a great day
how to compile with gcc.
It is not valid C syntax. It is not possible to compile it with gcc as a C source code. It is also not a common extension in common C compilers.
It may be possible to compile the following with a C++ compiler (which makes the line compile-able after adding two lines and adding -xc++ to gcc):
struct foo_v2 { int stuff; };
#define a int variable
struct foo_v1 : foo_v2 { a; };
The presented line in the question, on its own, is not valid C++ code anyway.
Related
While writing a simple program where I had to create structs for saving information about a film and a film director, both of those had the same variables inside but with different names
struct Regista
{
char nome[30];
char cognome[30];
int nascita;
int doIexist;
};
typedef struct Regista regista;
struct Film
{
char titolo[30];
char reg[30];
int anno;
int doIexist;
};
I had created an array for both and I wanted to pass those as argument in different functions in order to add elements in those array, and now the question I have: I wrongly wrote this:
int insertfilm(film arrayfilm[]);
int insertreg(regista arrayreg[]);
int main(){
//other stuff
film arrayfilm[SIZE];
regista arrayreg[SIZE];
//other stuff
switch (mainMenuChoice)
{
case 1:
{
insertfilm(arrayfilm);
break;
}
case 2:
{
insertreg(arrayfilm);
break;
}
//other stuff
it should be noted that i passed an array of struct film while it should expect an array of struct regista in insertreg()
I was expecting that it would report an error of wrong type but instead it went all silent and run it without any problem.
My question is: is this caused by my IDE (codeeblocks) or by the C implementation?
The gcc/mingw compiler that Codeblocks uses by default is lax when it comes to give compiler errors for C language type compatibility violations. If you run it with default settings, you get:
warning: passing argument 1 of 'insertreg' from incompatible pointer type
Now as far as the C language is concerned, the above is sufficient for the compiler to be compliant. C doesn't speak of errors and warnings, only of diagnostic messages.
Your code is not valid C, since two structs are only compatible if they have the same struct tag, if all their members have the same type and if they have the same variable name. You use different struct tags and different variable names both, so they aren't compatible.
Formally, since the types aren't compatible, your code is a "constraint violation of the simple assignment rule" and a compiler must give you a diagnostic message, which it did.
I strongly recommend all beginners to compile with strict standard compliance and maximum warnings though. With gcc this means -std=c11 -pedantic-errors -Wall -Wextra. Pedantic errors in particular will block the code from compiling into an executable even though there are C language violations.
In Codeblocks specifically: go Settings -> Compiler, then check the corresponding options there, for example "Enable extra compiler warnings [-Wextra]" to enable -Wextra.
Is there any extension feature to specify size of C enum on each compiler?
GCC
Clang
MSVC
With GCC, you cannot specify the exact length, but you can have it take up the shortest length possible with -fshort-enums. Example:
#include <stdio.h>
typedef enum
{
f1, f2
} foo;
int main()
{
printf("%i\n", sizeof(foo));
return 0;
}
Compile:
gcc program.c -fshort-enums
Output:
1
However, if you ever want to link to anything, you have to make sure that whoever looks at your headers also uses -fshort-enums or it will not be ABI compliant (and you will see some really funny errors).
C++11 introduced a standardized way to do this, but since this is C you'll have to settle for a more simple method of making the last enum INT_MAX or a value thats large enough so that only the type you want can hold it (this is what the DirectX SDK does). Unfortunatly there is no way to force a maximum size (at least not without compiler specific extensions).
With clang you can use this:
enum Tristate : char { Yes, No, Maybe };
If you want to typedef, ensure you include the size specifier for both, the enum and the typedef definition:
enum Tristate : char { Yes, No, Maybe };
typedef enum Tristate : char Tristate;
Otherwise you will get a compiler warning.
Or you define an anonymous enum and typedef directly:
typdef enum : char { Yes, No, Maybe } Tristate;
I'm having some trouble with -Wpadded using C11 and structs.
I've already read Structure member alignment with _Alignas, and I looked in the clang docs and saw that it IS supported now.
Also, I'm using a very new version of clang that I built from trunk recently.
$ clang --version
clang version 3.3 (trunk 175473)
Target: x86_64-unknown-linux-gnu
Thread model: posix
The problem I'm running into is this:
#include <stdlib.h>
#include <stdalign.h>
struct foo{
void* a;
int b;
};
int main() {
struct foo instance;
instance.a = NULL;
instance.b = 2;
return 0;
}
Which throws me this warning:
$ clang -Weverything -std=c11 t.c
t.c:4:8: warning: padding size of 'struct foo' with 4 bytes to alignment boundary [-Wpadded]
struct foo{
^
1 warning generated.
Now isn't this what _Alignas is for? I tried putting it before the int member declaration, like so:
struct foo{
void* a;
_Alignas(void*) int b;
};
But the same warning remains. I also tried putting the _Alignas in various places, to no avail. What am I missing here?
I know I could just ignore this particular warning and I understand why padding is important, so I'm not interested in workarounds or explanations about what padding is. I want to know how to change my C in a portable, standards conformant way so that the warning is no longer emitted.
-Weverything prints all diagnostic messages required by C as well as some diagnostics not required by C. The diagnostic that is printed here is not required by C: its purpose is informative and your program is already strictly conforming. C says an implementation is free to produce additional diagnostic messages as long as it does not fail to translate the program.
I have a struct that contains a float var. I am trying to read the value using a pointer to a struct. Here's the code:
struct mas {
float m;
};
int main(void)
{
struct mas *ms;
ms=(struct mas*)malloc(sizeof(struct mas));
scanf("%f",&(ms->m));
printf("%f",ms->m);
return 0;
}
But running the program produces the following error:
scanf floating point formats not linked
The compiler used is Borland Turbo C++ (3.0) on a Windows PC. Why is this so?
This might be helpful: http://www.faqs.org/faqs/msdos-programmer-faq/part2/section-5.html
From the article:
Borland's compilers try to be smart and not
link in the floating- point (f-p) library unless you need it. Alas, they
all get the decision wrong. ... (To force them to link it) define this function somewhere in a source file but don't
call it:
static void forcefloat(float *p)
{
float f = *p;
forcefloat(&f);
}
Also:
If you have Borland C++ 3.0, the README file documents a slightly less
ugly work-around. Insert these statements in your program:
extern unsigned _floatconvert;
#pragma extref _floatconvert
Why is this so..
Because there's a bug in your ancient, useless compiler. Upgrade to a newer one that properly handles floating point operations.
The latest version of GCC is a good choice, or you can download Microsoft's Visual C++ Express package for free, which bundles their compiler with a world-class IDE.
I am able to compile and run this code under GCC 4.2.1:
#include <stdlib.h>
#include <stdio.h>
struct mas{
float m;
};
int main()
{
struct mas *ms;
ms=malloc(sizeof(struct mas));
scanf("%f",&(ms->m));
printf("%f\n",ms->m);
return 0;
}
Are you missing any #include statements? I don't think you need to cast the result from malloc().
I am of late working on gcc compiler. whenevr i m compiling my code, m encountering problem with declaration of structure. How to tackle this problem. Do i need to write the syntax differently in gcc?. if yes, how? please suggest something.
I am reasonably sure gcc conforms to C standards, for a more succinct explanation than one found in the standard, please, turn to pages 148-150 of C: A Reference Manual.
So something simple like this linked list element:
struct foo
{
int a;
float b;
char *s;
struct foo *next;
} my_struct;
should work.
If your needs are more complex.. then you should post your non-working example.
EDIT: If you don't have access to CAR then this will suffice for now: http://publications.gbdirect.co.uk/c_book/chapter6/structures.html (obviously not C99)