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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Int changing value for no reason at runtime.....

Status
Not open for further replies.

jaredgalen

Programmer
May 28, 2003
34
LT
I'm new to C, today being my first day really.
Here's the prob:

BYTE indexes[11]; <-- putting ints between zero and 4096 into this array.
In one hand I have a variable X.
I'm looking at indexes[0], I want to I push everything down and put X in at indexes[0].

However, somehow X is getting changed.

Before reordering - Inserting X=3236 <-variable X
225 161 98 164 225 161 100 96 37 36 33

X=164<- variable X has changed to this, somehow

After reordering
164 225 161 98 164 225 161 100 96 37 36

164 is what indexes[0] contains now, it should be 3236.

Should I be using pointers or something?? I really haven't a clue, this whole 'C' thing is very confusing.
What am I missing or have a made a really silly mistake that I just can't see....

Here's the bit of source that corresponds to this, there is only a print statement between...
The code is really crappy I know, I don't really know any major functions so it's very basic.

for(i=0;i<4096;i++)
{
if(keyCounter>largestKeyCount)
{
printf(&quot;\nBefore reordering - Inserting &quot;);
printf(&quot;%d&quot;,i);
printf(&quot;\n&quot;);
for(j=0;j<11;j++)
{
!!XXXX Here it has the right value XXX!!
printf(&quot;%d&quot;,indexes[j]);
printf(&quot; &quot;);
}

tempIndex1 = indexes[0];
indexes[0] = i;
printf(&quot;\n&quot;);<-!!XXXX Here it has changed XXXX!!
printf(&quot;%d&quot;,indexes[0]);
for(j=1;j<11;j++)
{
tempIndex2 = indexes[j];
indexes[j] = tempIndex1;
tempIndex1 = tempIndex2;
}
printf(&quot;\nAfter reordering\n&quot;);
for(j=0;j<11;j++)
{
printf(&quot;%d&quot;,indexes[j]);
printf(&quot; &quot;);
}
}
Hope someone can help me.
Thanks
jG
 
the size of a byte is 8 bits. That is a value from 0-255. Change your array type to short or int.

Matt
 
Oh for the love of god......
I feel like a right goon.
jG
 
It has happened to the best of us :-D Sometimes it just requires a second pair of eyes. The one that used to bite me in the butt was assigning instead of comparing in if statements.

if(value = <some value other then zero>)

matt :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top