473,395 Members | 1,595 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

sizeof for extern struct

I've got a question on the proper handling of sizeof.

I have two files; a.c and b.c

In b.c I have:

struct Key en_keys[] = {
{EN_ENTER, DELAY, NULL},
};

in a.c I have

extern struct Key en_keys[];

and later, in a.c I do

foo = sizeof(en_keys);

Of course the compiler promptly pukes since sizeof is computed at
compile time and it doesn't know the size until link time....

So - what's the proper / recommended / sensible way to deal with this?
The number of elements in en_keys[] could change and I really don't want
to have to redo all of the size info.
Sep 14 '07 #1
3 2346
afaik you cant have the sizeof value at compile time because it is in
another code unit.

If you dont really need the exact value at compile time:
b.c
int theSize = sizeof(en_keys);
a.c
extern int theSize;
Typically you dont use the value, unless you use it for eg template
parameter instantiation..
Or duplicate the struct def in a.c
As long as they are idential it should be fine. Just dont try to compare the
types.
Sep 14 '07 #2
CptDondo wrote:
I've got a question on the proper handling of sizeof.

I have two files; a.c and b.c

In b.c I have:

struct Key en_keys[] = {
{EN_ENTER, DELAY, NULL},
};

in a.c I have

extern struct Key en_keys[];

and later, in a.c I do

foo = sizeof(en_keys);

Of course the compiler promptly pukes since sizeof is computed at
compile time and it doesn't know the size until link time....

So - what's the proper / recommended / sensible way to deal with this?
The number of elements in en_keys[] could change and I really don't want
to have to redo all of the size info.
Either add an extern const variable for the number of keys, or if the
the array is built at run time, end it with a NULL, as you appear to be
doing.

--
Ian Collins.
Sep 14 '07 #3
Punkie wrote:

Please retain the context of the thread you are replying to.
afaik you cant have the sizeof value at compile time because it is in
another code unit.

If you dont really need the exact value at compile time:
b.c
int theSize = sizeof(en_keys);
The type of sizeof is size_t.
a.c
extern int theSize;
Typically you dont use the value, unless you use it for eg template
parameter instantiation..
This is C....
>
Or duplicate the struct def in a.c
As long as they are idential it should be fine. Just dont try to compare the
types.
Not a good idea, one then has to maintain two or more copies of the same
array.

--
Ian Collins.
Sep 14 '07 #4

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

Similar topics

3
by: Christof Warlich | last post by:
Hi, I'm trying to build an _efficient_, _purely_ _abstract_ API for inter process(or) communication, _completely_ hiding any implementation details. The core components are "Buffer", "Address"...
7
by: Kieran Simkin | last post by:
I have in my project in main.c a number of arrays defined like this (outside of any functions): char muser, mpass; I'm declaring them in a number of other .c files (inside functions) like this:...
18
by: Kenneth Brody | last post by:
Is sizeof() legal within a #if preprocessor line? Specifically, I would like to use the following "sanity check" at compile time, but I'm getting an error on the compile about the #if line. ...
17
by: Tapeesh | last post by:
I would like to know what is the expected behaviour of C compilers when an extern decleration is intialized. When the following code is compiled using gcc //File extern.c int arr ; int a ;
6
by: Howard Kaikow | last post by:
Given: private struct PROCESSENTRY32 { public int dwSize; public int cntUsage; public int th32ProcessID; public int th32DefaultHeapID; public int th32ModuleID;
16
by: Martin Jørgensen | last post by:
Hi, Problem: ======== Some of my output functions are beginning to take pretty many arguments... I mean.... We're talking about 10-15 arguments :-) So I thought to myself, perhaps this is...
38
by: James Brown | last post by:
All, I have a quick question regarding the size of pointer-types: I believe that the sizeof(char *) may not necessarily be the same as sizeof(int *) ? But how about multiple levels of pointers...
9
by: Lalatendu Das | last post by:
I have seen a header file in which one structure is defined with extern declaration in a header file and declared one variable of that structure in one C-file. The code goes as mentioned below . I...
27
by: CodeMonk3y | last post by:
gotta question on sizeof keyword does the sizeof keyword calcuates the size at compile time or run time ?? -- Posted on news://freenews.netfront.net - Complaints to news@netfront.net --
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.