2

I understand the premise of pointers, but I find it very annoying, and I don't get why it's considered useful;
I've learned about pointers, and the next thing I know, I start seeing bubbles, asterisks, and ampersands everywhere.

#include <stdio.h>
int main () {
   int *ptr, q;
   q = 50;
   ptr = &q;
   printf("%d", *ptr);
   return 0;
}

why is this important or useful?

Sapphire_Brick
  • 1,560
  • 12
  • 26
  • 1
    because CPUs itself work with memory addresses for loading/storing data, and C is low-level enough to have pointer as a primitive type – phuclv Sep 29 '19 at 02:08
  • that is the reason for the capability, not the reason for the importance. – Sapphire_Brick Sep 29 '19 at 02:55
  • 1
    Imagine trying to run a package delivery company or postal service and not being allowed to record street addresses. Pointers are the variables in C that hold addresses - and every time a CPU accesses memory to load or store data, it does so by address. – Andrew Henle Sep 29 '19 at 22:00
  • 1
    https://stackoverflow.com/questions/162941/why-use-pointers – M.M Dec 13 '19 at 04:08
  • https://stackoverflow.com/questions/5727/what-are-the-barriers-to-understanding-pointers-and-what-can-be-done-to-overcome – M.M Dec 13 '19 at 04:10
  • @M.M The first link was poorly formatted, and asked much more than "why use pointers?". The second link asked why pointers are so difficult for newbies to understand, and nothing to do with their usefulness. – Sapphire_Brick Mar 13 '20 at 01:27
  • @Sapphire_Brick re. the first one -- great , you have extra information that might be useful. I don't see any formatting issues but you can make edits to improve the formatting . Re. the second link, those topics are all inter-related – M.M Mar 13 '20 at 01:35
  • @M.M The first one was closed because of its poor formatting, The second one is a different question with similar answers. – Sapphire_Brick Mar 13 '20 at 01:37
  • @Sapphire_Brick in any case, people reading your question may find things of value in those other two questions – M.M Mar 13 '20 at 01:39
  • @M.M well then, – Sapphire_Brick Mar 13 '20 at 01:45

1 Answers1

7

First, parameters passed to a function can only be primitives(int, char, long....), structs or pointers. Then if you need to pass a more complex element like an array (strings) or a function, you have to pass a reference to this element.

The second things that I can quickly think of is: parameters are always passed by "value". This means the called function only get a copy of your variable. So, modifications will only affect the copy, the original variable will remain unchanged. If you pass a variable by "reference" with a pointer, the pointer itself is immutable but as it is a reference to the original var, any modification to the pointed element will also affect the var in the caller function. In other words, if you want to create a function that can alter a variable, you have to pass it a pointer to that variable to achieve this.

nobur
  • 144
  • 4
  • 2
    And pointers are needed if you need to access hardware registers at fixed addresses – Simson Sep 29 '19 at 01:14
  • 2
    You cannot pass an array by value, but you absolutely can pass a `struct` or even a `union` by value, even if it has an array member. (IOW, you can pass an array by value by wrapping it in a `struct`.) – rici Sep 29 '19 at 04:52
  • @rici: thanks to point me that. I made a wrong assumption since years. I'll correct my answer so. ( And some lines of my personnal code ) – nobur Sep 29 '19 at 20:40
  • 2
    as a trivial example, a linked list is a bunch different structs, in memory at 'random' locations in memory. Without pointers, cannot determine just where in memory each of those 'structs' are located. – user3629249 Sep 29 '19 at 21:32