pointers
pointers
3)Pointer to a characters:-
char ch = 'A';
char *char_ptr = &ch;
4)Pointer to a double:-
double value = 3.14;
double *ptr = &value;
5)Pointer to a string:-
char *str = "Hello, World!";
6)Pointers to Arrays:-
int arr[] = {1, 2, 3, 4, 5};
int *ptr = arr;
7)Pointers to Functions:-
int add(int a, int b)
{
return a + b;
}
int (*ptr)(int, int) = add;
#include <stdio.h>
int main()
{
int num = 42;
int *ptr = #
printf("Value through pointer: %d\n", *ptr);
return 0;
}
Syntax:- *pointerVariableName;
where
The indirection operator(*) that returns the value of the variable located at the address specified
by its operand. This is also called Dereferencing a pointer or Dereferencing operator.
Example:-
#include <stdio.h>
int main()
{
int num = 42;
int *ptr = #
printf("Value of num: %d\n", num);
printf("Value through pointer: %d\n", *ptr);
return 0;
}
#include <stdio.h>
int main()
{
int array[] = {1, 2, 3, 4, 5};
int i;
printf("Array elements: ");
for (i = 0; i < 5; ++i) {
printf("%d ", array[i]);
}
printf("\n");
return 0;
}
#include <stdio.h>
int main()
{
int array[] = {1, 2, 3, 4, 5};
int *ptr = array;
int i;
printf("Array elements using pointers: ");
for (i = 0; i < 5; ++i)
{
printf("%d ", *(ptr + i));
}
printf("\n");
return 0;
}
Pointer arrays:-
A pointer array in C is an array whose elements are pointers. Each element of the array holds the
memory address of a variable or an object of a specific type.
Example:-
#include <stdio.h>
int main()
{
int num1 = 10, num2 = 20, num3 = 30;
int *intArray[] = {&num1, &num2, &num3};
printf("Values through pointer array: %d, %d, %d\n", *intArray[0], *intArray[1],
*intArray[2]);
return 0;
}
int main()
{
int x = 5, y = 10;
printf("Before swapping: x = %d, y = %d\n", x, y);
swap(x, y);
printf("After swapping: x = %d, y = %d\n", x, y);
return 0;
}
Arguments that are passed through the command line are called Command line
arguments.
Command line arguments are handled by main( ) function.
Syntax:-
int main(int argc, char *argv[])
{
………………..
}
where
argc( Argument Count) denotes the number of arguments to be passed.
argv[](Argument vector) denotes to a pointer array that is pointing to every argument that has
been passed to the program.
Example:-
#include <stdio.h>
int main(int argc, char *argv[])
{
int i;
if (argc > 1)
{
printf("Program name: %s\n", argv[0]);
printf("Arguments provided:\n");
for (i = 1; i < argc; i++)
{
printf(" Argument [%d]is %s\n", i, argv[i]);
}
}
else
{
printf("No command line arguments provided.\n");
}
return 0;
}
They are used when we need to control our program from outside instead of hard-coding
it.
They make installation of programs easier.
A command-line argument allows us to provide an unlimited number of arguments.
The data is passed as strings as arguments, so we can easily convert it to numeric or other
formats.
Advantages of Pointer:-
Pointer saves memory.
Pointer increases the processing speed because the manipulation of data is done through
the address.
It helps in accessing the array in a very efficient manner.
Can access memory address very efficiently.
Since the pointer is used with data structures, it is useful for representing two-
dimensional and multi-dimensional arrays.
Pointer is very useful in dynamic memory allocation. With the help of pointer, we can
easily do dynamic memory allocation.
Disadvantages of Pointer:-
Pointers are a little complex to understand.
If an incorrect value is provided to the pointer, memory corruption can occur.
Pointers are slightly slower than normal variables.
There is a possibility of memory leakage.