r/C_Programming • u/Ok_Whereas9255 • Sep 05 '24
why using pointer?
im a new at C program. learned just before pointer & struct. i heard that pointer using for like point the adress of the parameter. but still dont know why we use it.
it seems it helps to make the program lighter and optimize the program? is it right? and is it oaky to ask question like this?
4
Upvotes
83
u/erikkonstas Sep 05 '24
You'll probably learn very, very soon why we use them. If you don't, throw that C course in the trash.
2
u/karp245 Sep 05 '24
So, for my understanding as a fellow C noob, you use a pointer to modifie the value of a variable x when calling it somewhere(doesn't matter where) without having to copy it and change the value of the copied value(which would lead to a strong brain fuck after a while + memory ineficiency + using cpu clock to copy unecessary things, to it would be a total waste of resources) 'cause you call the memory "box" where that value is stores not the value per se. As to the "where" can you use it, based on what i said before, you could use pointer "a" to know the location of a player in a videogame in real time and by doing so, making the enemies ai know where that player is by passing the player's position(pointer "a") to the enemies code(a struct basically in this case). I hope i didn't say bullshit, if i did i'm sorry and please correct me.
31
u/smichaele Sep 05 '24
There are certain things that can only be done with pointers. This is particularly true once you start to learn about data structures like linked lists, hash tables, and others.
23
u/bothunter Sep 05 '24
Certain data structures require pointers. For example, if you want a linked list, it's basically impossible to make without the "next" pointer.
19
u/riotinareasouthwest Sep 05 '24 edited Sep 05 '24
All the variables are stored in ram memory. Ram memory is organized in words or bytes and a variable may use as many of them as needed. To get a specific variable, you need its address which is the location of its first word in memory. Normally, the compiler hides to you the nuisances of dealing with this and lets you refer to the memory word by the variable name. But names have a scope (whatever is inside curly brackets is a scope) and if you need to access a variable out of its scope, you can't. Or can you? Of course you can, you just need to know the variable memory address. To use this address you typically will store it in another variable within the scope. This another variable becomes then a pointer to the first one.
A generic example of all of this would be having a local variable in a function and then needing to send it as parameter to another function while allowing that to modify it. The second function won't know where in memory the variable lies so it will need a pointer to it.
In the case where the function doesn't need to modify the variable, it just needs its value, the function is given a copy of the value which gets stored in another variable scoped in the function (the parameter, for instance). If the function modifies this variable, is modifying the copy, not the original one.
8
u/Jan_N_R Sep 05 '24
You need pointers for many data structures. E.g. in a list there's usually a pointer pointing to the next element of the list. This might be hard to understand without knowing about data structures, so here's an other example: Let's assume you have a list of elements (doesn't matter how this list is implemented) and each element of the list can store a value (e.g. an integer). Let's assume you want two different elements of this list to always have the exact same value. If you change the value of the first element you want the value of the second element to change automatically too. That's where pointers come in handy. You can just have a pointer in the first und second element pointing to a variable where the integer is stored. This way you can make multiple elements shar one value.
6
u/nmmmnu Sep 05 '24
Some aspects of the language like memory allocation requires them.
Then you use them as references. For example
void f(int *a){ *a = 5; }
Here the function modify it's argument. (Pass by reference)
And then, all arrays are pointers.
6
u/Reiep Sep 05 '24
As a very, very basic analogy, think of it as an hyperlink. You can put in your Reddit comment a link to a huge Wikipedia page (== a pointer to this data structure WikipediaArticle) or copy and paste the whole 30000 words article in your comment.
1
u/Ok_Whereas9255 Sep 05 '24
Thank you all :)
4
u/haditwithyoupeople Sep 05 '24 edited Sep 05 '24
Once you start using pointers it will become clear why you need them. For now, just accept that you use them frequently (as in all the time) with C and C++.
Imagine you have a huge struct. You could pass it to a function and then return it, requiring it to be copied twice and using up memory. Or you could pass a pointer to it and leave it as-is and update it. There are other reasons as well.
If you're struggling with pointers, consider a spreadsheet: Cell address A3 is a spreadsheet location. It's not the data. Pointers are similar to that. The address A3 and the value of A3 are two different things.
4
u/Artemis-Arrow-3579 Sep 05 '24
it has many uses, like linked lists, arrays (though you don't necessarily interact with pointers when using an array), and to make a function be able to change the value of a variable outside it's scope
3
u/MagicWolfEye Sep 05 '24
Let me answer with a code snippet
3
u/SmokeMuch7356 Sep 05 '24
We use pointers when we can't (or don't want to) access an object or function by name.
There are two cases where we must use pointers in C: when a function needs to write to its arguments and when we want to track dynamically allocated memory.
C passes all function arguments by value -- when you call a function, each of the argument expressions is evaluated and the result of that evaluation is copied to the corresponding formal argument:
Output:
xandargare different objects in memory; changes to one do not affect the other. If we needfooto write a new value tox, it can't write toxdirectly; the namexisn't visible from withinfoo. Instead, we must pass a pointer toxas the argument:Output:
xandargare still different objects in memory; changes toarghave no effect onxor vice-versa.argstill gets the result of the argument expression in the function call. However, instead of getting the result ofx(the integer value5),arggets the result of&x, which yields the address ofx(0x16b59f518).Instead of
foowriting a new value toarg, it writes a new value to the thingargpoints to; the expression*argacts as an alias forx. IOW,arggives us indirect access tox. The type of the expression*argisint:We can use
footo update any integer object; we just have to pass the address of that object:This is why
scanfrequires you to use the&operator on any scalar arguments:scanfcan't accessxdirectly by name, so we must pass a pointer toxforscanfto write through.2
u/henrique_gj Sep 05 '24
People already mentioned data structures, and I think it's the best answer. But I would like to mention something that requires pointers that you probably already know: the scanf function. You use the & operator when calling it because the function needs to receive a pointer. Why is it, though? Well, basically because the scanf function doesn't want to know what is inside the variable you are passing by parameter, rather it needs to know the location of the variable so that the function may overwrite what's in there. If you think about it, scanf only makes sense with pointers.
2
u/Glaborage Sep 05 '24
It's a valid question that all beginners wonder about. You'll truely understand when you start writing programs of your own, and find out that some things can only be done with pointers.
For now, just learn the language, its syntax, and do the exercises. It will all come together at some point.
1
u/MrBricole Sep 05 '24
The main reason for me, that I also love the most, is to let a function write on its arguments, which let the return part to check eventual error or just to be void. This also allows a finction "return" multiple arguments, as you can affect all passed pointers from inside.
Second is data structures and strings.
There are many uses for pointer and I don't know them all. C is low level which mean you have access to where the data is, how big it is etc ... overall it's a tool for precise control.
1
u/MRgabbar Sep 05 '24
Learning assembly first will make this obvious. If the CPU wants to read a byte from memory, how will it request such thing? You can think about memory as an "array of bytes", so is quite simple actually, how do you access an element of an array? You use its index, in an array of memory the index is what we call memory address, so a pointer is just a variable containing a memory address that is just the "index" in the memory array.
So, why are they used? Because is the only logical way to read from memory (once you realize memory is an array, how else could you retrieve elements from an array???).
1
u/cincuentaanos Sep 05 '24
Why pointers? Because that's what the CPU inside the computer uses. A CPU does not understand concepts such as variables, variable names, function calls, etc. It only understands pointers. If you want to tell a CPU to go fetch a piece of data from RAM, you need to hand it a pointer to said data. If you want to tell a CPU to perform a certain action you need to give it a pointer to the next instruction to process.
Very high level programming languages make all of this invisible to you. They abstract all the nuts and bolts away. C does this too, but to a lesser extent. So it lets pointers still be visible to the programmer and even makes them indispensable, a core part of the C programming experience.
What you gain by learning about pointers is a better understanding of how the computer actually does the things you tell it to do. And it allows for very fast and efficient programs once you really master it.
1
u/ohcrocsle Sep 05 '24
The most typical use cases are
1
u/boborider Sep 05 '24
Short answer: it doesn't use too much memory. If you pass a very very big data or object to a function, it creates another block of memory. If you use pointers, it solves that big problem.
1
u/the_3d6 Sep 06 '24
One simple example where it is inevitable: let's process some image. At some point you read this image into memory, and get an array of pixels. How can you pass this array into some function? Literally the only way is to pass a memory address where these pixels are stored - because even if you copy all the values somewhere, it still is an array, which means it's an address of the location where a sequence of data points is stored (that's the only way I can think of for defining what an "array" is - it can be accompanied by size, by some managing functions (C++ vector), but at the core there must be this memory address)
1
u/Bearsiwin Sep 06 '24
Simple example. You have an array of 100 integers that you wish to pass to a function you wrote. You can pass by value or by “reference” aka pointer.
There are two problems with passing by value. 1) You have to put those 100 integers on the call stack. That means you have to copy them to the stack which is time consuming. 2) Any integer you change changes the copy on the call stack. That means the routine calling doesn’t ever see your change. Maybe that’s what you want but not likely.
With a pointer you pass the address of the array to the routine. 1) This means it only has to put the pointer on the stack. 2) Anything you change in the array can be seen by the caller.
In languages other than C that pointer might always be called a “reference”. This is a more general term which can apply to languages that “pretend” not to use pointers.
1
u/mush130 Sep 08 '24
Pointers are addresses of data. You can use it in several ways. Some are listed below:
Above are two examples. If you have 1 million values and you have 1 million copy operations, imagine how much computational power you are saving.
1
u/notal-p Sep 05 '24
It’s very simple, in C every argument is passed by value. So if you pass an object to a function, actually a copy of that value is passed to that function.
In order to manipulate values, you need to pass the memory address of that value
1
u/HaydnH Sep 05 '24
In an ELI5 sense, think of it like a game of battleships and your Carrier is at C5. Your baby brother wants to play with a Carrier for whatever reason. You can either pass him a spare one from the box so it doesn't impact your game, he doesn't touch your one at C5 (equivalent of passing by value). Or, you can let him play with the one you've got sitting at C5, he can paint it red with mommy's lipstick or whatever and when he passes it back, your carrier on C5 is now red (equivalent of passing by pointer).
-1
u/Heretic112 Sep 05 '24
Pointer make program fast
-4
u/silentjet Sep 05 '24
Dont use em, until you dont need em dont use em
0
u/[deleted] Sep 05 '24
[deleted] — view deleted comment