pointers question

Discussion in 'Web Design & Coding' started by FishBoy, Apr 7, 2005.

  1. 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
    here is a small part of it
    that's the struct definition
    struct Field{
    	int left;
    	int top;
    	int right;
    	int bottom;
    	WwText &txt;
    Field data[50];
    int rect;
    and here is where i initialized them
    int Layout::add(int left,int top,int right,int bottom,WwText&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
  2. Ohzopants

    Ohzopants My mom thinks I'm cool

    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)
  3. 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
  4. Ohzopants

    Ohzopants My mom thinks I'm cool

    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.
  5. 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
  6. 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
  7. Mainframeguy

    Mainframeguy Debiant by way of Ubuntu Folding Team

    London, UK
    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.
  8. X-Istence

    X-Istence * Political User


    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


    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.
  9. 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
  10. Mainframeguy

    Mainframeguy Debiant by way of Ubuntu Folding Team

    London, UK
    sorry 'bout that - been a while since I coded any C - and it shows.... thanks for the correction X
  11. 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