Reputation: 887
I've googled a lot now, and I still can't find any solution to this... The thing is, that I'm trying to make a program that stores different points in a coordinate system, and displays them on the screen (it will later turn into a type of graph, but I'm not quite there yet). But unfortunately, I've had some issues with this...
I've decided to store all the the points in addresses after each other, of type string, like this:
string p;
string * pointer = &p;
p = "5, 3";
*(&p+1) = "6, 4";
*(&p+2) = "7, 5";
cout << *pointer << *(pointer+1) << *(pointer+2);
Or this:
string p;
string * pointer = &p;
p = "5, 3";
*(pointer+1) = "6, 4";
*(pointer+2) = "7, 5";
cout << *pointer << *(pointer+1) << *(pointer+2);
But whenever I get to line 4 or 5, I get an error on this row in the memcpy assembly:
mov [edi],al ;U - write second byte to destination
So apparently this doesn't work...
I'm starting to suspect that it has something to do with the fact that the pointer points to an address of type string, which consists of char arrays, but I'm not sure why nor how... If it now is like this, why would it even be possible to use string pointers?
Regardless, any solution/explaination is appreciated, really. I haven't really used pointers that much in the past, so excuse me if I'm missing something obvious. But as said, I've tried searching for this, and I can't find anything.
Upvotes: 1
Views: 198
Reputation: 320531
Your first code will not even compile.
Your second code attempts to store values into std::string
objects presumably located after p
in memory. But there are no std::string
objects after p
in memory. Any attempts to "store" anything into those non-existing objects leads to undefined behavior.
If you declared your p
as an array
string p[3];
string * pointer = p;
*pointer = "5, 3"; // same as `p[0] = "5, 3"`
*(pointer+1) = "6, 4"; // same as `p[1] = "6, 4"`
*(pointer+2) = "7, 5"; // same as `p[2] = "7, 5"`
cout << *pointer << *(pointer+1) << *(pointer+2);
then the second version of the code would safely store the strings into consecutive elements of that array.
But what you have now just doesn't make sense. And it is not immediately clear what you are trying to do.
Upvotes: 4
Reputation: 5101
First of all, you take the address of a std::string object and you increment it - std::string is not (w)char[]
. It does store its data contiguously but it also stores some meta-data in an implementation defined manner. So you're overwriting something that just happens to be in the memory - and you're overwriting it with something that mismatches its type, therefore it won't even compile.
When manipulating std::strings, you can use the operator[] that will give you access to the actual data of the string.
That said, I am pretty sure you do NOT want to store random pointers to string. Ideally, make a class Point<T>
with T x, y;
data members to store the coordinates.
Upvotes: 0
Reputation: 87957
Don't use pointers, they are difficult, and you have some serious (and strange) misunderstanding about how they work. Just use a vector.
#include <vector>
struct Point
{
Point(int xx, int yy) : x(xx), y(yy) {}
int x;
int y;
};
std::vector<Point> p;
p.push_back(Point(5,3));
p.push_back(Point(6,4));
p.push_back(Point(7,5));
I've defined a simple Point
class because that also seems sensible if you are writing a program about points. But if you really want to store your points as strings then replace std::vector<Point>
with std::vector<std::string>
.
Upvotes: 0
Reputation: 339
Why don't you simply do this:
string p1 = "5, 3";
string p2 = "6, 4";
string p3 = "7, 5";
cout << p1 << p2 << p3;
If you have more points, you could use a vector like that:
vector<string> points;
points.push_back("5, 3");
points.push_back("6, 4");
points.push_back("7, 5");
cout << points[0] << points[1] << points[2];
By the way, why do you want to store coordinates in a string instead of a float array for example ?
Upvotes: 0