#### Archived

This topic is now archived and is closed to further replies.

# Pointers - needed as a beginner?

## Recommended Posts

I''ve been learning c++ for a bit, mainly off www.cprogramming.com Now exams are over and I have a few free months, in which I aim to get a decent understanding of the language. However, as many other new people, I have come across a few problems with pointers. I think I understand them, I will a bit better when I''m more awake, but my question is: As a beginner, do I need pointers yet? I mean I could learn some more, and do some basics such as a blackjack game or text based games, and then come back to pointers when I probably understand them better? Or should I make sure I learn and understand them now before proceeding? Thanks for any help. What about me? What about Raven?

##### Share on other sites
FunkyTune    122
In my opinion, pointers are such an important part of C/C++, that you have to understand at least the basics of them (how to declare and assign values to them, and their usage in functions), before trying to make a game.

I''m sure it''s possible to do a game without them, but they will make things so much easier, so if you ask me, learn about pointers.

/John

##### Share on other sites
TheHermit    122
Learn Pointers.

Pointers are good for you, they keep your programs trim and in good shape, as well as excercise the mind (for the first couple of months anyway)

Pointers are a necessary evil with any c-based language, and allow incredible powers of production (and for that matter destruction) of programs.

The choice you have is spend x effort to learn pointers asap, or spend x^2 effort remembering to use them in all the places you should have but didn''t becuase you did not know any better.

Once more...

Learn Pointers.

##### Share on other sites
SabreMan    504
What problem do you have which you think requires a pointer as part of the solution?

##### Share on other sites
merlin9x9    174
Internally, pointers are used for just about everything—and I mean that literally. Now, from the programmer''s point of view, pointers are often used to implement certain data structures or. More generally, they''re used to access objects indirectly.

Don''t listen to the hype that pointers are "evil" or "dangerous." The alternative (references) is not much safer than pointers on a modern operating system, and the rest of the difference is syntax. In a language that allows both pointers and references, it''s often good to use pointers to make it clear that the object passed indirectly may be modified—in contrast to references which, conincidentally, look exactly like pass-by-value.

##### Share on other sites
Guest Anonymous Poster
quote:
Original post by sanguineraven
I''ve been learning c++ for a bit, mainly off www.cprogramming.com
Now exams are over and I have a few free months, in which I aim to get a decent understanding of the language. However, as many other new people, I have come across a few problems with pointers.
I think I understand them, I will a bit better when I''m more awake, but my question is:
As a beginner, do I need pointers yet? I mean I could learn some more, and do some basics such as a blackjack game or text based games, and then come back to pointers when I probably understand them better? Or should I make sure I learn and understand them now before proceeding?
Thanks for any help.

Concept behind pointer isn''t difficult. It just the location of something in memory or in a file. For me anyway the hardest part was keep the syntax straight.

##### Share on other sites
I find the thing with anything I learn is that I don't understand it until I see it used.

When a book explains something and shows a demo to explicitly demostraight HOW to use something I understand it, but my thoughts go along the lines of "Now why the hell do we need this!?".

When they start using it generally and practically without explicit use of it here, there and everywhere - then I start to understand it.

What I'm saying is that, I, along with quite a few people was clueless as to the point of pointers, until it's shown in a decent way, but I see the authors' reasoning behind pointers-only-demo-that-didn't-needpointers-and-just-for-no-reason-use-them.exe because they don't want to confuse you with other stuff. If you haven't learnt the reference/alias-variable things yet (I forget the proper name) then learn them, the tutorial might help you understand the pointer thing better.

[edited by - garconbifteck on June 10, 2002 3:24:28 PM]

##### Share on other sites
LockePick    707
I''m still looking for a good source to answer the "Why the hell do you use a pointer?" question. To me it''s just a couple extras lines and symbols to use a variable. And I''m making my own engine! *cackles*
Er, as a beginner you don''t need to learn how to use pointers, you should though. Or you''ll end up like me

------------
aud.vze.com - The Audacious Engine <-- It''s not much, yet. But it''s mine... my own... my preciousssss...
MSN: nmaster42@hotmail.com, AIM: LockePick42, ICQ: 74128155

##### Share on other sites
nicho_tedja    122
Pointers are gods that can make your life easier or worse. Worse if you don''t know how to use them. Easier if you know their mighty powers.

You''ve got many responsed from people before me, you should read them because they''re all correct. But, I''ll point out something:
I know this is how people teach you using pointers:

  int a, *pa;a = 10;pa = &a;*pa = 12;   // assign a value to a variable using pointersa = 12;    // why don''t we just do it this way??

Frankly, this is not what (usually) pointers are used. The example just shows you how pointers work, but we never use them that way. It''s just stupid, allocating 2 variables for 1 value, what a waste of memory. Learn how to allocate memory, that''s where you can see the wonderful part of pointers. Imagine...storing values without variables...how godly is that!

My compiler generates one error message: "Doesn''t compile."
-Albert Tedja-

##### Share on other sites
Geocyte    196
An obvious use for pointers is dynamic memory allocation. Say you want an array of ints but you want the user to decide exactly how many ints there are:

int *int_ptr;

int_ptr = new int[user_supplied_value];

usually you would be better off using std::vector here but that essentially uses pointers to achieve this effect.

Then you could have a pointer acting as a cursor to the currently selected int to allow the user to select and modify one of the values.

Why not just use an unsigned int representing the array index for this? Well you may decide you want the user to manupulate some other random int which isn''t part of an array or anything. Because you used a pointer you can just assign the pointer to your random int and the code to allow the user to manipulate ints via the cursor can remain unchanged.

Various other pointer related delights lie in store for you. Pointers basically allow you to manipulate memory directly which gives you almost total control and is also very fast. As an exercise in pointers you could try knocking up a string class or a linked list class.

Geocyte Has Committed Suicide.

##### Share on other sites
merlin9x9    174
Don''t forget to delete[]!

##### Share on other sites
JWalsh    498
Heya Raven,

In my opinion, learn pointers and learn them now.

Here''s how:
1. Go grab a good book on how to code data structures.
3. Then code the data structures yourself.
4. Make sure to add an Insert(), Delete(), Search(), and Sort(), without looking back at the book if you can.

When all is said and done, and you''ve spent your 36 hours wondering why you keep getting memory errors, you''ll in the end, for better or for worse, understand pointers.

Best Regards,
Jeromy "Maverick" Walsh
------------------------
"The question isn''t how far, the question is do you possess the constitution, the depth of faith, to go as far as is needed?" -Boondock Saints

##### Share on other sites
Xai    1848
In my opinion you learn pointers early .. and they are important .. but NOT before you understand other groundwork ...

I had written plenty of stuff in GWBASIC as a kid, and then PASCAL before I knew pointers .. and I did just fine ... you will find this ... if you don''t know pointers ... to get any real work done you will use a lot of arrays and interger indexes ...

But you should NOT try to learn pointers before these things:

variable declaration, assignment, and comparison
conditionals (if, while, for)
function declarations and calls
basic I/O (using cin and cout)
simple file I/O

THEN you have two choice to go to next ... simple structs or classes, or pointers ... BOTH are easy to use, hard to completely understand as a beginner, and USED TOGETHER ... being able to write with CLASSES is almost useless without pointers ... because almost all abstract data structures (like lists, vectors, strings, stacks) ... use pointers ... and all dynamically sized things use pointers ...

##### Share on other sites
felonius    122
Pointers are great, but they *are* hard to grasp for beginners. About the first 7-8 years of programming I did was without the use of pointers (or references for that matter) and I still managed to get a lot of stuff done. BASIC doesn't even have pointers (modern variants have references, though) so I will claim that you can get along perfectly well without pointers if you use a language such as BASIC.

However, if you insist on using C or C++ you will have a hard time avoiding pointers and you might as well just learn how to use them properly.

Just my 2 c.

[edited by - felonius on June 11, 2002 4:21:06 PM]