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

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.

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.

Click to expand...

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

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

Click to expand...

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