0% found this document useful (0 votes)
9 views46 pages

C Programming - Module V

The document outlines the course ES 202 - Introduction to Computers and Programming in C, taught by Dr. Kanta Prasad Sharma at Amity School of Engineering & Technology. It covers various topics related to pointers, including their declaration, initialization, dereferencing, and types, as well as their relationship with arrays and structures. Additionally, it discusses function pointers and techniques for passing pointers to functions, emphasizing their importance in C programming.

Uploaded by

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

C Programming - Module V

The document outlines the course ES 202 - Introduction to Computers and Programming in C, taught by Dr. Kanta Prasad Sharma at Amity School of Engineering & Technology. It covers various topics related to pointers, including their declaration, initialization, dereferencing, and types, as well as their relationship with arrays and structures. Additionally, it discusses function pointers and techniques for passing pointers to functions, emphasizing their importance in C programming.

Uploaded by

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

ES 202 - Introduction to Computers and Programming in C

Dr. Kanta Prasad Sharma ( K P Sharma)


Associate Professor –CSE
Amity School of Engineering & Technology
Greater Noida Campus
+91 9760 207629
Faculty Profile
Greater Noida Campus Academic Highlights
 Ph.D. – Information Technology
 Industrial Training- UI Path Automation
 Industrial Training – Global Logic – Full Stack
 Books Published : 5
Experience
14 Years as academics &Innovative Research
Research Highlights

 SCI :18 [ WOS -H Index :08] & WCIF – 01.02

 Scopus : 42 [Scopus- H Index : 12]

 Citations : 454 [ H- 11 & I- 13 ]


Dr. Kanta Prasad Sharma
Associate Professor (CSE)  Research Gate : 9.9 [ H- 8]
Amity School of Engineering & Technology
Official : Faculty Cabin - 413 ( IV Floor,)
Amity University – G –Noida Campus  Patents :15 [ Published :18 ,Grant:10]
+91 9760 20 7629 Members
MODULE - V
Greater Noida Campus

Outlines
Pointers
relationship between arrays and pointers Argument passing using pointers
Array of pointers
Passing arrays as arguments
Strings and C string library.
Structure and Union.
Giving values to members,
Array of structure,
Nested structure, passing strings as arguments.
File Handling.
Pointers
Greater Noida Campus

• Pointers are one of the core components  [ programming

language ].

• A pointer - use [ to store ]  the memory address [ variables,

functions, or even other pointers] .

• A pointer is defined as a derived data type that can store the address of other

C variables or a memory location.


Syntax Pointers
Greater Noida Campus

( * ) dereferencing operator in the pointer declaration.

Datatype * ptr;

Ptr is the name pointer

Datatype is the type of data it is pointing


Syntax Pointers
Greater Noida Campus

int *ip;

double *dp;

float *fp;

char *ch
Greater Noida Campus
Use of Pointers

Pointer
Declaration

Pointer
Initialization

Pointer
Dereferencing
Greater Noida Campus
Pointer Declration

Int * ptr

• point to some random memory address as it is not initialized.

• Such pointers are called wild pointers


Greater Noida Campus
Pointer initialization
• Pointer initialization  [process]  assign some initial value  [to the pointer

variable].

• use the ( &: ampersand )  addressof operator  to get the memory address 

[ a variable]  [ then store]  [pointer variable].

Int x=10;

Int *ptr;

Prt=&x;

Int *prt=&x;
Greater Noida Campus
Pointer Dereferencing
• Dereferencing a pointer is the process of accessing the value  [stored in the
memory address specified in the pointer].
• Use the same ( * ) dereferencing operator .
Greater Noida Campus
Pointer Dereferencing
#include <stdio.h>
void main()
{
int x = 10;
int * ptr = & x;
printf("Value of x = %d\n", * ptr);
ptr = 20;
printf("Value of x = %d\n", * ptr);
}
Greater Noida Campus
Pointer Dereferencing
#include <stdio.h>
int main()
{
int x = 10; float y = 1.3f; char z = 'p';
int * ptr_x = & x;
float * ptr_y = & y;
char * ptr_z = & z;
printf("Value of x = %d\n", * ptr_x);
printf("Value of y = %f\n", * ptr_y);
printf("Value of z = %c\n", * ptr_z);
}
Greater Noida Campus
Pointer ( & , * operators)
#include <stdio.h>
int main()
{
int var = 20;
int *ip;
ip = &var;
printf("Address of var variable: %p\n", &var);
printf("Address stored in ip variable: %p\n", ip);
printf("Value of *ip variable: %d\n", *ip );
printf("Variable: %d \t Address: %p", var, &var);
}
Greater Noida Campus Pointer Types

Pointer
Integer Array Structure Function Double NULL Void Wild Constant
to
Pointers Pointer Pointer Pointers Pointers Pointer Pointer Pointers Pointers
Constant
Greater Noida Campus
Pointer Types

Integer Pointers
As the name suggests, these are the pointers that point to the integer values.

Syntax
int *ptr;

• These pointers are pronounced as Pointer to Integer.


• Similarly, a pointer can point to any primitive data type.
• It can point also point to derived data types such as arrays and user-defined data types
such as structures.
Greater Noida Campus
Pointer Types
Greater Noida Campus
Pointer Types
#include<stdio.h>
int main()
{
int a = 10;
int *ptr; ptr = &a;
printf("value of a = %d\n",a);
printf("value stored at ptr = %d\n",*ptr);
printf("Address of a = %x\n",&a);
printf("ptr points to the address = %x\n",ptr);
printf("Address of ptr = %x\n",&ptr);
return 0;
}
]

Greater Noida Campus


ARRAY POINTER

• Pointers and Array are closely related to each other.


• Even the array name is the pointer to its first element.
• They are also known as Pointer to Arrays.

Syntax
char *ptr = &array_name;
pointer_type *array_name [array_size];
]

Greater Noida Campus


ARRAY POINTER

• Pointers and Array are closely related to each other.


• Even the array name is the pointer to its first element.
• They are also known as Pointer to Arrays.

Syntax
char *ptr = &array_name;
pointer_type *array_name [array_size];
Greater Noida Campus
STRUCTURE POINTER

Structure Pointer
• The pointer pointing to the structure type is called Structure Pointer.
• It can be declared in the same way as declare the other primitive data types.
Syntax
struct struct_name *ptr;
Greater Noida Campus
STRUCTURE POINTER

#include <stdio.h>
int main()
{

int var1 = 10;


int var2 = 20;
int var3 = 30;
int* ptr_arr[3] = { &var1, &var2, &var3 };

for (int i = 0; i < 3; i++)


{
printf("Value of var%d: %d\tAddress: %p\n", i + 1, *ptr_arr[i], ptr_arr[i]);
}

}
Greater Noida Campus
STRUCTURE POINTER

char* arr[5] = { “ram", “gita", “mohan", “amity", “B.Tech" }


Greater Noida Campus
STRUCTURE POINTER
#include <stdio.h>
void main()
{
char str[3][10] = { “Ram", “Gita", “mohan" };
printf("String array Elements are:\n");
for (int i = 0; i < 3; i++)
{
printf("%s\n", str[i]);
}

for (int J = 0; J < 3; J++)


{
printf("Address of arr[%d]: %p\n", J, (void*)arr[J]);
}
}
Greater Noida Campus
STRUCTURE POINTER
#include <stdio.h>
void add(int a, int b)
{
printf("Sum : %d\n", a + b);
void main()
}
{
void subtract(int a, int b)
int x = 50, y = 5;
{
printf("Difference : %d\n", a - b);
void (*arr[4])(int, int) = { &add, &subtract, &mult,
} &divide };
void mult(int a, int b)
{ for (int i = 0; i < 4; i++)
printf("Product : %d\n", a * b); {
}
arr[i](x, y);
void divide(int a, int b)
{ }
printf("Quotient : %d", a / b); }
}
Greater Noida Campus
FUNCTION POINTER

• A pointer is  a variable  [stores the address]  of another variable.


• A variable  [that stores]  [address] a function is called function
pointer or pointer to a function.
• Function pointers can be useful
when we  want to [ call a function dynamically]
• The mechanism of callback functions [dependent on the function
pointers].
• Function Pointers  point to code  like normal pointers.
• In functions pointers the function's name  can be used to get
function's address.
• A function can -[ pass]  as an argument andcan be returned from
a function
Greater Noida Campus
Declration Function Pointer

Syntex
function_return_type(*Pointer_name)(function argument list)

Example
void morning()
{
printf(“Good Morning");
}

Declare a pointer to function

void (*ptr)() = &morning;


Greater Noida Campus
Example Function Pointer

#include <stdio.h>
void morning()
{
printf(“Good Morning");
}

void main()
{
void (*ptr)() = &hello;
(*ptr)(); // function calling using function pointer
}
Greater Noida Campus
Function Pointer with Argument

• A function pointer can also be declared for the function having arguments.
• During the declaration of the function,
need to provide the specific data types as the parameters list.

int addition (int a, int b)


{
return a + b;
}

Declare a function pointer


int (*ptr)(int, int) = addition;

call the function through its pointer


int z = (*ptr)(x, y);
Greater Noida Campus
Function Pointer with Argument
#include <stdio.h>
int addition (int a, int b)
{
return a + b;
}
void main()
{
int (*ptr)(int, int) = addition;
int x = 10, y = 20;
int z = (*ptr)(x, y);
printf("Addition of x: %d and y: %d = %d", x, y, z);
}
Greater Noida Campus
Pointer to Function with Argument

void swap(int *a, int *b)


{
int c;
c = *a;
*a = *b;
*b = c;
}

void (*ptr)(int *, int *) = swap;

(*ptr)(&x, &y);
Greater Noida Campus
Pointer to Function with Argument

#include <stdio.h>
void swap(int *a, int *b)
{
int c; c = *a; *a = *b; *b = c;
}
void main()
{
void (*ptr)(int *, int *) = swap;
int x = 10, y = 20;
printf("Values of x: %d and y: %d before swap\n", x, y);
(*ptr)(&x, &y);
printf("Values of x: %d and y: %d after swap", x, y);
}
Greater Noida Campus
Array of Function Pointer

Syntex

type (*ptr[])(args) = {fun1, fun2, ...};


Greater Noida Campus
Array of Function Pointer
int main()
#include <stdio.h>
{
float add(int a, int b) float (*ptr[])(int, int) = {add, subtract, multiply,
{ return a + b; } divide};

float subtract(int a, int b) int a = 15, b = 10;


int op=3;
{ return a - b; }
if (op > 5)
float multiply(int a, int b)
printf("Result: %.2f", (*ptr[op-1])(a, b));
{ return a * b; }
return 0;
float divide(int a, int b) }
{ return a / b; }
Greater Noida Campus
Double Pointer

• pointer to pointer which is also known as a double pointer


• in C is used  [to store  the address of another pointer].
• A "pointer to a pointer" is a form  multiple indirection or a chain of
pointers.
• Normally, a pointer [ contains ] the address of a variable.
• At time to define a "pointer to a pointer“ the first pointer contains
 [the address of the second pointer]
• which points to the location that contains the actual value.
Greater Noida Campus
Double Pointer

• The declaration of a pointer to pointer (double pointer) is similar to the declaration of


a pointer,
• the only difference is that you need to use an additional asterisk (*) before the
pointer variable name.

• When a target value is indirectly pointed to by a "pointer to a


pointer", accessing that value requires that the asterisk operator
be applied twice.
Greater Noida Campus
Double Pointer

int **var;
#include <stdio.h>
void main()
{
int a = 100;
int *ptr = &a;
int **dptr = &ptr;
printf("Value of 'a' is : %d\n", a);
printf("Value of 'a' using pointer (ptr) is : %d\n", *ptr);
printf("Value of 'a' using double pointer (dptr) is : %d\n", **dptr);
}
Greater Noida Campus
Pointer

• Assume that an integer variable "a" is located at an arbitrary


address 1000.
• Its pointer variable is "b" and the compiler allocates it the address
2000.
Greater Noida Campus
Double Pointer

• "c" is a pointer to a pointer to int, and should be declared as "int


**“
Syntex:

int **c = &b;


printf("b: %d \nPointer to 'b' is 'c': %d \nValue at b: %d\n", b, c, *c);
Greater Noida Campus
Double Pointer

#include <stdio.h>
int main()
{
int a = 10;
int *b = &a;
printf("a: %d \nAddress of 'a': %d \nValue at a: %d\n\n", a, b, *b);
int **c = &b;
printf("b: %d \nPointer to 'b' is c: %d \nValue at b: %d\n", b, c, *c);
printf("Value of 'a' from 'c': %d", **c);
return 0;
}
Greater Noida Campus
Multiple Pointer

• There is no limit to how many asterisks can appear in a pointer


declaration.
• If you do need to have a pointer to "c" (in the above example),
• it will be a "pointer to a pointer to a pointer" and may be declared
as

• int ***d = &c;


Greater Noida Campus
Passing Pointer to Function

• A pointer In C is a variable that stores [ the address of another variable].


• It acts as a reference to the original variable.
A pointer can be passed to a function, just like any other argument is passed.

Techniques
• Call by Value
• Call by Reference
Greater Noida Campus
Call by Value

Formal arguments −
• A function needs [certain data ]  to perform its desired process.
• When [ function is defined] means[assumed] [data values will be
provided].
• Data value[in the form of][parameter or argument list] [inside the
parenthesis].
• These arguments [ are the variables] certain data type.
Greater Noida Campus
Call by Value

Actual arguments
• When a certain function - is to be called,
• Function  provided the required number of values [same type]
• In the same sequence as used in its definition

Syntex
type function_name (type var1, type var2, ...)
Greater Noida Campus
Call by Value
#include <stdio.h>
int add(int x, int y)
int add(int x, int y)
{
{ int z = x + y;
int z = x + y; return z;
}
return z;
int main()
} {
int a = 10, b = 20;
int c = add(a, b); printf("Addition:
%d", c);
}
Greater Noida Campus
Call by Value
Greater Noida Campus
Call by Refrence

#include <stdio.h>
int add(int *, int *);
void main()
{
int a = 10, b = 20;
int c = add(&a, &b);
printf("Addition: %d", c);
}
int add(int *x, int *y)
{ int z = *x + *y;
return z;
}

You might also like