473,698 Members | 1,921 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

declaring an array in a structure with a #define length

Hello everyone,
i am attempting to make a structure

#include "globalVars .h"

struct myStruct{
int offset;
unsigned char uChars[numOfGlobalVars -1];

} saveVars, getVars;
and i have #define numOfGlobalVars in globalVars.h... it does not
compile saying

main.cpp:23: error: 'struct myStruct' has no member named 'uChars',
when i try to access these uChars later on in my code.

should i not be able to do that?

nass

Jul 28 '06 #1
10 2594
at************* *@gmail.com writes:
>Hello everyone,
i am attempting to make a structure
>#include "globalVars .h"
>struct myStruct{
int offset;
unsigned char uChars[numOfGlobalVars -1];
>} saveVars, getVars;
>and i have #define numOfGlobalVars in globalVars.h... it does not
compile
It works for me. I suggest you post a minimal but complete example.
Jul 28 '06 #2

at************* *@gmail.com wrote:
Hello everyone,
i am attempting to make a structure

#include "globalVars .h"

struct myStruct{
int offset;
unsigned char uChars[numOfGlobalVars -1];

} saveVars, getVars;
and i have #define numOfGlobalVars in globalVars.h... it does not
compile saying

main.cpp:23: error: 'struct myStruct' has no member named 'uChars',
when i try to access these uChars later on in my code.

should i not be able to do that?

nass
It worked for me. Tell us a little more about the compiler you've used
the version and stuff.

gangs.

Jul 28 '06 #3
hello again
g++ 3.3.6 compiler on slackware linux but
i am using the qmake utility of QtDesigner to generate the makefile,
then i just type make...
still no qlibraries are used in this part of the program...
here im sending u the fileIO.h and fileIO.cpp as well as a globalVars.h
and a main.cpp that i made to test the fileIO files:

/*------------globalVars.h----------------------------------------------------------*/

#ifndef GLOBALVARS_H
#define GLOBALVARS_H

int appDateTimeOffs et;
unsigned char operationalStat e; //0=auto, 1=manual,
2=scheduled, 3=logging
unsigned char logSamplingPeri od; //0=off, 1=1min, 2=10min,
3=30min, 4=1hour
unsigned char logType; //0=cyclical, 1=linear
unsigned char changeLog; //0=per1min, 1=per1hour,
2=per1day

#endif //GLOBALVARS_H
/*------------fileIO.h----------------------------------------------------------*/

#ifndef FILEIO_H
#define FILEIO_H

void writeToFile(con st void *buffer,const int &unitSize,co nst char
*filename="../globalsFile.txt ");

int readFromFile(vo id *buffer,const long bufSize,const char
*filename="../globalsFile.txt ");

#endif //FILEIO_H

/*------------fileIO.cpp----------------------------------------------------------*/

#include "fileIO.h"
#include <stdio.h>

void writeToFile(con st void *buffer,const int &unitSize,co nst char
*filename)
{
FILE * pFile;
pFile = fopen (filename,"wt") ;
fwrite(buffer,u nitSize,1,pFile );
fclose(pFile);

}

int readFromFile(vo id *buffer,const long bufSize,const char *filename)
{
FILE * pFile;
pFile = fopen (filename,"rt") ;

if (pFile==NULL) return (0);

fread (buffer,bufSize ,1,pFile);

fclose (pFile);

return (1);
}

/*------------main.cpp----------------------------------------------------------*/

#include "fileIO.h"
#include "../globalVars.h"
#include <iostream.h>

struct myStruct{
int offset;
//unsigned char uChars[numOfGlobalVars-1];
unsigned char uChar1;
unsigned char uChar2;
unsigned char uChar3;
unsigned char uChar4;
} saveVars, getVars;

int main()
{
//init the global variables
appDateTimeOffs et=99;
operationalStat e=96;
logSamplingPeri od=101;
logType=75;
changeLog=69;

//fill up the structure with the globalVariables values
saveVars.offset =appDateTimeOff set;
saveVars.uChar1 =operationalSta te;
saveVars.uChar2 =logSamplingPer iod;
saveVars.uChar3 =logType;
saveVars.uChar4 =changeLog;

writeToFile(&sa veVars,sizeof(s aveVars));

//reinit the globalVars
appDateTimeOffs et=0;
operationalStat e=0;
logSamplingPeri od=0;
logType=0;
changeLog=0;

if (readFromFile(& getVars,sizeof( getVars)))
cout<<"all OK"<<endl;

//refill the globalVars with the readFromFile values
appDateTimeOffs et=getVars.offs et;
operationalStat e=getVars.uChar 1;
logSamplingPeri od=getVars.uCha r2;
logType=getVars .uChar3;
changeLog=getVa rs.uChar4;

cout<<appDateTi meOffset<<endl;
cout<<operation alState<<endl;
cout<<logSampli ngPeriod<<endl;
cout<<logType<< endl;
cout<<changeLog <<endl;

}//main

/*-----------------------------END-------------------------------------------------------------*/

i have switched to 4 independent entries (the uChar# declarations) in
order to make it work... but thats pretty much it.. if you want to
compile it just change the uChar# to uChars[#]..
Thank you for your help.
nass
gangs wrote:
at************* *@gmail.com wrote:
Hello everyone,
i am attempting to make a structure

#include "globalVars .h"

struct myStruct{
int offset;
unsigned char uChars[numOfGlobalVars -1];

} saveVars, getVars;
and i have #define numOfGlobalVars in globalVars.h... it does not
compile saying

main.cpp:23: error: 'struct myStruct' has no member named 'uChars',
when i try to access these uChars later on in my code.

should i not be able to do that?

nass

It worked for me. Tell us a little more about the compiler you've used
the version and stuff.

gangs.
Jul 28 '06 #4

<at************ **@gmail.comwro te in message
news:11******** *************@i 42g2000cwa.goog legroups.com...
Hello everyone,
i am attempting to make a structure

#include "globalVars .h"

struct myStruct{
int offset;
unsigned char uChars[numOfGlobalVars -1];

} saveVars, getVars;
and i have #define numOfGlobalVars in globalVars.h... it does not
compile saying

main.cpp:23: error: 'struct myStruct' has no member named 'uChars',
when i try to access these uChars later on in my code.
Are you using "struct myStruct" as a local variable or parameter type
somewhere? Try just "myStruct" as the type. This is C++, not C. (Right?)

(Also, next time, post the code that's causing the problem. You've left out
main, where the error message says the problem is.)

-Howard

Jul 28 '06 #5

<at************ **@gmail.comwro te in message
news:11******** **************@ s13g2000cwa.goo glegroups.com.. .
hello again
g++ 3.3.6 compiler on slackware linux but
i am using the qmake utility of QtDesigner to generate the makefile,
then i just type make...
still no qlibraries are used in this part of the program...
here im sending u the fileIO.h and fileIO.cpp as well as a globalVars.h
and a main.cpp that i made to test the fileIO files:

/*------------globalVars.h----------------------------------------------------------*/

#ifndef GLOBALVARS_H
#define GLOBALVARS_H

int appDateTimeOffs et;
unsigned char operationalStat e; //0=auto, 1=manual,
2=scheduled, 3=logging
unsigned char logSamplingPeri od; //0=off, 1=1min, 2=10min,
3=30min, 4=1hour
unsigned char logType; //0=cyclical, 1=linear
unsigned char changeLog; //0=per1min, 1=per1hour,
2=per1day

#endif //GLOBALVARS_H

<snipped fileIO stuff>
/*------------main.cpp----------------------------------------------------------*/

#include "fileIO.h"
#include "../globalVars.h"
#include <iostream.h>

struct myStruct{
int offset;
//unsigned char uChars[numOfGlobalVars-1];
unsigned char uChar1;
unsigned char uChar2;
unsigned char uChar3;
unsigned char uChar4;
} saveVars, getVars;

int main()
{
//init the global variables
appDateTimeOffs et=99;
operationalStat e=96;
logSamplingPeri od=101;
logType=75;
changeLog=69;

//fill up the structure with the globalVariables values
saveVars.offset =appDateTimeOff set;
saveVars.uChar1 =operationalSta te;
saveVars.uChar2 =logSamplingPer iod;
saveVars.uChar3 =logType;
saveVars.uChar4 =changeLog;

writeToFile(&sa veVars,sizeof(s aveVars));

//reinit the globalVars
appDateTimeOffs et=0;
operationalStat e=0;
logSamplingPeri od=0;
logType=0;
changeLog=0;

if (readFromFile(& getVars,sizeof( getVars)))
cout<<"all OK"<<endl;

//refill the globalVars with the readFromFile values
appDateTimeOffs et=getVars.offs et;
operationalStat e=getVars.uChar 1;
logSamplingPeri od=getVars.uCha r2;
logType=getVars .uChar3;
changeLog=getVa rs.uChar4;

cout<<appDateTi meOffset<<endl;
cout<<operation alState<<endl;
cout<<logSampli ngPeriod<<endl;
cout<<logType<< endl;
cout<<changeLog <<endl;

}//main

/*-----------------------------END-------------------------------------------------------------*/

i have switched to 4 independent entries (the uChar# declarations) in
order to make it work... but thats pretty much it.. if you want to
compile it just change the uChar# to uChars[#]..
Thank you for your help.
nass

Please don't top-post. Post responses below or interspersed with what
you're quoting. And trim off extra stuff that's not needed, ok? (Thanks.)

Where is numOfGlobalVars defined? I don't see it anywhere here. If you've
left it out, you'll get more than just the one error you asked about.
Perhaps you're also getting an error that numOfGlobalVars is undefined?

One suggestion: don't use globals unless you have to. Why not declare
getVars and saveVars as local variables in main? I see no reason they need
to be globals.

-Howard

Jul 28 '06 #6

<at************ **@gmail.comwro te in message
news:11******** **************@ s13g2000cwa.goo glegroups.com.. .
>
/*------------globalVars.h----------------------------------------------------------*/

#ifndef GLOBALVARS_H
#define GLOBALVARS_H

int appDateTimeOffs et;
unsigned char operationalStat e; //0=auto, 1=manual,
2=scheduled, 3=logging
unsigned char logSamplingPeri od; //0=off, 1=1min, 2=10min,
3=30min, 4=1hour
unsigned char logType; //0=cyclical, 1=linear
unsigned char changeLog; //0=per1min, 1=per1hour,
2=per1day

#endif //GLOBALVARS_H

This probably isn't a good idea. For one thing, it's best to avoid global
variables whenever possible. These could all have gone inside your main()
function. There's no need to make them globals.

Second, if you ever include this header file from another unit, you'll get
an "undefined symbol" error, because of the include guards. And removing
the include guards would result in "muliply defined" errors.

If you really need to use global variables, and if you need a header file
you can include in multiple units to see those variables, then here's what
you do: Declare the global variables in an implementation (.cpp) file.
Then, declare them as "extern" in the header file. This lets everyone who
includes the header file see those symbols, without getting multiple
definition errors.

But as I said, try to avoid using globals in the first place, and life
(well, at least your programming life) will be much easier.

-Howard
Jul 28 '06 #7
at************* *@gmail.com wrote:
hello again
g++ 3.3.6 compiler on slackware linux but
i am using the qmake utility of QtDesigner to generate the makefile,
then i just type make...
still no qlibraries are used in this part of the program...
here im sending u the fileIO.h and fileIO.cpp as well as a globalVars.h
and a main.cpp that i made to test the fileIO files:

/*------------globalVars.h----------------------------------------------------------*/

#ifndef GLOBALVARS_H
#define GLOBALVARS_H

int appDateTimeOffs et;
unsigned char operationalStat e; //0=auto, 1=manual,
2=scheduled, 3=logging
unsigned char logSamplingPeri od; //0=off, 1=1min, 2=10min,
3=30min, 4=1hour
unsigned char logType; //0=cyclical, 1=linear
unsigned char changeLog; //0=per1min, 1=per1hour,
2=per1day

#endif //GLOBALVARS_H
As others have said, putting the definitions of globals in a header is a
bad idea.

One more thing, if you variables have a fixed set of valid values, use
enums.

--
Ian Collins.
Jul 28 '06 #8
People,
i do appreciate all your efforts to pull me away from using global
variables and i can assure you i don't often.. the code i posted here
is trivial however, just a test main.cpp to make sure that fileIO.h,
fileIO.cpp work correctly... so yes, these 2 structures are not
defined globally there.. they did not need to indeed...
however some other important variables declared in globalVars.h file
are assigned values by forms that are are generated at run time and
also get destroyed then to free up memory.. and it would get messy if i
tried to include every header file containing an extern declaration to
every other simply cause i would have declared a variable in their
corresponding cpp...

anyhow you tell me that i have posted in complete code.. i mean come
on, did you read what i have included? main.h is there, and
globalVars.h is there too along with fileIO.h and fileIO.cpp... in fact
i was thinking i wrote too much code... however since i know that this
main.cpp SHOULD compile i decided to post it all just in case i have
overlooked smth else that causes the error in compilation.

once again i thank you for your advice - my programming experience is
very limited, yet i know to not globalise everything. still lets focus
on the fact that
struct myStruct {unsigned char uChar[numOfGlobalVars-1];} does not
compile.
nass

Ian Collins wrote:
at************* *@gmail.com wrote:
hello again
g++ 3.3.6 compiler on slackware linux but
i am using the qmake utility of QtDesigner to generate the makefile,
then i just type make...
still no qlibraries are used in this part of the program...
here im sending u the fileIO.h and fileIO.cpp as well as a globalVars.h
and a main.cpp that i made to test the fileIO files:

/*------------globalVars.h----------------------------------------------------------*/

#ifndef GLOBALVARS_H
#define GLOBALVARS_H

int appDateTimeOffs et;
unsigned char operationalStat e; //0=auto, 1=manual,
2=scheduled, 3=logging
unsigned char logSamplingPeri od; //0=off, 1=1min, 2=10min,
3=30min, 4=1hour
unsigned char logType; //0=cyclical, 1=linear
unsigned char changeLog; //0=per1min, 1=per1hour,
2=per1day

#endif //GLOBALVARS_H
As others have said, putting the definitions of globals in a header is a
bad idea.

One more thing, if you variables have a fixed set of valid values, use
enums.

--
Ian Collins.
Jul 29 '06 #9
at************* *@gmail.com wrote:
>and i have #define numOfGlobalVars in globalVars.h... it does not
compile saying
>main.cpp:23: error: 'struct myStruct' has no member named 'uChars',
when i try to access these uChars later on in my code.
/*------------globalVars.h----------------------------------------------------------*/

#ifndef GLOBALVARS_H
#define GLOBALVARS_H

int appDateTimeOffs et;
unsigned char operationalStat e; //0=auto, 1=manual,
2=scheduled, 3=logging
unsigned char logSamplingPeri od; //0=off, 1=1min, 2=10min,
3=30min, 4=1hour
unsigned char logType; //0=cyclical, 1=linear
unsigned char changeLog; //0=per1min, 1=per1hour,
2=per1day

#endif //GLOBALVARS_H
There is no define numOfGlobalVars in globalvars.h .

Jul 29 '06 #10

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

Similar topics

8
2576
by: Steve Lambert | last post by:
Hi, I'd be grateful if someone could clarify this for me. In the linked list structure my intention is to declare an array of length 3 containing pointers to node eg. Node *Iterators The compiler seems to interpret this as a pointer to an array of 3 nodes instead. This interpretation ensures that the second assigment to mynode below fails compilation with the given message.
36
4425
by: Eric Laberge | last post by:
Hi! I'm working on automatically generated code, and need to assign arrays. memcpy is an obvious solution, but it becomes complicated to use in the context I'm working on, ie.: I could use it but I don't want to. Arrays cannot be assigned in C, but structs can, so I coded the following: #include <stdlib.h>
1
4546
by: Me | last post by:
I'm trying to get a structure into a byte array. I can't seem to figure out how to get a non-fixed length null-terminated string into the array (without rolling my own logic). For example, a struct like (from another posting in this group): <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Ansi)> _ Private Structure SPECIFIC_SOCKET_MSG Public Length As Integer Public MsgId As Integer Public FileName As String
12
3878
by: gcary | last post by:
I am having trouble figuring out how to declare a pointer to an array of structures and initializing the pointer with a value. I've looked at older posts in this group, and tried a solution that looked sensible, but it didn't work right. Here is a simple example of what I'm trying to accomplish: // I have a hardware peripheral that I'm trying to access // that has two ports. Each port has 10 sequential // registers. Create a...
9
5615
by: rkk | last post by:
Hi, I have written a generic mergesort program which is as below: --------------------------------------------------------- mergesort.h ----------------------- void MergeSort(void *array,int p,int r,int elemSize,int(*Compare)(const void *keyA,const void *keyB));
8
2934
by: =?Utf-8?B?UHVjY2E=?= | last post by:
Hi, I'm using vs2005, .net 2, C# for Windows application. I use DllImport so I can call up a function written in C++ as unmanaged code and compiled as a dll us vs2005. My application is able to call the function, EncodeAsnUser. And it's returning OK but when I display the decoded data in another part of my application it shows no data has been decoded, all fiedls are either null or blanks. For some reason, I am not able to step through...
15
3761
by: bernd | last post by:
Hi folks, a simple question for the experts, I guess. Obviously I am doing something wrong when trying to access an element of an array declared within a structure: #include <stdio.h> #include <stddef.h>
5
3788
by: =?Utf-8?B?QXlrdXQgRXJnaW4=?= | last post by:
Hi Willy, Thank you very much for your work. C++ code doesnot make any serialization. So at runtime C# code gives an serialization error at "msg_file_s sa = (msg_file_s) bf.Deserialize(ms);" I thought that it is very hard to memory map structure array. I need both read and write memory mapped file at both side of C# and C++.
10
6368
by: Scott Townsend | last post by:
So I need to talk to a devices that expects all of the bits and bytes I sent it to be in specific places (not yet 100% defined). I wanted to create a structure/class with all of the data in it and then convert that to a byte array, pass it to the device, then get a reply and then convert that to a structure. I'm having issues with making sure what I've coded is correct. Cant figure out how to define an array in structure that is a...
0
8671
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8598
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
9152
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
7709
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...
1
6515
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4360
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...
1
3037
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 we have to send another system
2
2321
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
1997
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.