Confused about Pointers

This is a discussion on Confused about Pointers within the C Programming forums, part of the General Programming Boards category; I am taking a beginners C course and we are starting to learn about Pointers. I am somewhat confused as ...

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    42
    I am taking a beginners C course and we are starting to learn about Pointers. I am somewhat confused as to the value of them. My instructor said they make programming "fun", though they don't seem very fun to me I can't quite grasp it.
    From what I can tell a "pointer" points to a address in the memory as part of it's "use". Ok, I got that. But for coding why use a Pointer, when you can just use an Array? Etc etc.

    I am probably missing something obvious here, but Pointers just seem like a different way to go about it, albeit not necessary. I am sure I am wrong though. Can someone break it down into layman's terms? What is the value? And where would it be most value to use a Pointer?

    This isn't for homework, it's for my own knowledge!

    Thanks

    Or if someone can point me to a good article about the use of Pointers, that would be great too.

    Thanks!

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    2,541
    An C array is implemented as a pointer; without the idea of pointers you could only return a single value from a function. With pointers/pass by reference you can change multiple non-global variables (outside the function) in a single function.

    Tim S.

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    42
    Quote Originally Posted by stahta01 View Post
    An C array is implemented as a pointer; without the idea of pointers you could only return a single value from a function. With pointers/pass by reference you can change multiple non-global variables (outside the function) in a single function.

    Tim S.
    Ahh ok, that sort of makes sense now. It makes your program more flexible without changing a specific function.

    Thanks

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    There are many situations where "why not just use an array" is in fact the right answer.

    Some of the reasons to use pointers...
    1) You need to create and destroy arrays, structures, lists etc. "on the fly" inside your program. Linked lists are a good example of this.

    2) You need to make room for something REALLY BIG like loading a 30 megabyte file. This would almost certainly crash your program if you tried to do it with char array[30000000];

    3) You don't know in advance how big something needs to be. In these cases you use malloc() and free() to get around the problem of not knowing the size of an array at compile time.

    Does that help?

  5. #5
    Registered User
    Join Date
    Feb 2011
    Posts
    43
    somebody might be able to correct me if i am wrong but here is a basic understanding of it

    computer memory is cmos technology( which is a type of transistor) which we could get into a whole debate about cmos technology but that is not the point of this topic

    computer memory gets saved in stacks...as i am sure you teach has explained or drew the picture. so each variable gets saved into a specific memory location, and enless you make that variable static or pass by reference it will take what is previous in that memory location delete it

    so if you had a variable called test and set that equal to zero so the code would be...
    int test=0
    the variable test is being saved into a spacific memory location, and lets say for the the conversation it is saved in memory location oxo1(hex 1) or ooo1 in binary so if you had a pointer in your code say *test it would point directly to that memory location(oxo1) and you could read the value of what it( for our case it is zero, or the aski equivalent of zero) otherwise you would have a bunch of garbage in that memory location

  6. #6
    Registered User
    Join Date
    Mar 2011
    Posts
    42
    Quote Originally Posted by CommonTater View Post
    There are many situations where "why not just use an array" is in fact the right answer.

    Some of the reasons to use pointers...
    1) You need to create and destroy arrays, structures, lists etc. "on the fly" inside your program. Linked lists are a good example of this.

    2) You need to make room for something REALLY BIG like loading a 30 megabyte file. This would almost certainly crash your program if you tried to do it with char array[30000000];

    3) You don't know in advance how big something needs to be. In these cases you use malloc() and free() to get around the problem of not knowing the size of an array at compile time.

    Does that help?
    Yes, especially #2. That kind of example is what I am reading about in my book. It makes perfect sense, I think I just need to go back and break it down step by step as to HOW it works. I may have gone through it too quickly. I can definitely see the value though. Thanks guys

    Quote Originally Posted by begginer View Post
    somebody might be able to correct me if i am wrong but here is a basic understanding of it

    computer memory is cmos technology( which is a type of transistor) which we could get into a whole debate about cmos technology but that is not the point of this topic

    computer memory gets saved in stacks...as i am sure you teach has explained or drew the picture. so each variable gets saved into a specific memory location, and enless you make that variable static or pass by reference it will take what is previous in that memory location delete it

    so if you had a variable called test and set that equal to zero so the code would be...
    the variable test is being saved into a spacific memory location, and lets say for the the conversation it is saved in memory location oxo1(hex 1) or ooo1 in binary so if you had a pointer in your code say *test it would point directly to that memory location(oxo1) and you could read the value of what it( for our case it is zero, or the aski equivalent of zero) otherwise you would have a bunch of garbage in that memory location
    Thanks beginner, as I said to Tater I need to go back and re-read this. I knew it had something to do with pointing to the memory but I couldn't grasp it well. I am starting to get the idea now.
    Last edited by JoshD75; 03-28-2011 at 09:32 PM.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,588
    4) You want to pass an object to a function, but it is expensive to copy, e.g., it contains a large array.

    5) You want to pass an object to a function and have changes to that object from within the function be reflected in the caller.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by begginer View Post
    somebody might be able to correct me if i am wrong but here is a basic understanding of it
    It makes no difference, to a programmer, what technology memory uses. From the keyboard, we simply don't care about that. (but fwiw, ddr2 and ddr3 memory actually uses a matrix of capacitors that store tiny charges to represent the content of memory. Google "computer memory types" for more complete info.)

    When you launch a program it is assigned a block of memory out of the system's pool of memory. This is enough to load the program image, create a stack and do any initial memory allocations for things where the size is known at compile time.

    The stack is actually a region within the program's allocation set aside for temporary storage used by the CPU as it executes the code. When a procedure begins, the parameters and internal variables are "pushed" onto the stack where the procedure's code can have the CPU operate on them. When the function exits, it's space on the stack is relinquished and made available to the next function.

    The "heap" is a global pool of memory shared by all programs. When we call malloc() or realloc() we are asking for a chunk of that memory for our own use. When we call free() we are returning that memory back to the heap.

    A pointer can point to either the stack or the heap... depending on what kind of software legerdemain we are trying to pull at the moment. But the important thing to remember is that in modern systems we don't actually know the hardware address of that memory. It is "virtual memory" assigned as an offset from the program's entry point, not as a harware address.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. confused with pointers
    By Mahdi123 in forum C Programming
    Replies: 2
    Last Post: 04-25-2007, 01:08 PM
  3. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 05:58 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21