Why pointer in c




















Instead of holding the value of a variable, they tell you its address. This is particularly useful when dealing with arrays, since using a pointer to the first element in an array its address you can quickly find the next element by incrementing the pointer to the next address location.

Pointers are similar to references. In other words, they're not copies, but rather a way to refer to the original value. Before anything else, one place where you will typically have to use pointers a lot is when you're dealing with embedded hardware. Maybe you need to toggle the state of a digital IO pin. Maybe you're processing an interrupt and need to store a value at a specific location.

You get the picture. However, if you're not dealing with hardware directly and are just wondering about which types to use, read on.

Why use pointers as opposed to normal variables? The answer becomes clearer when you're dealing with complex types, like classes, structures and arrays. That will work just fine and if you don't know exactly why you're using pointers, you shouldn't use them. Beware of the "they're probably faster" reason. Run your own tests and if they actually are faster, then use them.

However, let's say you're solving a problem where you need to allocate memory. When you allocate memory, you need to deallocate it. The memory allocation may or may not be successful. This is where pointers come in useful - they allow you to test for the existence of the object you've allocated and they allow you to access the object the memory was allocated for by de-referencing the pointer.

This is the key to why you would use pointers - references assume the element you're referencing exists already. A pointer does not. The other reason why you would use pointers or at least end up having to deal with them is because they're a data type that existed before references.

From a maintainability point of view, I should also mention that when you do use pointers, you either have to test for their validity and handle the case when they're not valid, or, just assume they are valid and accept the fact that your program will crash or worse WHEN that assumption is broken.

Put another way, your choice with pointers is to either introduce code complexity or more maintenance effort when something breaks and you're trying to track down a bug that belongs to a whole class of errors that pointers introduce, like memory corruption. So if you control all of your code, stay away from pointers and instead use references, keeping them const when you can.

This will force you to think about the life times of your objects and will end up keeping your code easier to understand. Just remember this difference: A reference is essentially a valid pointer. A pointer is not always valid. So am I saying that its impossible to create an invalid reference? It's just harder to do unintentionally and you will be amazed at how many bugs are unintentional :. Basically, the standard CPU architecture is a Von Neumann architecture, and it's tremendously useful to be able to refer to the location of a data item in memory, and do arithmetic with it, on such a machine.

If you know any variant of assembly language, you will quickly see how crucial this is at the low level. One use of pointers I won't mention things already covered in other people's posts is to access memory that you haven't allocated. This isn't useful much for PC programming, but it's used in embedded programming to access memory mapped hardware devices. Back in the old days of DOS, you used to be able to access the video card's video memory directly by declaring a pointer to:.

Since they're the address of an item, they're small: they take up only the space of an address. Since they're small, sending them to a function is cheap. And then they allow that function to work on the actual item rather than a copy. If you want to do dynamic storage allocation such as for a linked-list , you must use pointers, because they're the only way to grab memory from the heap.

Pointers are important in many data structures whose design requires the ability to link or chain one "node" to another efficiently. You would not "choose" a pointer over say a normal data type like float, they simply have different purposes. The address of the first element in your array can be assigned to a pointer. This then allows you to access the underlying allocated bytes directly. The whole point of an array is to avoid you needing to do this though. One way to use pointers over variables is to eliminate duplicate memory required.

For example, if you have some large complex object, you can use a pointer to point to that variable for each reference you make. With a variable, you need to duplicate the memory for each copy. Really, when you think about it, this makes sense. When you use subtype polymorphism, ultimately, you don't know ahead of time which class's or subclass's implementation of the method will be invoked because you don't know what the actual class is.

Note: if a class has 5 instance fields versus 3, more space will need to be allocated. The need for pointers in C language is described here. The basic idea is that many limitations in the language like using arrays, strings and modifying multiple variables in functions could be removed by manipulating with the memory location of the data.

To overcome these limitations, pointers were introduced in C. Further, it is also seen that using pointers, you can run your code faster and save memory in cases where you are passing big data types like a structure with many fields to a function. Making a copy of such data types before passing would take time and would consume memory. This is another reason why programmers prefer pointers for big data types. PS: Please refer the link provided for detailed explanation with sample code.

Here's my anwser, and I won't promse to be an expert, but I've found pointers to be great in one of my libraries I'm trying to write. The draw method takes these triangle objects, and well.. Find roots of a quadratic equation. Print Pyramids and Patterns. Check prime number. Print the Fibonacci series. Reference Materials string. Start Learning C. Explore C Examples. C Pass Addresses and Pointers. Access Array Elements Using Pointer. C Dynamic Memory Allocation. C Array and Pointer Examples.

C allows the choice. C instead, as you have now come to expect, bodges it up with pointers. If one has a pointer to the variable instead of the variable itself as the parameter then, even though that pointer gets copied, one can use its value, the memory address, to directly access the memory where the value of the original variable is stored. The use of pointers for passing alterable variables to subroutines has an another use - it can save memory and run faster because it does not have to duplicate the data.

For the above example, there would be no saving because an integer is as small in bytes as the pointer which is being copied instead. However, for more complicated variables in programs that must run very fast, it can be a vital trick. Structures are user-defined variable types which are used to keep a set of different variables associated with a single thing together. The use of pointers with structures is so common that there is an alternative syntax available to make such program lines look less messy.

It can get very, unnecessarily, confusing if these different uses of pointer are combined. For example, even if one is just passing an alterable array of strings to a subroutine, one has to work with a pointer to pointers to pointers to characters with the pointers being used in 3 different ways at once, none of them being the raw memory manipulation pointers were essentially designed for!

By Andrew Hardwick. Distributable under GPL freeware licences. But by convention, if a pointer contains the null zero value, it is assumed to point to nothing. Pointers have many but easy concepts and they are very important to C programming. Passing an argument by reference or by address enable the passed argument to be changed in the calling function by the called function. C allows a function to return a pointer to the local variable, static variable, and dynamically allocated memory as well.

C - Pointers Advertisements. Previous Page. Next Page. Live Demo.



0コメント

  • 1000 / 1000