array in a function c++
hi
When I deal with an array in a function I con not access to the content of array in a for loop, but out of a for loop I can access to them!!! for example Code:
cout << arr[i] << endl; |
We can't guess at your error from that little information.
A complete runnable example is much better when asking that kind of question. If you can't provide that, you need to do a much better job of guessing which parts will be relevant. Your description makes it sound like the way the array was passed to the function is an important part of the problem, so we would need to see how the array is declared in the calling code, how the function is called, how the function is declared and how the array is used in the function. |
You should see what the for loop does with it. If the for loop uses i as counter, then it comes to make sense.
I don't get it because I don't know the code. I didn't get if inside a for loop you get the contents, or out of it (you said both!). cout gets pointers to null terminated character arrays. Just saying : Well, if array "arr" is an array of characters, and arr is a pointer to characters. then you should say: cout << arr << endl; and if cout << arr[i] << endl; prints the contents, anywhere, inside a loop or outside of it, so array "arr" is an array of arrays of characters, and arr[i] is a pointer to characters. |
Quote:
Quote:
this is my code: Code:
void inc(int num[], int size); Code:
9 |
Well your loop is wrong.
You start with the loop counter being 2, the code will then look at the element indexed with a 2, which doesn't exist which is why you are getting the "strange" results. Either have a loop counter of: Code:
for (int i = 0; i < size; ++i) Code:
for (int i = size-1; i > 0; --i) |
graemef is correct:
Quote:
Quote:
Quote:
Personally I have a strong preference for: Code:
for (int i = size; --i >= 0;) |
All times are GMT -5. The time now is 03:07 PM. |