pointers question

FishBoy

Feeeesh
Joined
1 Aug 2004
Messages
1,685
hey i just wanted to ask how to use the '->' pointer in c++
im getting this error when i compile i'd attach the source code but they are in 5 files
layout.cpp:58: error: base operand of `->' has non-pointer type `WwText'

here is a small part of it
that's the struct definition
Code:
struct Field{
	int left;
	int top;
	int right;
	int bottom;
	WwText &txt;
};
Field data[50];
int rect;
and here is where i initialized them
Code:
int Layout::add(int left,int top,int right,int bottom,WwText&txt){
...
data[rect].left=left;
data[rect].top=top;
data[rect].right=right;
data[rect].bottom=bottom;
data[rect].txt=txt;
//the txt on the right side is a reference too in a function parameter
//edit: plus im not sure if i declared it right
...
...
...
58: str=new(nothrow)char[data[rect].txt->length()];
//and that's line 58 where it's giving me that error
im getting the error on a couple of other lines but they're the same idea, if that makes enough sense (hopefully) how could that be fixed
 
fishboy, you always have programming questiona...
are you a computer science student or this work related? (this is just for my own personal curiosity, feel free to ignore it)
 
lol yea im still a student, computer programming though not computer science, it's for one of my assignments, the last one actually :D, we have to do a word wrapping program
 
that's cool, U of T?
I guess this means that when I have simple programming questions I can ask you then.
Don't worry they aren't usually that hard.
 
no im at seneca college, even though it's a college but it's still really good in computer studies, they have a strong computer program, i think in 4 months they tought us what they tought my friend in 2 semesters in C++ he goes to smu in texas, but yea sure i could help you with your programming stuff, im good up to where i am, but i still need a couple of tips here and there every now and then
 
well i fixed the error i replaced the '->' with a dot and it worked but i still dont know why, like what does the arrow do exactly
 
you would have needed a data type of "ptr" to use pointers - when you changed that to a dot you were switching to array subscipting - which would work since you had an integer data type for the operand.

Pointers you would typically use for much more abstract storage addressing - they can be more powerful used correctly, but need to be understood correctly too.
 
Mainframeguy said:
you would have needed a data type of "ptr" to use pointers - when you changed that to a dot you were switching to array subscipting - which would work since you had an integer data type for the operand.

Pointers you would typically use for much more abstract storage addressing - they can be more powerful used correctly, but need to be understood correctly too.

No.

A pointer is:

char * mychar;

This is a character pointer, currently pointing at nothing.

char mycharb[5] = "bah\0";

Is a character, which is not a pointer.

You can assign mycharb to mychar like this:

mychar = &mycharb;

And now mychar points to the memory location at which mycharb is located. But you normally use pointers for text, and other such things so that you can new them on the fly, thus adding more memory if needed, rather than a fixed length:

mychar = new[] char[VARIABLELENGTH];

Now mychar is pointing to the first part of the memory location that new returned, and thus can be used just like any other pointer.

The difference between the dot and the -> is simple.

. is the <struct> <memberof> <variable> whereas -> is <struct ptr> <redirection to right part of memory> <variable>.

So if it is a pointer you use ->, of it is a normal struct you created on the stack(MyStruct blah;) rather than on the heap (MyStruct * blah = new MyStruct; ).

It can be hard to differentiate between the two, so it is a matter of trying, or keeping track really well :p

edit:

For a better example of pointers, i will use std::string:

std::string * mystring;

mystring = new std::string();

Now, we can use all the standard operators to put text into this string (>>).

The thing is, that since this is allocated on the heap, it will not be destroyed when the currently <thing (Can't remember)> goes out of context.

the advantage is, that since it is allocated dynamically, a few lines after using mystring, you can delete it:

delete mystring;. The default destructor is called, and mystring is gone. Whereas an std::string declared as:

std::string myotherstring;

Would stick around, with no means to destroy it on the spot to claim back memory.

To get the size() of the std::string for mystring, these can be done:

mystring->size(); // The -> means follow the memory location
(*mystring).size(); // The * means you want to derefence it

With myotherstring:

myotherstring.size(); // Using the . to say i want this member of it.

Using pointers is a painfull task, and should not be done unless you are really proficient at C and or C++, it can cause all kinds of errors that are hard to debug. There are only a few reasons why you would want to use pointers in C++ and or C, but none of them would apply to a simple little program. Stick to variables that are on the stack, it will make your life easier.
 
wow thx x-istance, yea i do need to use pointers for my assignments, iim dealing with strings and stuff on the 2nd ass't i had to write a word wrapping program i handed it a month late, and for the one due next week i have to use that word wrapping program to extract the text to a screen in small boxes and stuff, too complicated im lost about it i dont know how to do it
 
sorry 'bout that - been a while since I coded any C - and it shows.... thanks for the correction X
 
Mainframeguy said:
sorry 'bout that - been a while since I coded any C - and it shows.... thanks for the correction X
yea that's the thing that i hate the most when you dont program for a lil while and then poof it all goes away it happened to me at the beg. of this semester almost dropped out of my program coz i was so lost but then i got back on track
 

Members online

No members online now.

Latest profile posts

Also Hi EP and people. I found this place again while looking through a oooollllllldddd backup. I have filled over 10TB and was looking at my collection of antiques. Any bids on the 500Mhz Win 95 fix?
Any of the SP crew still out there?
Xie wrote on Electronic Punk's profile.
Impressed you have kept this alive this long EP! So many sites have come and gone. :(

Just did some crude math and I apparently joined almost 18yrs ago, how is that possible???
hello peeps... is been some time since i last came here.
Electronic Punk wrote on Sazar's profile.
Rest in peace my friend, been trying to find you and finally did in the worst way imaginable.

Forum statistics

Threads
62,015
Messages
673,494
Members
5,621
Latest member
naeemsafi
Back