Jump to content

Pointers: why?

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
17 replies to this topic

#1
Phoenixz

Phoenixz

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 256 posts
Why use pointers, I talk of C. Why would I benefit using the memory address of a variable per say, when I could just use the variable name?

Just.. anyone who has had experience in a way that this has been neccesary for something more than an exercise please share your experience! :)
Posted Image

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
Pointers allow you to dynamically allocate memory. C also uses pointers to handle the "pass by reference" concept so that scanf can return data in the indicated variables.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
chili5

chili5

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 7,247 posts
If you dynamically allocate your memory. You can work with larger amounts of data without getting overflow errors. :)

#4
Termana

Termana

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 4,057 posts
Lies. They put it in there so only people who were able to grasp advanced computing theory could use the language and then they just wanted to give the people who use the language a hell of a time trying to track down pointer related errors in their programs. No I'm joking (besides the fact I don't think pointers are a hard concept, but to each their own I guess).

Interested in participating in community events?
Want to harness your programming skill and turn it into absolute prowess?
Come join our programming events!


#5
jwxie518

jwxie518

    Speaks fluent binary

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,180 posts

Phoenixz said:

Why use pointers, I talk of C. Why would I benefit using the memory address of a variable per say, when I could just use the variable name?

Just.. anyone who has had experience in a way that this has been neccesary for something more than an exercise please share your experience! :)

well this is what panther mentioned to me, just one of the benefits you can get from pointers and references
http://forum.codecal...html#post126437

i think this is also true with c, since c++ comes from c

it's basically what panther and chill5 had mentioned above
i am only a beginner, but i think so far the basic concept, i think that's what it is

#6
fread

fread

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 787 posts
Why?
C was developed when computers were much less powerful than they are today and being very efficient with speed and memory usage was often not just desirable but vital. The raw ability to work with particular memory locations was obviously a useful option to have. A few tasks these days, such as programming microcontrollers, still need this. However most modern programmers do not need such fine control and the complications of using pointers make programs less clear to understand and add to the ways in which they can be go wrong. So why are pointers still used so much in C & its successor, C++?

The reason is that pointers are used to bodge into C some vital features which are missing from the original language: arrays, strings, & writeable function parameters. They can also be used to optimize a program to run faster or use less memory that it would otherwise.

Full article at: Why C has Pointers
Perfection of means and confusion of ends seem to characterize our age. Albert Einstein :confused:

#7
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
First comment: do NOT read the referenced article. It is loaded with misinformation or confusing information. Any article that described pointers as "confusing" is probably written by someone who doesn't really get them.

Second comment: fread, it looks like you quoted heavily from the blog you referenced. It completely fails to explain the true purpose of pointers. It also fails to note that almost every compiled language has pointers. Pascal, Delphi, Java (called references), Fortran, and many many others have pointers.

Final comment: data structures are essentially IMPOSSIBLE without pointers. This includes linked lists, trees, stacks, queues, etc. While pointers can be confusing at first, they are essential to advanced programming skills.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#8
fread

fread

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 787 posts
I did quote from the blog..thats why i said full article at->..
I thought it was fairly simple....but most importantly, short...

A lot of people seem to agree on the difficulty of pointers vs many other concepts especially in C. And it is confusing if you dont practice it, if you dont use it for work or often enough it remains confusing, code with pointer errors in my opinion are amongst the hardest to debug.

Saying pointers are confusing doesn't mean you will never understand it. It just means you should pay close attention when doing it.
What are your thoughts panther:>
Chapter 10: Pointers

i thought pheonix already understand what a pointer is and how it works. Just wanted the 'why pointers'. Programmer?
My apologies if i misunderstood you.
Perfection of means and confusion of ends seem to characterize our age. Albert Einstein :confused:

#9
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
I think the Chapter 10 does a nice job of explaining the concept.

I agree that pointers tend to cause a lot of confusion when first introduced. I taught a course on programming logic, and both arrays and pointers caused a lot of issues for people. I think part of the problem is that people try to think of programming variables the same as math variables. Arrays start to erode that model, and pointers start to REALLY trash that model of programming variables. Since many people have difficulty adjusting to what variables in programming really represent (due to poor instruction, or a rigid mindset, or some combination of the two), they tend to also have difficulty understanding pointers.

To further complicate matters, many people only have a basic course in programming and then move on (they had to satisfy a general education requirement, for example). Because algorithms and data structures are generally reserved for more advanced courses (after C/C++/Java/Other is mastered), many people are exposed to pointers, but not the true purpose of them.

To further confuse matters, many languages, such as various .NET languages and Delphi, have pre-defined abstract data types. When you don't have to implement your own list type, you can get away with not directly using pointers and not realize that you are using them heavily. The result is that I am able to do fairly advanced programming, with heavy database access and dropdown, and never have pointers forced on my awareness. As long as the structures I'm using are well-designed and don't require tweaking for my purposes, I can ignore the implementation details. If I have to implement my own version, however, I will rapidly be neck-deep in pointers.

Phoenix's question is valid, and I suspect it's a common question when learning languages that use pointers heavily. I suspect one of the indicators of a high-level language is the ability to program with minimal use of pointers. C and C++ are low-level, and one side-effect is that they use pointers extensively.

fread: I realize I may have come across harsh in my last reply. That wasn't my intention and I apologize if I sounded upset. I just don't want misinformation getting out. Programming languages have come a LONG way in the last 20 years, and many people don't remember when the concept of a window was a radical concept.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#10
Phoenixz

Phoenixz

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 256 posts
I just don't see much sue for them still, perhaps due to my lack of experience in a real world enviroment
Posted Image

#11
Chris_Williams

Chris_Williams

    Newbie

  • Members
  • Pip
  • 2 posts
basically what everyone is saying, pointers are their so you can allocate memory that you can use when you need too. So there's no memory overflow

#12
cook777

cook777

    Newbie

  • Members
  • PipPip
  • 13 posts
You can use pointers to make a set of numbers in array to be made small to biggest (or any other order) with out changing the order of the original array or making a copy and having two arrays for the same thing just one in a different order.

Edited by cook777, 21 March 2009 - 02:54 AM.
person just above answered what I had there so I removed it.