Pointers are used everywhere in C, and if you have a good understanding of them C should not pose a problem. Pointers are an extremely powerful programming tool. They can make some things much easier, help improve your program’s efficiency, and even allow you to handle unlimited amounts of data.
C uses pointers in three main ways. First, they are used to create dynamic data structures: data structures built up from blocks of memory allocated from the heap at run-time. This is the only visible way that Pascal uses pointers. Second, C uses pointers to handle variable parameters passed to functions. And third, pointers in C provide an alternative means of accessing information stored in arrays, which is especially valuable when you work with strings. There is an intimate link between
arrays and pointers in C.
Pointer is a derived data type which creates special types of variables which can hold the address of primitive data type like char, int, float, double or user defined data type like structure, union, enum or derived data type like array, function, pointer etc. An integer pointer points to integer data, functional pointer points to a particular function depends
upon its prototype and so on…
All the operations performed on the pointers done through two unary operators
1) Address operator (&) returns the address of a particular variable.
2) Pointer operator (*) returns the data stored in a particular address.
To declare a pointer you have to put an * in front of its name.
In c programming every variable keeps two type of value.
- Contain of variable or value of variable.
- Address of variable where it has stored in the memory.
Simple pointer declaration and definition:
int * ptr;
About variable a:
- Name of variable : a
- Value of variable which it keeps: 5
- Address where it has stored in memory : 1025 (assume)
About variable ptr:
- 4. Name of variable : ptr
- 5. Value of variable which it keeps: 1025
- 6. Address where it has stored in memory : 5000 (assume)
Pointer is of two types:
1) Typed pointer: Typed pointer always points a particular data such as int, char, float…….
2) Untyped pointer: Untyped pointer is nothing but void pointer which can points to any king of
data but initially points to unknown.
int *ptr_A; /* A typed pointer */
void *ptr_B; /* A untyped pointer */
The following example describes that in how many ways we can access a variable using pointers without disturbing the corresponding variable.
printf(“%u\n%u\n%u\n%u\n%u\n%u”, i , *ptr, *(&ptr), ptr, &i, &ptr);
What is the size of Pointer variable in C?
All data is stored in memory. But different data types occupy different amount of memory. The sizeof() operator in C can be used to determine the number of bytes occupied by each data type. For example, on some machine you may have
sizeof(int) = 2
sizeof(float) = 4
sizeof(double) = 8
These numbers are Not the same for all machines. You should use the sizeof() operator instead of assuming the value.
- Size of Pointer can be evaluated by using sizeof operator
- Pointer stores the address of the Variable.
- Address of variable is nothing but the integer value.
- In C, for storing integer value 2 bytes are required.
- So Pointer variable requires 2 bytes of memory.
/* my output
CALL BY VALUE & CALL BY REFERENCE:
1) Call by Value
- While calling a Function, if we pass values as arguments to the called function known as call by value.
- So that the Arguments those are passed to that function just contains the values from the variables but not an Actual Address of the variable.
- So that generally when we call a Function then we will just pass the variables or the Arguments and we doesn’t Pass the Address of Variables ,
- So that whatever happened in the called function which never effects on the Values or on the variables. In the calling function
int x, y, temp;
printf(“Enter the value of x and y\n”);
scanf(“%d%d”, &x, &y);
printf(“Before Swapping\nx = %d\ny = %d\n”,x,y);
Void swap(int a,int b)
temp = a;
a = b;
b = temp;
printf(“After Swapping\nx = %d\ny = %d\n”,a,b);
2) Use of pointers in Call By Reference :-
- While calling a function if we pass address of actual parameters as arguments to the formal parameters in the calling function known as call by reference.
- In call by reference mechanism, if any changes happened on the formal parameters in the called function directly affects on the actual parameters in the calling function.
void swaping(int *x, int *y);
printf(“Enter first number (n1) : “);
printf(“Enter second number (n2) : “);
printf(“\nBefore swapping values:”);
printf(“\nAfter swapping values:”);
void swaping(int *x, int *y)
printf(“\nAfter swapping values:”);