0% found this document useful (0 votes)
14 views

pointers

The document provides a comprehensive overview of pointers in C programming, including their declaration, initialization, and usage with variables, arrays, and functions. It also covers command line arguments, their properties, advantages, and disadvantages of using pointers. Examples are included to illustrate the concepts discussed.

Uploaded by

soumyamkdesai40
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

pointers

The document provides a comprehensive overview of pointers in C programming, including their declaration, initialization, and usage with variables, arrays, and functions. It also covers command line arguments, their properties, advantages, and disadvantages of using pointers. Examples are included to illustrate the concepts discussed.

Uploaded by

soumyamkdesai40
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Pointers

 Pointers:- A pointer is a variable which holds the address of another variable.


 Declaring pointers:-
Syntax:- <data_type> *<pointer_name>;
where
1) data_type is the pointer’s data type and indicates the type of the variable that the
pointer points to.
2) The asterisk(*) is indirection operator which is used to dereference a pointer.
 Examples:-
1) int *intPointer; //pointer to an integer
2) float *f; //pointer to a float
3) char *ch; //pointer to a character
4) double *d; //pointer to a double
 Initializing a Pointer:-
Syntax:- pointer = &variable;
Here, the address of variable is assigned to pointer.
 Examples:-
1) Pointer to an integer
int x = 10;
int *ptr;
ptr = &x;
OR
int x = 10;
int *ptr = &x;

2) Pointer to a float-point number:-


float pi = 3.14;
float *float_ptr;
float_ptr = &pi;

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;

Example to demonstrate the initialization of pointers:-

#include <stdio.h>
int main()
{
int num = 42;
int *ptr = &num;
printf("Value through pointer: %d\n", *ptr);
return 0;
}

Accessing a Variable through its Pointer:-

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 = &num;
printf("Value of num: %d\n", num);
printf("Value through pointer: %d\n", *ptr);
return 0;
}

Program to access the array elements without using a pointers.

#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;
}

Program to access the array elements using a pointers.

#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.

1. Declaring a Pointer Array:


int *intArray[3];

2. Initializing a Pointer Array:


int num1 = 10, num2 = 20, num3 = 30;
int *intArray[] = {&num1, &num2, &num3};

3. Accessing Values through the Pointer Array:


printf("Values through pointer array: %d, %d, %d\n", *intArray[0], *intArray[1], *intArray[2]);

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;
}

Using Pointers as Function arguments:-

 Using pointers as function arguments in C allows you to pass addresses of variables to


functions, enabling the function to modify the values directly.

 The parameters to the function are passed in two ways:-


1) By value 2) By reference

1)Program to illustrate call by value(Swapping of two numbers):-


#include <stdio.h>
void swap(int a, int b)
{
int temp;
temp= a;
a = b;
b = temp; Output:-
Before swapping: x=5, y=10
After swapping: x=5, y=10
}

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;
}

2)Program to illustrate call by reference(Swapping of two numbers):-


#include <stdio.h>
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
Output:-
int main()
Before swapping: x=5, y=10
{ After swapping: x=10, y=5
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;
}

Command line arguments:-

 Arguments that are passed through the command line are called Command line
arguments.
 Command line arguments are handled by main( ) function.

Components of Command line Arguments:-


In order to implement Command line arguments we use 2 parameters are passed into the main
function:-
 Number of Command line arguments.
 The list of Command line arguments.

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.

Properties of Command Line Arguments:


1. They are passed to the main() function.
2. They are parameters/arguments supplied to the program when it is invoked.
3. They are used to control programs from outside instead of hard coding those values
inside the code.
4. argv[argc] is a NULL pointer.
5. argv[0] holds the name of the program.
6. argv[1] points to the first command line argument and argv[argc-1] points to the last
argument.

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;
}

Advantages of Command line arguments:-

 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.

You might also like