473,796 Members | 2,607 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2384
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
2206
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" and "Process". They may be instantiated by a "Factory". Here is a simplified, but compilable version: // Abstract Interface enum OsSelector {
7
9043
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: extern char muser, mpass; However, in one of these functions outside of main.c I need to snprintf into these buffers. Usually when using snprintf I call it like this: snprintf(muser, sizeof muser, "text");
18
21816
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. struct foo { int a; int b; };
17
4934
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
6698
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
2323
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 beginning to get out of hands if I continue to put in extra functionality in the (file-writing) output-functions....
38
2610
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 to the same type? Would sizeof(char **) be the same as sizeof(char *)? And if it is, would the internal representation be the same in both cases? background on this: I'm writing a simple IDL compiler that produces 'C'
9
2694
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 am confused with the way it is declared and defined. a.h ------- : :
27
5612
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
9531
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10237
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10187
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10018
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9055
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6795
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5446
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5578
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2928
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.