Jump to content

initilizing int to 0 in for-loop gets wierd numbers...

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
18 replies to this topic

#1
nick3

nick3

    Newbie

  • Members
  • PipPip
  • 29 posts
Ok the name of the topic sucks i guess, but dont know how to better describe my problem... Im doing a program using the WinAPI, in WM_CREATE I want to fill an array with 20 blanks (" "), so I did a for loop

for(int i = 0; i <= 20; i++)

		item[i] = (char)" ";


to me that looks as simple and straight forward as it gets but I have a problem, when I debug, i gets the value -858993460 instead of 0... ofc that results in no blanks in the array. I just dont understand this.

I guess its something very easy i missed, but would love to get some help with it!

/Nick

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
Do you have i declared anywhere else? Also, why not assign item[i] = ' ';?
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
nick3

nick3

    Newbie

  • Members
  • PipPip
  • 29 posts

WingedPanther said:

Do you have i declared anywhere else? Also, why not assign item[i] = ' ';?

Hmm, but would that matter since I set it to zero in the for()? Not used to program in C++ and even less in WinAPI so dont really use char and stuff but thanks for the tip!

#4
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
My concern is that the gdb results may be misleading. C++ scoping rules can cause odd things to happen.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#5
Natrobius

Natrobius

    Programmer

  • Members
  • PipPipPipPip
  • 166 posts
Is your program giving unexpected output? Why are you debugging that variable anyways?

#6
dcs

dcs

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 775 posts

nick3 said:

I want to fill an array with 20 blanks (" "), so I did a for loop

for(int i = 0; i <= 20; i++)

		item[i] = (char)" ";
If item is a string, you're doing it wrong. A space character is ' ' (single quotes). You seem to be assigning the address of a string literal.

Why the cast if you are otherwise doing it correctly (I find that many casts, especially by folks new to the language, are used to erroneously shut the compiler up when it's trying to tell you to do the right thing).

Also, your loop has 21 iterations. If your array has 20 elements, you're overrunning the array.

And C-style strings end in a null character, '\0'.


Bear in mind that some of what I mentioned might not apply if item is a std::string. Often the most important bit of relevant information in a post, the data type, seems to be frequently omitted by forum posters. I've never figured that out.

#7
nick3

nick3

    Newbie

  • Members
  • PipPip
  • 29 posts

WingedPanther said:

My concern is that the gdb results may be misleading. C++ scoping rules can cause odd things to happen.

It doesnt matter if I change the name from i to something else...
The other stuff is not a problem atm I just want to know how my int can suddenly be a wierd number when I initilize it to 0.

Thanks for answers thou :)

#8
TkTech

TkTech

    The Crazy One

  • Moderators
  • 1,396 posts
You're doing it wrong.

for(int i = 0; i < 20; i++)
    item[i] = ' ';

This is assuming the array is 20 elements long and its actually a char. For some reason I get the feeling item is an array of ints and you actually think assigning a space to an an int will zero it.

#9
nick3

nick3

    Newbie

  • Members
  • PipPip
  • 29 posts

TkTech said:

You're doing it wrong.

for(int i = 0; i < 20; i++)

    item[i] = ' ';

This is assuming the array is 20 elements long and its actually a char. For some reason I get the feeling item is an array of ints and you actually think assigning a space to an an int will zero it.

The array (item) is 20 elements long, its an array of chars. I can add one char at a moment, but I cant loop becouse the int i (same if i change name) for some reason gets the value -858993460 for som odd reason

#10
manux

manux

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 234 posts
dcs answered your question,

" " is different from ' '
' ' is the space character 0x20
" " is a string, its a pointer to a character array somewhere in your memory. ('\0' terminated string)
so doing table[i] = " ";
is similar to doing table[i]=&somevariable; which usually gives a "big" number(vm starts at like >2^24) if gdb doesnt see this number right(if it thinks its signed) it might even appear negative.
so I think what you want to do is put ' '(space character) everywhere, just use ' '
now,(or even table[i]=0x20).
If you want to put zeros everywhere(as said in your title) just do table[i]=0; its as simple as that :P

#11
nick3

nick3

    Newbie

  • Members
  • PipPip
  • 29 posts

manux said:

dcs answered your question,

" " is different from ' '
' ' is the space character 0x20
" " is a string, its a pointer to a character array somewhere in your memory. ('\0' terminated string)
so doing table[i] = " ";
is similar to doing table[i]=&somevariable; which usually gives a "big" number(vm starts at like >2^24) if gdb doesnt see this number right(if it thinks its signed) it might even appear negative.
so I think what you want to do is put ' '(space character) everywhere, just use ' '
now,(or even table[i]=0x20).
If you want to put zeros everywhere(as said in your title) just do table[i]=0; its as simple as that :P


I may have been unclear, but I want to have a blank space on all 20 elements in the array, the i (from 0 to 20 (should be 19 i know...) is just the place in the array to fill it with blanks... but i doesnt get 0-19 it starts at -858993460 instead...

#12
Guest

Guest

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 3,414 posts
Try this code:
int i;
for(i = 0; i < 20; i++)
item[i] = ' ';
Sometimes strange things happen to me when I declare variables in weird places. Just declare them all at the beginning.
Root Beer == System Administrator's Beer
Download the new operating system programming kit! (some assembly required)