Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Error When Looping Through Array

Status
Not open for further replies.

jamez05

Programmer
Jul 29, 2005
130
US
I get the errror "error C2109: subscript requires array or pointer type".

I'm not sure why this doesn't work. The error happens when I try to set CFtemp2 = bnext[md]: sprintf(CFtemp2,"%i",bnext[md]):

Code:
#define MAXATM 60
#define MAXDEG 6
int	bnext[MAXDEG+1][MAXATM+1];
char CFtemp[1000];//used for temparary storage variable when converting ints to chars
char CFtemp2[1000];//Secondary used for temparary storage variable when converting ints to chars for arrays

//functions fill bnext, now I try to loop through it:

			for (i = 0 ; i <= MAXATM + 1 ; i++) {
			
				
				sprintf(CFtemp,"%i",bnext[i]);

				for (md = 0 ; i <= MAXDEG + 1 ; md++) {
				
					int iRow = pQuery->AddRow() ;

					sprintf(CFtemp2,"%i",bnext[i][md]);				

					pQuery->SetData( iRow, iCol1, CFtemp2 ) ;
					pQuery->SetData( iRow, iCol2, CFtemp ) ;	
				

					CFtemp2[0] = '\0';
				
				}

				CFtemp[0] = '\0';

		
			}
 
I cant explain that error, but other than there are some obvious bugs
Code:
for (md = 0 ; i <= MAXDEG + 1 ; md++) {
Should be
Code:
for (md = 0 ; md <= MAXDEG + 1 ; md++) {

Also, when you declare something like
Code:
someType foo[x]
you're saying "foo is an array of x someTypes starting with index zero". That means valid index range is foo[0] to foo[x-1].

Hence with
Code:
int    bnext[MAXDEG+1][MAXATM+1];
and a checks like
Code:
i <= MAXDEG + 1
You will get an error when i==MAXDEG + 1, because the highest valid index is MAXDEG+1-1 wich of course == MAXDEG.

You normally write it as
Code:
someType foo[MAXFOO];
for (int i=0;i<MAXFOO;++i) // Note less than, not <=
  foo[i] = // ....


/Per
[sub]
www.perfnurt.se[/sub]
 
Thanks for the advice its appreciated. I'm going to have to take a look into it a little closer. I'm new to C++ and was given old Fortran Code that someone converted into a C executable. My job is to convert it to a C++ dll.

The author uses some odd code for compatibility:

Code:
// conversion note from Fortran to C:
// There is a great deal of dependency by the Fortran
// code on 1-based data arrays. Therefore, the C will
// make all data arrays 1 item longer, and use 1-based
// loops for them. We will handle character strings
// as zero-based, per C usage.

// Not the best solution, * but it will be a lot closer
// to the algorithms of the original authors.

I have a couple questions:

1. I'm wondering if I should be starting at 1. Here's some code the author has in a different function:

Code:
for (j = 1 ; j <= MAXDEG ; j++) {

}

2. The other question is how to check for null integer values in an array.

 
1. No, if you start at 1 you'll be skipping the first number in the array.

2. There are no NULL integers, only NULL pointers. Since you don't seem to be using pointers, you don't need to check anything for NULL.

If you're getting that strange error on this line:
Code:
sprintf(CFtemp2,"%i",bnext[i][md]);
Maybe you can try this and see what happens.
Code:
int test = bnext[i][md];
sprintf( CFtemp2, "%i", test );
Also, shouldn't this line:
Code:
sprintf(CFtemp,"%i",bnext[i]);
be:
Code:
sprintf(CFtemp,"%i",bnext[i][0]);
 
1.
Well, working with 1 based index is a bit silly in C++ since the array will start at index 0, no matter what. You'd still have someting at theArray[0] even if you never access it.

But I guess that in a short transition phase (while youre converting your 1-based fortran to 0-based C++) you could have it. However if it was me, I'd make it 0-based as soon as possible. The code will be much simpler, no MAX+1 stuff, for example. Simply MAX and <MAX.

2.
Assuming you mean an array of integers (or char for that matter, char is an integer type):
Code:
#define MAXATM 60
#define MAXDEG 6
int    bnext[MAXDEG][MAXATM];
// <assume bnext has ben given its proper values>
for(int i=0;i<MAXDEG;++i)
  for(int j=0;j<MAXATM;++j)
    if (bnext[i][j] == 0)

On a side note, you could replace your
Code:
#define MAXATM 60
with a cleaner
Code:
const int MAXATM=60;



/Per
[sub]
www.perfnurt.se[/sub]
 
Thanks again.

Conversion to 0-based is definitly on my list. I'm not sure what the author was thinking. However, for the time being, I'll take your advice:
Code:
for(int i=0;i<MAXDEG;++i)
instead of
for(int i=0;i =<MAXDEG + 1;++i)

Additionally, cpjust mentioned

Also, shouldn't this line:

Code:
sprintf(CFtemp,"%i",bnext[i]);
be:

Code:
sprintf(CFtemp,"%i",bnext[i][0]);

I'll give that a shot as well.

James
 
There are many ways of converting code from Fortran to C++. The easiest I find is to

1) increase all arrays by 1
2) have all loops going from 1 to <= limit
3) add a dummy value to all data initializations to cater for increasing array sizes by 1.

You will lose one value because the zeroth element is not used but if you want to get an algorithm working without worrying too much about indices, then code C the Fortran way.

Once you've got it working, re-examine the code and optimize for C. Trying to translate and get something working at the same time can be quite difficult. Some things are very easy to miss: for instance, if ix is and index and maxix is the max value,
Code:
if (ix .eq. maxix) ix = 1
will need to be translated to
Code:
if (ix == (maxix - 1)) ix = 0;
but you may not spot that straight away.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top