pointer problem

This is a discussion on pointer problem within the C Programming forums, part of the General Programming Boards category; Hi! I have problem with POINTER. I read two books but I did not understand the pointer. I know that ...

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    16

    pointer problem

    Hi!
    I have problem with POINTER.
    I read two books but I did not understand the pointer.
    I know that the & operator is unary operator that returns the address of its operand.
    Code:
    int  y=5;
    int *ptr;
    ptr=&y;
    asigns the address of the variable y to pointer variable ptr.
    The question is:
    when should we use a pointer?
    why should we use a pointer?

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >The question is:
    >when should we use a pointer?
    >why should we use a pointer?
    The answer is: Just keep on trucking and you'll find yourself in situations where you need a pointer. As you encounter those situations, add them to your list if when and why. Seriously, if you have to ask, you probably aren't at a point where you would care even if we told you the dozens of useful ways and reasons to use pointers.
    My best code is written with the delete key.

  3. #3
    Sr. Software Engineer filker0's Avatar
    Join Date
    Sep 2005
    Location
    West Virginia
    Posts
    235

    Another answer

    Quote Originally Posted by peterx
    The question is:
    when should we use a pointer?
    why should we use a pointer?
    There are many situations where pointers are required in C programming. Any time you want to pass an item (not just its value) to a function is one such situation -- By default, C is a pass-by-value language, meaning that changes to the value of a parameter within a function will not be reflected in the code that calls that function. Also, passing structures as arguments to functions (C90 and later) by value is rather slow, as the structure must be copied, so using a pointer to the structure, which mearly involves copying an address (what the actual value of the pointer is) makes more sense.
    Most times that you're dealing with strings or arrays, you'll find yourself dealing with pointers whether you know it or not.
    Overall, pointers are an essential part of programming in C (and in many other languages as well), and you'd do well to gain a good understanding of them.
    Insert obnoxious but pithy remark here

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >By default, C is a pass-by-value language
    C is only a pass-by-value language. There's no other option. However, you can simulate pass-by-reference semantics with pointers. It's a subtle difference, I know. But things don't fall into place until you get it.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointer to pointer realloc problem
    By prakash0104 in forum C Programming
    Replies: 14
    Last Post: 04-06-2009, 09:53 PM
  2. Another pointer problem
    By mikahell in forum C++ Programming
    Replies: 21
    Last Post: 07-20-2006, 08:37 PM
  3. Pointer problem
    By mikahell in forum C++ Programming
    Replies: 5
    Last Post: 07-20-2006, 11:21 AM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 08:48 PM
  5. pointer problem
    By DMaxJ in forum C Programming
    Replies: 4
    Last Post: 06-11-2003, 01:14 PM

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