473,471 Members | 1,970 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

struct in C99

Hi all,

Using -std=c99 to enforce the c99 standard in gcc I get:
$ gcc -Wall -std=c99 timespec.c
timespec.c: In function 'main':
timespec.c:5: error: storage size of 'a' isn't known
timespec.c:5: error: storage size of 'b' isn't known
....

with:

#include <time.h>
#include <stdio.h>

int main() {

struct timespec a, b;

printf("Doh!\n");

return 0;

}
Can someone please explain why? This doesn't happen in previous
versions of C.

Cheers,

Paulo Matos

Jun 12 '06 #1
10 9428
po******@gmail.com schrieb:
Hi all,

Using -std=c99 to enforce the c99 standard in gcc I get:
Note: gcc still is not entirely a C99 compiler, see
http://gcc.gnu.org/C99status.html
$ gcc -Wall -std=c99 timespec.c
Use gcc -Wall -std=c99 -pedantic -O timespec.c to get more
warnings and refuse more non-Standard C stuff.
timespec.c: In function 'main':
timespec.c:5: error: storage size of 'a' isn't known
timespec.c:5: error: storage size of 'b' isn't known
...

with:

#include <time.h>
#include <stdio.h>

int main() {

struct timespec a, b;
Note: Neither <stdio.h> nor <time.h> is guaranteed to define the
type struct timespec. This type _probably_ comes from your
<sys/types.h> and seems to be not excluded properly for other
modes invoked by the std option.
In short: The compiler gets it right for -std=c99 but should
get it right for -std=c89 as well...
It may be necessary to
#include <sys/types.h>
or to
#define FOO
where FOO governs whether certain parts or the whole of
sys/types.h are processed/included. This is implementation
specific and thus off-topic round here.

It depends on the gcc and glib versions in use, so you have
to ask in a newsgroup dealing with either gcc or your OS to get
a complete/good answer.


printf("Doh!\n");

return 0;

}

Can someone please explain why? This doesn't happen in previous
versions of C.


Note that gcc by default compiles with -std=gnu89 which is C90
plus GNU extensions -- this language and its library contain
stuff plain standard C (in the C90, C95, or C99 variety) does
not have.
Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Jun 12 '06 #2


po******@gmail.com wrote On 06/12/06 15:29,:
Hi all,

Using -std=c99 to enforce the c99 standard in gcc I get:
$ gcc -Wall -std=c99 timespec.c
timespec.c: In function 'main':
timespec.c:5: error: storage size of 'a' isn't known
timespec.c:5: error: storage size of 'b' isn't known
...

with:

#include <time.h>
#include <stdio.h>

int main() {

struct timespec a, b;

printf("Doh!\n");

return 0;

}
Can someone please explain why? This doesn't happen in previous
versions of C.


There is no `struct timespec' in the C Standard library.
Your problem (if you want to call it such) is that you have
told the compiler to adhere to the C Standard (-stc=c99) but
are using a non-Standard extension.

--
Er*********@sun.com

Jun 12 '06 #3
po******@gmail.com writes:
Using -std=c99 to enforce the c99 standard in gcc I get:
$ gcc -Wall -std=c99 timespec.c
timespec.c: In function 'main':
timespec.c:5: error: storage size of 'a' isn't known
timespec.c:5: error: storage size of 'b' isn't known
...

with:

#include <time.h>
#include <stdio.h>

int main() {

struct timespec a, b;

printf("Doh!\n");

return 0;

}
Can someone please explain why? This doesn't happen in previous
versions of C.


The type "struct timespec" is defined by POSIX, not by the C standard.
In (non-POSIX) standard C, a user program is allowed to declare its
own "struct timespec", so it's not allowed to declare it in <time.h>.
You need to tell your compiler to compile in some sort of
POSIX-specific mode. I don't know how to do that. Try a Google
search, or gnu.gcc.help.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Jun 12 '06 #4

po******@gmail.com wrote:
Hi all,

Using -std=c99 to enforce the c99 standard in gcc I get:
$ gcc -Wall -std=c99 timespec.c
timespec.c: In function 'main':
timespec.c:5: error: storage size of 'a' isn't known
timespec.c:5: error: storage size of 'b' isn't known
...

with:

#include <time.h>
#include <stdio.h>

int main() {

struct timespec a, b;

printf("Doh!\n");

return 0;

}


It simply means that "struct timespec" is an incomplete type... Which
simply means that there has not been any definition of this structure.
Since it is a non-standard structure, this result was expectable.

Jun 12 '06 #5

Michael Mair escreveu:>
Use gcc -Wall -std=c99 -pedantic -O timespec.c to get more
warnings and refuse more non-Standard C stuff.


Why is the -O?

Jun 12 '06 #6

Eric Sosman escreveu:

There is no `struct timespec' in the C Standard library.
Your problem (if you want to call it such) is that you have
told the compiler to adhere to the C Standard (-stc=c99) but
are using a non-Standard extension.

Wierd, I would guess that -stc=c99 would let me use other stuff besides
pure c99 but would require _my_ code to be c99 compliant. It seems that
it is much more restrictive. Which is a shame!

Thanks for clearing that up.
--
Er*********@sun.com


Jun 12 '06 #7
po******@gmail.com writes:
Michael Mair escreveu:>
Use gcc -Wall -std=c99 -pedantic -O timespec.c to get more
warnings and refuse more non-Standard C stuff.


Why is the -O?


I presume you know, or can easily find out from the documentation,
that "-O" enables optimization.

A compiler typically has to collect more information about the program
it's compiling in order to perform certain optimizations. For
example, given:

{
int x, y;
...
x = 10;
...
y = x;
}

If the compiler can prove that x is not modified before its value is
assigned to y, it can replace "y = x;" with the equivalent of "y = 10;".
This is called "dataflow analysis".

In the course of collecting this information, it can detect certain
kinds of programming errors, such as using a variable that hasn't been
initialized. So enabling optimization *and* warnings can often give
you more information about errors in your code than just enabling
warnings.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Jun 13 '06 #8
On 12 Jun 2006 16:45:30 -0700
po******@gmail.com wrote:
Wierd, I would guess that -stc=c99 would let me use other stuff besides
pure c99 but would require _my_ code to be c99 compliant. It seems that
it is much more restrictive. Which is a shame!

Thanks for clearing that up.

If you use functions that are not from c99 standard, then your code is
not c99 compilant. You can, of course, create the function you want, and
that includes functions that are not part of c99 standard, and you have
to have a definition for such functions.
Jun 13 '06 #9
po******@gmail.com schrieb:
Eric Sosman escreveu:
There is no `struct timespec' in the C Standard library.
Your problem (if you want to call it such) is that you have
told the compiler to adhere to the C Standard (-stc=c99) but
are using a non-Standard extension.


Wierd, I would guess that -stc=c99 would let me use other stuff besides
pure c99 but would require _my_ code to be c99 compliant. It seems that
it is much more restrictive. Which is a shame!


<OT>
You are probably looking for "-std=gnu99".
</OT>

Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Jun 13 '06 #10

po******@gmail.com wrote:
Eric Sosman escreveu:

There is no `struct timespec' in the C Standard library.
Your problem (if you want to call it such) is that you have
told the compiler to adhere to the C Standard (-stc=c99) but
are using a non-Standard extension.


Wierd, I would guess that -stc=c99 would let me use other stuff besides
pure c99 but would require _my_ code to be c99 compliant. It seems that
it is much more restrictive. Which is a shame!

No, it is a flag which allows the GNU compiler to be fully C99
compliant, with extensions.
It allows any piece of strictly conforming code to compile (well, at
this time, the GNU compiler doesn't fully implement C99, but in a near
future, it will).
But these extensions mustn't interact badly with strictly conforming
code.
And a strictly conforming code can write its own "struct timespec".
If this non-standard extension was put in the language, a strictly
conforming program, such as:
#include <time.h>
#include <stdio.h>

struct timespec {int a,b;};
int main() {
struct timespec a, b;
a.a=0;a.b=8;
printf("Doh!\n");
return 0;
}

Would not compile, saying that timespec is defined twice.

It doesn't mean that this extension is inexistant; Read the
documentation.

There is perhaps a __timespec (names starting with double underscores
are reserved for compiler extensions and this kind of stuff).
Or there is perhaps a header to include : Perhaps <timespec.h>

The gnu99 mode is "almost" ISO compliant but contains a few core or
library extensions which doesn't permit the compilation of a few
strictly conforming well-formed programs.

Anyway, -std=c99 doesn't require your program to be conforming... You
can still use a lot of non-standard extensions, but not extensions
which change the meaning of strictly-conforming programs.
With -std=c99, the compiler itself is ISO-compliant (as far as
possible).

If you want to reduce the number of non-standard extensions used in
your program, then you should use the "--pedantic-errors" compiler
flag... Otherwise your program might contain several non-standard
extensions.

For instance, this piece of code:

int main() {
int i=42;
int u[i];
}

Compiles with "-xc -std=c89"
While it doesn't compile (complaining that VLA are forbidden) with "-xc
-std=c89 --pedantic-errors".

Jun 13 '06 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Roy Hills | last post by:
When I'm reading from or writing to a network socket, I want to use a struct to represent the structured data, but must use an unsigned char buffer for the call to sendto() or recvfrom(). I have...
10
by: Rick Anderson | last post by:
All, I am receiving the following compilation error on LINUX (but not Solaris, HPUX, WIN32, etc): compiling osr.c LBFO.h(369): warning #64: declaration does not declare anything extern...
5
by: PCHOME | last post by:
Hello! I am working on dividing a single C file into several files. Now I encounter a problem about the global variables and can not find a way to solve it. All global variables and codes used...
19
by: Russell Shaw | last post by:
Hi, I have two structs in a header file, and they reference each other, causing a compile error. Is there a standard way to deal with this? typedef struct { ... RtAction *actions; }...
16
by: burn | last post by:
Hello, i am writing a program under linux in c and compile my code with make and gcc. Now i have 4 files: init.c/h and packets.c/h. Each header-file contains some: init.h: struct xyz {
5
by: Johs32 | last post by:
I have a struct "my_struct" and a function that as argument takes a pointer to this struct: struct my_struct{ struct my_struct *new; }; void my_func(struct my_struct *new); I have read...
7
by: Alex | last post by:
If I have two struct. See below: struct s1 { int type; int (*destroy)(struct s1* p); } struct s2 { struct s1 base;
4
by: hobbes992 | last post by:
Howdy folks, I've been working on a c project, compiling using gcc, and I've reached a problem. The assignment requires creation of a two-level directory file system. No files have to be added or...
4
by: hugo.arregui | last post by:
Hi! I have two struts like that: struct { int num; int num2; struct b arrayOfB; } a;
4
by: Sheldon | last post by:
Hi, I have a unique case where I need an array of structs that grows and within this array is another struct that grows in some cases. I'm having trouble allocating memory. Since I have never...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.