To understand the basic level of pointers we will be using C language. I'll be sharing some examples from HarvardX school of Computer Sciences & Stanford cs library.
The C programming language gives us the ability to directly manipulate the contents of memory addresses via pointers. Unfortunately, great power comes with great responsibility and this power is enough to screw things up spectacularly as simple errors can corrupt the state of your program in ways that make debugging difficult. The goal of these slides is to make sure that you understand exactly what you're doing when you use pointers in your code.
Here is an video example of Binky clay telling about pointers.
syntax: int* a;You have just declared an integer type pointer of variable a.
So next question raises in you mind must be how do we reference it? right..
You can do it like
syntax: a = malloc(sizeof(int));
Note: it gives a new place in the memory to the variable a
You can make a reference for already declared variable as a = &variable as well because there maybe a chance if you try to refer it and there is lack of space in the memory.
The C programming language gives us the ability to directly manipulate the contents of memory addresses via pointers. Unfortunately, great power comes with great responsibility and this power is enough to screw things up spectacularly as simple errors can corrupt the state of your program in ways that make debugging difficult. The goal of these slides is to make sure that you understand exactly what you're doing when you use pointers in your code.
Here is an video example of Binky clay telling about pointers.
FIRST OF ALL WHY DO WE USE POINTERS?
Yea one may ask what is the need of the pointers? Well when it comes to the concept of data structures using Linked List and Binary Search Trees. Pointers made it possible to implement them because without them there would be no Linked list or Binary Trees. Another reason of using pointers is we get access to our variables for sharing purpose quite easily, like without pointers we may have to work around to copy back and forth the data but because of pointers we don't need this.USAGE
Next we are thinking how do we declare a pointer? See the syntax belowsyntax: int* a;You have just declared an integer type pointer of variable a.
Referencing
After declaration the very first thing you need to do is referencing your pointer in order to use it, Unfortunately compiler or langues won't help you in that thats the task you have to perform by your hands.So next question raises in you mind must be how do we reference it? right..
You can do it like
syntax: a = malloc(sizeof(int));
Note: it gives a new place in the memory to the variable a
You can make a reference for already declared variable as a = &variable as well because there maybe a chance if you try to refer it and there is lack of space in the memory.
De-Referencing
You have successfully referenced your variable and you are ready to de-reference it. We can use the * operator to de-referencesyntax: a* = 40;Remember here you can de-reference only similar type otherwise it would be a run time error.
Code and data for your program are stored in random-access memory (RAM), which is basically a huge array of 1 byte (8 bits) blocks. Each block is associated with a hexadecimal number that represents its memory address.
Just as ints are variables that store integers and floats are variables that store floating-point values, a pointer is a variable that stores memory addresses. In the appliance (a 32-bit operating), a memory address is 4 bytes, so it makes sense that a pointer is also 4 bytes