View Full Version : pointer/array question...
inkedmn
05-17-2003, 08:06 PM
ok, i wrote this little deal to loop through an array of strings and give the (starting) memory location of each string:
#include <string>
#include <iostream>
using namespace std;
void showPtr(string x) {
string* ptr;
ptr = &x;
std::cout << x << " is located in memory at " << ptr << endl;
}
int main() {
const int MAX = 5;
int i;
string names[] = {"brett","joana","paula","joe","joey"};
for(i = 0; i < MAX; ++i) {
showPtr(names[i]);
}
}
and the output:
<|inkedmn@skank|~/code/c++/code>% ./hello
brett is located in memory at 0xbffff9c0
joana is located in memory at 0xbffff9c0
paula is located in memory at 0xbffff9c0
joe is located in memory at 0xbffff9c0
joey is located in memory at 0xbffff9c0
any reason why they're all showing up as having the same memory location? what's up with my code, i ask you? :)
EscapeCharacter
05-18-2003, 03:59 AM
hey duder, if you change it to look like this it works.
#include <string>
#include <iostream>
using namespace std;
void showPtr(string *x, int i) {
std::cout << x[i] << " is located in memory at " << &x[i] << endl;
}
int main() {
const int MAX = 5;
int i;
string names[] = {"brett","joana","paula","joe","joey"};
for(i = 0; i < MAX; ++i) {
showPtr(names, i);
}
}
i think the way you have it, its printing the address of the array and not the individual element, i could be wrong though
jamessan
05-18-2003, 09:50 AM
Here are two ways that I was able to get it to work:#include <string>
#include <iostream>
using namespace std;
void showPtr(string *x) {
std::cout << *x << " is located in memory at " << x << endl;
}
int main() {
const int MAX = 5;
string names[] = {"brett","joana","paula","joe","joey"};
for(int i = 0; i < MAX; ++i) {
showPtr(&names[i]);
}
}#include <string>
#include <iostream>
using namespace std;
int main() {
const int MAX = 5;
int i;
string names[] = {"brett","joana","paula","joe","joey"};
for(i = 0; i < MAX; ++i) {
cout << names[i] << " is located in memory at " << &names[i] << endl;
}
}If I'm thinking correctly, the reason your method wasn't working was because you are looking at the pointer of the instance variable x which is created each time you call showPtr. You're passing by value in showPtr, which is why you aren't able to see the actual address. In my first block of code, I passed the string by reference, so the instance variable is holds the address of the location in memory and you then dereference it to get the value of the string.
codejockey
07-06-2003, 02:54 PM
You are, in fact, passing the address of an array element (i.e., a pointer) to the ShowPtr function. You then take the address of the pointer with the statement ptr = &x, and this is the value you print in your cout statement. I believe what you intended was ptr = x (or, as other posters have shown, you could just use x directly; the ptr = x assignment is just a dead store).
Hope this helps.
sans-hubris
07-06-2003, 09:47 PM
Originally posted by codejockey
You are, in fact, passing the address of an array element (i.e., a pointer) to the ShowPtr function. You then take the address of the pointer with the statement ptr = &x, and this is the value you print in your cout statement. I believe what you intended was ptr = x (or, as other posters have shown, you could just use x directly; the ptr = x assignment is just a dead store).
Hope this helps.
Umm, that doesn't sound right.
inkedm, the reason all the addresses are the same is that you're passing the string in by value (and thus it's just making a local copy.) Try passing it in by reference:
void showPtr(string &x);
codejockey
07-07-2003, 12:59 AM
I stand corrected. Thanks for the info. Too much C, not enough C++ ;)
nothing
07-07-2003, 09:45 AM
Why did he use const int MAX = 5; instead of int MAX = 5; ? I'm learning C++ and I didn't get this part.
stuka
07-07-2003, 10:27 AM
nothing: a const int is a constant integer. The const flag tells the compiler that you can never change the value of that int. The point of this is that no matter what, the value of MAX will stay at 5 during the program run.
nothing
07-07-2003, 10:41 AM
Stuka: thank you!
sans-hubris
07-07-2003, 11:44 PM
Originally posted by codejockey
I stand corrected. Thanks for the info. Too much C, not enough C++ ;) Even in C, my statement still mostly stands. The only difference is that C doesn't have pass by reference, and so therefore a pointer would need to pass in a pointer to the function.
codejockey
07-10-2003, 01:51 AM
The only difference is that C doesn't have pass by reference,
Not sure I understand. C allows you to pass the address of a parameter to a function (i.e., pass by reference; the parameter is a pointer). AFAIK, C supports both pass by value and pass by reference.
I also don't understand your last comment:
therefore a pointer would need to pass in a pointer to the function.
In C, a pointer (like any other parameter) can be passed by value or by reference. Passing by value uses the value of the pointer; passing by reference uses the address of the pointer. A pointer variable doesn't have to be passed in any special way in C, nor is it limited to either pass by reference or pass by value.
I'm sure I've missed the intent of your post (apologies) so any clarification appreciated.
sans-hubris
07-10-2003, 03:27 AM
Originally posted by codejockey
Not sure I understand. C allows you to pass the address of a parameter to a function (i.e., pass by reference; the parameter is a pointer). AFAIK, C supports both pass by value and pass by reference.
I also don't understand your last comment:
In C, a pointer (like any other parameter) can be passed by value or by reference. Passing by value uses the value of the pointer; passing by reference uses the address of the pointer. A pointer variable doesn't have to be passed in any special way in C, nor is it limited to either pass by reference or pass by value.
I'm sure I've missed the intent of your post (apologies) so any clarification appreciated. You're right in saying that a reference can be passed to a function, but it's not the same thing as pass by reference. In C++, to pass a variable by reference doesn't really mean that the function is receiving a pointer address, even though that's how pass by reference works, but rather passing a value by reference means that the function that takes the reference can use the value as a local value (not a pointer value) and any changes made in that function will be reflected outside that function. Essentially, it allows a value scoped somewhere else become scoped into another function. When the function with pass-by-reference arguments is called, those arguments that were passed by reference may have their values changed after the function returns.
jamessan
07-10-2003, 09:16 AM
*Note: This is what I think sans-hubris is trying to differentiate. I could be completely wrong.*
In C, client code (code that Joe Coder writes to interface with a library) varies depending on whether a function needs to have a value passed or a reference, i.e.:foo(bar);as opposed tofoo(&bar);On the other hand, the same line of code in C++ will do two different things depending on how the implementor wrote the function.foo(bar);can either pass by value:void foo(int bar);or pass by reference:void foo(int &bar);Here's (http://www.geocities.com/sstutor/valref2.htm) a web page that better explains what I'm talking about (be sure to look at the 2nd page).
codejockey
07-10-2003, 06:15 PM
You're right in saying that a reference can be passed to a function, but it's not the same thing as pass by reference. In C++ ... passing a value by reference means that the function that takes the reference can use the value as a local value (not a pointer value) and any changes made in that function will be reflected outside that function.
I think this was the original source of my confusion. I took "pass by reference" in the C sense (i.e., a pointer); you intended "pass by reference" to mean the C++ sense (which you defined above). Clearly, C does not have the C++ "pass by reference" characteristic (in the sense that you can arbitrarily decide, for a given invocation of a function, whether to pass variables by value or reference (which is what I think you were driving at with your statement that a "pointer would need to pass in a pointer to the function").
Similarly, when you
vBulletin® v3.7.0, Copyright ©2000-2010, Jelsoft Enterprises Ltd.