Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 24

POINTER, STRUCTURE

AND UNION
Abdur Rawf Akand
ID: IT 23050
Md. Tajul Islam
ID: IT 23051

Page :1
WELCOME
To Our Presentation On Pointer ,Structure and Union

Page :2
POINTER IN C
Abdur Rawf Akand
ID: IT 23050

Page :3
ID:IT 23050
POINTER
• Pointer is a variable that stores/points the address of other variable.
• Pointer holds a memory address.
• This address is the location of another object in the memory.
• Pointer as an address indicates where to find an object.

Page :4
ID:IT 23050
• Pointer Syntax:

data_type *pointer_name;

• Example:
Int *p;
char *p;
float *p;

Page :5
ID:IT 23050
DECLARING POINTER VARIABLE

• Pointer variable contain address that belong to a separate data type , they must be
decleard as pointer before we use them.The declaration of a pointer variable
takes the following form

data_type *pointer_name;

Page :6
ID:IT 23050
INITIALIZATION OF POINTER VARIABLE

• Pointer initialization is the process of assigning address of a variable to pointer variable. Pointer variable
contains address of variable of same data type. In c address operator & is used to determined the address
of a variable. The & retyrn the address of the variable.
• Example:

int a=10;
int *ptr; //pointer declaration
ptr=&a; //pointer initialization
Or,
Int *ptr=&a; //initialization and declaration togather

Page :7
ID:IT 23050
EXAMPLE PROGRAM

#include<stdio.h>
int main()
{
Output
int *ptr; //declaration 10
int a=10;
ptr=&a; //initialization
printf(“%d”,*ptr); //displays a value using pointer variable
return 0;
}

Page :8
ID:IT 23050
TYPES OF POINTER

• 1. Integer Pointer
• 2. Array Pointer
• 3.Function Pointer
• 4.Double Pointer
• 5.Null Pointer
• 7.Void Pointer

Page :9
ID:IT 23050
A PROGRAM TO ADD TWO INTEGER USING POINTER

#include<stdio.h>
void sum( int * a , int * b)
{
int *sum = *a + *b ;
return *sum ;
}
int main()
{
int num1 , num2 , total ;
total = sum ( &num1 , &num2 ) ;
printf(“ Total = %d \n ”,total ) ;
return 0;
Page :10
} ID:IT 23050
A PROGRAM TO SWAP TWO INTEGER USING POINTER
#include<stdio.h>

void swap ( int *x , int *y )

int t = *x ;

*x = *y ;

*y = t ;

int main ()

int a = 10 , int b = 20 ;

swap( &a , &b ) ;

printf(“ %d %d \n”, a , b) ;

return 0 ;
Page :11
} ID:IT 23050
MD. TAJUL ISLAM
ID: IT-23051

Page:12
IT23051
Introduction
• Structure in C

► A collection of one or more variables, possibly of different types, grouped together under a single name for convenient handing.

Example:

struct card {

char *face;

char *suit;

};

- ► srtuct introduces the definition for structure card

- ► card is the structure name and is used to declare variables of the structure type

- ► card contains two members of type char *

These mamber are face and suit

► Compairing structure is a syntax error.

Page:13
IT23051
Definition of Structure
Structure is a user-defined data type in c which allows
you to combine different data types to store
a particular type of record.

It is somewhat similar to an array. The only difference is that


array is used to store collection of similar data types
While structure can store collection of
Any type of data.

Page:14
IT23051
Example of Structure

Page:15
IT23051
Declaring a struct type and struct variable

• Syntax:
struct struct_name
{ <type> field1-name;
<type> field2-name;

<type> fieldn-name;
};
- Dectaring a structure introduces a new type of variable into
your program, struct variables.

- Variables of this new type can be defined just like int, char, or
float variables are defined .

Page:16
IT23051
STRUCTURES DECLARATION &
INITIALIZATION
struct Student

char name[50];

int age;

float grade;

};

// Initialization

struct Student student1 = {"John Doe", 20, 85.5};

// Accessing Structure Members

printf("Name: %s\n", student1.name);

printf("Age: %d\n", student1.age); Page:17


printf("Grade: %f\n", student1.grade); IT23051
MD. TAJUL ISLAM
ID: IT-23051

Page:18
IT23051
DEFINATION OF UNION

• Union is a user-defined data type that allows storing different data types in the same memory
location.
• Unlike structures, unions share the same memory space for all members.

Page:19
IT23051
UNION DECLARATION

union Data
{
int intValue;
float floatValue;
char stringValue[20];
};

Page:20
IT23051
UNION EXAMPLE
union Data data;
data.intValue = 42;
printf(“int value: %d\n",
data.intValue);

data.floatValue = 3.14;
printf("Float value: %f\n",
data.floatValue);

strcpy(data.stringValue, "Hello, Union!");


printf("String value: %s\n",
data.stringValue); Page:21
IT23051
Differences between Structure and Union

Page:22
IT23051
DIFFERENCES BETWEEN STRUCTURE AND UNION

Page:23
IT23051
Thank You

Page:24
IT23051

You might also like