0

So pointers are something a lot of people (including myself) get a little confused on. I understand how they work, but when im making an application or whatever....how do I know when it would be best to use them?

Is there like a general rule/guideline of when it's actually a good idea to use them. I feel like I understand their concept for the most part, but always struggle in wondering if I should be using a pointer here or there etc...

Thank you.

Cloud
  • 18,753
  • 15
  • 79
  • 153
  • 4
    Your question is tagged `c` and `c#`. These languages couldn't be more different in the context of this question. – Sam Harwell Oct 21 '13 at 17:54
  • 1
    @Dogbert, what evidence you have that the OP is asking about C instead of C#? – Scott Chamberlain Oct 21 '13 at 17:56
  • You use them when they're the best solution to the problem. Nobody can list all possible scenarios where pointers are the best solution, neither would such a list be on-topic for Stack Overflow. – user229044 Oct 21 '13 at 17:57
  • It was originally tagged as `C` and `C#`. It should be reduced to a lowest common denominator of the two for clarity. The same concepts apply to both, and can help avoid the answers from being too general. – Cloud Oct 21 '13 at 18:05
  • @Dogbert The same concepts do not apply to both at all! You use pointers very frequently in C and you should be almost never using pointers in C#. The answers would be total opposites of each other. – Scott Chamberlain Oct 21 '13 at 18:07
  • The structure of the languages make pointers less relevant in C#, so I see it as a non-question in that scope. They are far more relevant to C, where they are used frequently. The OP did not request the specifics on a per-language basis, so I'm aiming for the most relevant one, and reducing people using multiple tags if it's not necessary. – Cloud Oct 21 '13 at 18:10

3 Answers3

2

They are a basic part of the C language. Without being rude, I think the key thing to consider is that they are not something to be avoided. They are another tool at your disposal, and a powerful one at that.

In general, they are used when you want to access large amounts of data (ie: arrays, massive structures, dynamic memory allocation, etc). They are also very useful when designing APIs (application programming interfaces). This shouldn't really be C# question, as in C# and C++, you can pass by value or by reference, and the nature of your question changes based on the usefulness of passing by value or reference, along with the user of other means of accessing data external to a function, class, etc.

Pass by Reference / Value in C++

If you can just pass by value, and the value is read-only, you're fine without a pointer, as you have no means to change the value. A common case is a simple sum function:

int sum (int a, int b) {
   int value = a + b;
   return value;
}

You have no need to modify the values provided, and you're only accessing simple values, rather than arrays or complex structures.

Now, if you're modifying the contents of a large array, especially if the size of the array can change at run time (ie: linked lists and other data structures) you need pointers, period. If you're modifying multiple values, you could either use pointers, or you could just use the return value of a function (ie: int mysum = sum(1,2);). About the only concern I encounter when using pointers in regular work is making sure any possible NULL pointer values are checked for at the beginning of each function. The real problem is when working with larger projects where you can get non-NULL, invalid/corrupt pointers from a poorly written third party API or library. That's where the real fun begins, debugging with JTAG debuggers, GDB, etc.

Community
  • 1
  • 1
Cloud
  • 18,753
  • 15
  • 79
  • 153
0

For , I think you will naturally end up using pointers for just about any but the most trivial of trivial programs. You will need a pointer when you want to refer to any non-basic type, e.g. structs, arrays (including strings).

Lots of other places too:

  • any time you do dynamic memory allocation (malloc() and friends)
  • if you need to return more than one "thing" from a function, you can pass a pointer to the storage for that object and have the function populate that memory
  • you might need to use function pointers
  • probably lots of other examples too.
Digital Trauma
  • 15,475
  • 3
  • 51
  • 83
0

Well, a couple of easy-bits would be, in case of dynamic memory allocation, you would want to use Pointers.

If you are not sure of the size of arrays that you are going to use, better implement a linked list.

Want multiple return values from a function, better pass the address of variables to be modified.

Any place, where you are not 100% sure, if you should be using a normal variable, use Pointer.

Kraken
  • 23,393
  • 37
  • 102
  • 162