Pointer is a variable which holds the address of the another variable. for ex:int *p; here p is a pointer which holds the address of inter variable. uses of pointers: 1.To access array elements 2.Pointers use in dynamic memory allocation linked linked lists,trees,graphs. 3.Passing data in & out of without coping . 4.Create and manage your own data types.
Pointer is a variable that contain address which is a location of another variable in the memory.It reduces length and complexity of the program and pointer can access the variable outside the functions it has high execution speed.
Pointers are variables that store the memory address of other variables.
They are declared the same way like any other variable but they are prefixed by a " * " which is called a dereference operator.
By using the "*" we stating that the actual variable is a pointer.
The pointer's data type must match the data type of the variable it points to.
Once declared the pointer variable can be assigned using the " & " operator , which means "the address of ".
By using pointers we can get the memory address of the actual variable where to the pointers points to , or we can get the actual value what is stored at the variable memory address.
#include <stdio.h>
int main()
{ int x=2;y=5;
int *xptr=&x; int *yptr=&y;
printf("Address of x: 0x%p ",xptr); printf("Address of y: 0x%p ",yptr);
return 0;
}
what you got?
The actual memory address of x and y variables!
by modifying the above example put the the" * " operators in the printf() functions before xptr and yptr we can get the values of x and y variables.
The following program illustrates a simple illustration of the use of a pointer:
#include <stdio.h> int main(void) { int count, *p; count = 22; p = &count; printf("count is %d
", count); printf("p is %ld
", p); printf("*p is %d
", *p); return 0; }
Here, we create an integer count, and declare an int pointer p, declared as int *p. The command p = &count uses the address operator to associate the pointer p to the variable count. The results of running this program is
count is 22 p is 1245048 *p is 22
(the second line may differ on another machine, or at a different time). The important point for now is that p is some (perhaps mysterious) integer, whereas *p (being 22, the same as count) is the value of the pointer.
In a simplified way, one can think of pointers in the following way. When one declares a variable, your program asks the system for memory to store it. This memory has two (distinct) properties - the value stored, and the location (address) of where the memory block is. Running the program on different machines, or at another time, will result in the same value being stored, but the address location of the memory block may change. In the above example, p = &count associates the pointer with the address in memory where count is stored, and *p is used to access the value of the contents of that memory.
void main() { float a= 0.7; if (a < 0.7) printf("c"); else printf("c++"); } Output of the above program is c. Why? Whereas the same program with 0.8 instead of 0.7 gives c++ as the output? Why explain?