C Programming - Module V
C Programming - Module V
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
language ].
• A pointer is defined as a derived data type that can store the address of other
Datatype * ptr;
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
variable].
• use the ( &: ampersand ) addressof operator to get the memory address
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;
Syntax
char *ptr = &array_name;
pointer_type *array_name [array_size];
]
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()
{
}
Greater Noida Campus
STRUCTURE POINTER
Syntex
function_return_type(*Pointer_name)(function argument list)
Example
void morning()
{
printf(“Good Morning");
}
#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.
(*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
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
#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
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;
}