Why use pointers?

I am new at C++,and i have problems with this pointer shit.I have read many tutorials about C++ and they say,that pointers are very important,without them its unimaginable to programming with C++,they are the heart of the C++...
And all those tutorials give this reasons why they are important:
1-Dynamic Memory Allocation(Data Structures,unknown sized arrays)
2-Parameter passing to functions
3-Array handling
I really don't understand!!!Really!!!Do only such three reasons make the pointers so important???If yes,WHY?????
Please someone help me!!!





Comments

  • : I am new at C++, and i have problems with this pointer shit. I have read many tutorials about C++ and they say,that pointers are very important,without them its unimaginable to programming with C++,they are the heart of the C++...
    : And all those tutorials give this reasons why they are important:
    : 1-Dynamic Memory Allocation(Data Structures,unknown sized arrays)
    : 2-Parameter passing to functions
    : 3-Array handling
    : I really don't understand!!!Really!!!Do only such three reasons make the pointers so important???If yes,WHY?????
    :
    Pointers are fundamental to C++ because of it's C heritage. C was invented to allow assembly language programmers to still code efficiently at a higher-level, in a portable language. Fundamentally, all the objects manipulated by a computer program exist in a computers memory, and pointers give you absolute power to do anything you want with that memory.

    Under (1) you parenthesis "data structures" as if it was some minor offshoot of dynamic memory allocation. Data structures (and the algorithms that manipulate them) are the heart of computer science. Most of them require pointers.

    Suffice it to say that pointers should be second nature. In fact, they are not hard at all - once you get it. You will wonder why you ever found them difficult at all.

    Cheers,
    Eric

  • well if you dont like pointers try java, they hide them from
    the programmer.
    and what do you mean with ONLY such three reasons?
    what else do you want to do?

  • : well if you dont like pointers try java, they hide them from
    : the programmer.
    : and what do you mean with ONLY such three reasons?
    : what else do you want to do?
    :
    :
    As I said tutorials say "Pointers are heart of C++".
    Such a expression makes me think so that the pointers have 25-50 reasons to being important:)
    But there were only three and i was really disapointed.I think this is a small number to being the heart of C++.
    However I still want to know why those reasons make the pointers so important?????
  • dont you understand them? writing functions, dynamically allocating
    memory and using arrays is almost everything you do when you are programming!

    : : well if you dont like pointers try java, they hide them from
    : : the programmer.
    : : and what do you mean with ONLY such three reasons?
    : : what else do you want to do?
    : :
    : :
    : As I said tutorials say "Pointers are heart of C++".
    : Such a expression makes me think so that the pointers have 25-50 reasons to being important:)
    : But there were only three and i was really disapointed.I think this is a small number to being the heart of C++.
    : However I still want to know why those reasons make the pointers so important?????
    :

  • [b][red]This message was edited by the VEZNE at 2002-5-15 11:2:45[/red][/b][hr]
    : dont you understand them? writing functions, dynamically allocating
    : memory and using arrays is almost everything you do when you are programming!


    I understand but there are too many functions,which dont take pointer parameters for example.
    Pointer arithmetic can be faster,but we can still use indexing with [] operators i think.
    I dont have too much ideas about the dynamic memory and i cant make certain comments about it.But i have read that STL has some data structures for us already.
    Dont kill me,but I still wonder why they are important
    Well is there any other reason to use pointers except those three reasons?


  • : Pointer arithmetic can be faster,but we can still use indexing with [] operators i think.

    Indexing is an illusion. It's [italic]still[/italic] pointer arithetic...
    [code=ffffff]
    array[i] = [color=bb0000]10[/color];
    [/code]
    is literally interpreted as:
    [code=ffffff]
    *(array + i);
    [/code]
    Which is why you can also write:
    [code=ffffff]
    i[array] = [color=bb0000]10[/color]; [color=80a0b0][italic]// *(i + array)[/italic][/color]
    [/code]
    : I dont have too much ideas about the dynamic memory and i cant make certain comments about it.

    Then you are still a beginner. Give it some time. You will understand eventually. Trying to figure it out now is the same as asking "What's the big deal with Calculus?", when you're still learning how to add and subtract.

    : But i have read that STL has some data structures for us already.

    How do you think STL is written? Also, STL covers only the most common, basic data structures. Any advanced data structures you will have to write yourself.

    Cheers,
    Eric

  • Its true that I am still a newbie:)
    But There is still a question which kills me slowly day by day:
    Are there some another reasons which make pointers important?
    Can you tell me about them?
    This was my last question about pointers, I swear!!!:)
  • [b][red]This message was edited by the PeterTheMaster at 2002-5-15 12:41:2[/red][/b][hr]

    well, the reasons you gave are very general and therefore
    cover most of it.
    i find interesting about pointers that they can hold
    adresses of functions. here is how a callback mechanism
    can be implemented. note the (very primitive) variable
    sized array. when the class is written the programmer
    doesnt know how many functions will be registered.
    [code]
    #include
    [red]typedef void(*funcPointer)();[/red]

    void show1(){cout<<"show1"<<endl;}
    void show2(){cout<<"show2"<<endl;}
    void show3(){cout<<"show3"<<endl;}

    class CParent{
    public:
    CParent(int anz){ anz_funcs=anz; [red]f=new funcPointer[anz_funcs];[/red] }
    ~CParent(){ [red]delete[] f;[/red] }
    void ShowAll(){ for(int i=0;i<anz_funcs;i++) [red]f[i]();[/red] }
    void registerfunc(int i,[red]funcPointer f_[/red]){ [red]f[i]=f_;[/red] }
    private:
    int anz_funcs;
    [red]funcPointer *f;[/red]
    };

    void was_andres(){cout<<"and now for something completely different"<<endl;}

    int main(int argc, char *argv[]){
    CParent p(5);
    p.registerfunc(0,show1);
    p.registerfunc(1,show3);
    p.registerfunc(2,show2);
    p.registerfunc(4,show2);
    p.registerfunc(3,was_andres);
    p.ShowAll();
    return 0;
    }
    [/code]

    also polymorphism has to do a great deal with pointers.
    but this might be included in your 1st and 2nd point.





  • : well, the reasons you gave are very general and therefore
    : cover most of it.
    : i find interesting about pointers that they can hold
    : adresses of functions.

    C programs can do more than just take the address of a function - they can alter the function itself, treating their own machine code as data, allowing things like overlays or self-modifying programs. Of course, this is not portable, but it's the kind of stuff that having pointers in the language allows.

    Pointers are a defining feature of the language. They can cause nightmarish bugs, but there are what allows C to be used as a "high level assembler", and make it so popular in fields like systems programming and embedded programming.

    Cheers,
    Eric


  • i love the concept of self modifying code.
    i once made a fast real time lens on a 286.
    that was fun!
    i started with a pascal program and translated
    it step by step into assembler.
    and then i found this amazing possiblity.
    calculate a value once outside a loop
    and having a constant in some add and mul
    commands in the loop.
    i was so proud of myself. but noone
    i told about it at school understood what i
    was talking about.

    when i see this doday i realize 2 things:
    1) i dont understand what i wrote any more.
    2) the pascal program is as fast as the assembler. :-(
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories