LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-07-2010, 08:38 AM   #1
siaswar
Member
 
Registered: Aug 2009
Distribution: xubuntu
Posts: 39

Rep: Reputation: 16
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]
 
Old 03-07-2010, 09:30 AM   #2
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
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.
 
Old 03-08-2010, 08:10 PM   #3
truth believer
LQ Newbie
 
Registered: Dec 2008
Location: Tehran, Iran
Distribution: Mandriva - DSL
Posts: 17

Rep: Reputation: 1
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.
 
Old 03-09-2010, 01:47 AM   #4
siaswar
Member
 
Registered: Aug 2009
Distribution: xubuntu
Posts: 39

Original Poster
Rep: Reputation: 16
Quote:
Originally Posted by truth believer View Post
You should see ....
Quote:
Originally Posted by johnsfine View Post
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
 
Old 03-09-2010, 02:51 AM   #5
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
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)
 
1 members found this post helpful.
Old 03-09-2010, 07:43 AM   #6
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
graemef is correct:

Quote:
Originally Posted by graemef View Post
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.
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
What array is this C function expecting? Shay Programming 3 05-07-2009 09:35 PM
Bash array to function rejeep Programming 5 11-27-2007 02:05 PM
Perl - returning array from a function rose_bud4201 Programming 6 07-13-2007 01:02 AM
sending pointer array to function marek Programming 4 04-15-2004 04:46 PM
c++ function & array question shaggy112 Programming 1 05-24-2001 10:41 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:05 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration