ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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]
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.
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.