Moderators: phlip, Moderators General, Prelates
GTM wrote:I received a bunch of generated files labeled item001.h-item050.h. In each header file, there is a variable (let's say an int) named numberForItem001-050 (one for each header file), and each on has a different value in it. I want to change it so I can index into these variables. What I want is in item001.h, to declare an array numberForItem[50]. Then in item001.h make an assignment numberForItem[0] = 1, in item001.h make an assignment numberForItem[1] = 5; etc. Is there a way of doing this without moving everything to 1 header file (ie. initializing all 50 ints in item001.h). if not, any other ideas?
roband wrote:Mav is a cow.
/* item001.h */
numberForItem[0] = 1;
/* item002.h */
numberForItem[1] = 2;
/* etc */
/* items.c */
int numberForItem[50];
void initItems(void)
{
# include "item001.h"
# include "item002.h"
// etc
}/* item001.h */
1
/* item002.h */
2
/* etc */
/* items.c */
int numberForItem[] = {
# include "item001.h"
,
# include "item002.h"
,
// etc
};GTM wrote:Looks like I left out some info, oops.
The problem is that they are not in functions, the are just declared as global variables at the beginning of the header file.
Originally, I had a bunch of:
item001.h:
int numberForItem001 = 1;
item002.h:
int numberForItem002 = 2;
etc
now I want
item001.h:
int numberForItem[50];
numberForItem[0] = 1;
item002.h:
numberForItem[1] = 1;
but you can't assign values if they are not in functions (except during initialization)
#include "item001.h"
...
#include "itemN.h"
int *itemPntrArray[] = {
&numberForItem001,
&numberForItem002,
...
&numberForItemN,
};
*itemPntrArray[0] = 10;
#include "item001.h"
...
#include "itemN.h"
int &itemPntrArray[] = {
numberForItem001,
numberForItem002,
...
numberForItemN,
};
Sc4Freak wrote:I'm fairly certain you can't have an array of references. Arrays require default-constructible objects, and references can't be default constructed (since they can't be "reseated").
Comeau C/C++ 4.3.10.1 (Oct 6 2008 11:28:09) for ONLINE_EVALUATION_BETA2
Copyright 1988-2008 Comeau Computing. All rights reserved.
MODE:strict errors C++ C++0x_extensions
"ComeauTest.c", line 3: error: array of reference is not allowed
int& arr[] = {x , y, z};
^Sc4Freak wrote:I'm fairly certain you can't have an array of references. Arrays require default-constructible objects, and references can't be default constructed (since they can't be "reseated").
struct A { A(int); };
A x[2] = { 0, 1 }; // ok
A y[3] = { 0, 1 }; // errorjareds wrote:the prohibition on arrays of references is a specific rule with no clear rationale, not a particular case of a general rule.
Great Justice wrote:jareds wrote:the prohibition on arrays of references is a specific rule with no clear rationale, not a particular case of a general rule.
Well, since you can't have a pointer to a reference, and arrays are equivalent to const pointers to their first element, it is indeed consistent.
// item001.h
int numberForItem001 = 1;
// item002.h
int numberForItem002 = 2;
//itemarray.h
int *nums[] = {
&numberForItem001,
&numberForItem002
}
//main.c
#include <stdio.h>
#include "item001.h"
#include "item002.h"
#include "itemarray.h"
int main(int argc, char *argv[])
{
int i, n;
for (i=0; i < sizeof(nums)/sizeof(int *); i++) {
n = *nums[i];
printf("numberForItem%03d = %d\n", i, n);
}
return(0);
}
EvanED wrote:The second thing is false, and it's not consistent, and I'll edit this post with more later.
Great Justice wrote:...arrays are equivalent to const pointers to their first element, it is indeed consistent.
Yakk wrote:sizeof(array)/sizeof(array[0]) seems like a reasonable reason to avoid arrays of references. Not an unboundedly strong reason, but a non-trivial one.
letterX wrote:Well, in general, it seems that references really aren't first class objects in C++. They unfortunately make the programmer think that they're secretly a pointer, but the compiler is allowed to do pretty much anything with them (they have completely undefined values, and may not even exist as a value in the first place). Really, they're better off being treated as semantic sugar for compiler-voodoo than anything else.
Users browsing this forum: farrahfara11 and 7 guests