473,396 Members | 1,886 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,396 software developers and data experts.

Typedef shadowing?

I am using C, there is no dynamic array. So i tried this and it worked... but want to know if this can be platform independent so i can actually use this across any platform.

Expand|Select|Wrap|Line Numbers
  1. typedef struct {
  2. uint16_t n;
  3. uint32_t m;
  4. char filename[1024];     
  5. } MY_TYPEDEF
Expand|Select|Wrap|Line Numbers
  1. // for no padding binary output to file
  2. #pragma pack(1) 
  3. typedef struct {
  4. uint16_t n;
  5. uint32_t m;
  6. char filename[];         // empty array
  7. } MY_TYPEDEF_NO_PACK
  8. #pragma pack(0) 
Now, when i output it, i need to copy over to the other structure without padding before i do the binary output. However, i cannot do stuff like:

Expand|Select|Wrap|Line Numbers
  1. MY_TYPEDEF_NO_PACK   tmp_struct;
  2. ...
  3. fwrite(tmp_struct, 1, sizeof(MY_TYPEDEF_NO_PACK), fp);
because i don't know how big my filename will be...

BUT, if i redefine the typedef within my other output function like this:

Expand|Select|Wrap|Line Numbers
  1. #pragma pack(1) 
  2. typedef struct {
  3. uint16_t n;
  4. uint32_t m;
  5. char filename[strlen(data->filename)];
  6. } MY_TYPEDEF_NO_PACK
  7. #pragma pack(0) 
  8.  
  9. MY_TYPEDEF_NO_PACK   tmp_struct;
  10. ...
  11. fwrite(tmp_struct, 1, sizeof(MY_TYPEDEF_NO_PACK), fp);
Then it will show the correct size lol...
It works, but i want to know if this can be ported to other platform.

THERE WERE NO ERROR or WARNINGS from the gcc... even with -W

Any thought?
Aug 29 '07 #1
3 1934
JosAH
11,448 Expert 8TB
Expand|Select|Wrap|Line Numbers
  1. char filename[strlen(data->filename)];
THERE WERE NO ERROR or WARNINGS from the gcc... even with -W

Any thought?
Yep; that most definitely is a gnu extension. Didn't gcc warn even with a -Wall
flag set?

kind regards,

Jos
Aug 29 '07 #2
Nope, zero warning.
Have you done this "hack" before?

Well it will give warning IF i use it in my header file.
But since i already have it defined in my header with an EMPTY ARRAY, so i think it didn't complain when the compiler sees it the second time inside local function.

Expand|Select|Wrap|Line Numbers
  1. char filename[]; 
Was defined in a header file.

Expand|Select|Wrap|Line Numbers
  1. char filename[strlen(data.filename)];
Was re-defined in another function, sort act like a local typedef.
Aug 29 '07 #3
Ah on Solaris it would report expected constant. So i guess it doesn't work cross platform hehe...
Aug 29 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: joe | last post by:
hi, after reading some articles and faq, i want to clarify myself what's correct(conform to standard) and what's not? or what should be correct but it isn't simply because compilers don't...
3
by: Aguilar, James | last post by:
Hey all. I was making a newbie mistake that I eventually figured out. That is not my question. My question is about the error message. So let me set the stage for you: class Superclass {...
15
by: Merrill & Michele | last post by:
typedef struct { WORD versionNumber; WORD offset; } MENUITEMTEMPLATEHEADER; This is from vol 5 of unnamed platform's programmer's reference. I could make this conforming by enclosing...
3
by: Nick Stansbury | last post by:
Hi, Quick (and probably simple) question regarding shadowing and polymorphism. Problem is best explained by an example. Public Class A public CommonProperty as Integer end class Public...
5
by: Dave Taylor | last post by:
I'm trying to derive a class from a typed DataSet to add some methods to one of the DataTables. I would like to keep the names the same in the derived class as in the base class, so I have used...
2
by: David Garamond | last post by:
Is there a feature similar to this currently in Postgres, or will there be? Sometimes (like in a shared hosting environment), we cannot have the luxury of hot-swapped RAID or expensive SAN, and...
6
by: Alex | last post by:
Hello people, I am getting errors from VS2003 when working with typedef'ed types. For example, assume that I have a type T, defined in a 3rd party include file based on some condition #if...
1
by: John Salerno | last post by:
I understand that if you reassign a built-in name to your own variable, such as: str = 'hello' then you lose the use of the built-in (in this case str()), but is this also the case in terms...
3
by: Cartoper | last post by:
I really like the shadowing that this web site is using behind the images. I have tried to dig through their code to figure out how they did it, but it still eludes me. How do they do it? ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
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
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
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...
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,...

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.