LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   array in a function c++ (https://www.linuxquestions.org/questions/programming-9/array-in-a-function-c-793779/)

siaswar 03-07-2010 08:38 AM

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;
In a function when I send as parameter, in a for loop it prints the content of array and out of a for loop it prints the address of arr[i]

johnsfine 03-07-2010 09:30 AM

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.

truth believer 03-08-2010 08:10 PM

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.

siaswar 03-09-2010 01:47 AM

Quote:

Originally Posted by truth believer (Post 3890889)
You should see ....

Quote:

Originally Posted by johnsfine (Post 3889217)
We can't guess at your error from that little information...


this is my code:
Code:

void inc(int num[], int size);

int main()
{
  int jum[2];
  jum[0] = 1; jum[1] =8;
  inc(jum,2);
  return 0;
}

void inc(int num[], int size)
{
 int carry = 0;
  int temp = 0;
  num[size-1]++;
  cout << num[size-1] << endl; //prints 9 and this is correct
  for(int i = size; i > 0; --i)
  {
    temp = num[i]+carry; //wrong

    cout << temp << "===> inside for loop" << endl; //but this is wrong
    if(temp >= 10)
    {
      cout << temp << endl;
      carry = temp/10;
      num[i] = temp%10;
    }
  }
  for(int i = 0; i < size; i++)
    cout << num[i];
  cout << endl;
}

and this is output:
Code:

9
134514896===> inside for loop
134514896
13451498===> inside for loop
13451498
18


graemef 03-09-2010 02:51 AM

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)
or
Code:

for (int i = size-1; i > 0; --i)

johnsfine 03-09-2010 07:43 AM

graemef is correct:

Quote:

Originally Posted by graemef (Post 3891193)
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.

But ...

Quote:

Either have a loop counter of:
Code:

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

The OP clearly needs that loop to run backwards. So that suggestion doesn't fit.

Quote:

or
Code:

for (int i = size-1; i > 0; --i)

The intent of the original code is unclear, but I think you're overlooking half the problem with the original line. The > should have been >=.

Personally I have a strong preference for:

Code:

for (int i = size; --i >= 0;)
If you consistently write your backwards loops that way, you avoid confusion.


All times are GMT -5. The time now is 03:07 PM.