+ 1
C pointers
Is pointers really necessary in C? for now I only know that it stores memory address of a variable and is only useful if the variable gets changed,but we could still avoid using pointers if we just assign another variable to the one getting changed(like temp)
3 Respuestas
+ 1
Yes pointers are one of the most important concept in C. As u know pointers stores the memory address of another variable & they allows us to manipulate memory directly.
Using temporary variable(temp) u can change the variable value but why pointers,means -> for "space complexity", it saves memory space & execution time is faster with pointers.
+ 1
Akshit Gupta pointers are necessary in C, even though they are also the source of many hard-to-find bugs.
C is used for making the fastest code possible. Pointers facilitate fast access to array elements, especially for arrays with 2 or more dimensions that would use multiplication to calculate the address of an element from its indices.
The only way to pass arrays and data structures into a function is by pointers.
They are needed to dynamically allocate and access memory.
They are needed in firmware and embedded programming to access hardware latches and control registers.
There are more uses of pointers. These are enough to demonstrate that they are needed.
If you can avoid using pointers then your code will be safer and easier to maintain. That is why Java was invented. It is fundamentally C without pointers, but that makes Java programs run slower. Pointers slice through run-time inefficiency, but they also cut into development efficiency and code maintenance, slowing you down - a two-edged sword.
0
Pointers are actually really useful, and it's not just for changing values.
Pointers can be used for dynamic memory allocation. free, malloc, calloc these functions use pointers to allocate/deallocate memory at runtime. Without them, you will only have statically allocated memory, which is inefficient for many tasks, especially those where memory isn't known at compile time.
Also, function arguments are passed by value, meaning a copy of the variable is passed. To modify the original variable within a function, you'd need to pass the address of the variable (using a pointer). This is one of the core advantages of pointer. Without it, you will need to use approaches like returning values or creating global variables, which isn't the best way, at least.
When you need to pass large structures/arrays to function, you can use pointers to pass the memory address. This is pretty efficient than copying the entire data, as it can be time-consuming and also uses significant memory.
And there are many more uses!