0% found this document useful (0 votes)
26 views113 pages

arrays,strings,structures

This document covers Unit II of a programming course focusing on arrays, strings, structures, and pointers in C. It details the definitions, syntax, and examples of one and two-dimensional arrays, string handling functions, structure definitions, and the concept of pointers including their use in self-referential structures. Additionally, it includes sample programs demonstrating operations such as calculating the minimum, maximum, sum, and average of array elements.
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)
26 views113 pages

arrays,strings,structures

This document covers Unit II of a programming course focusing on arrays, strings, structures, and pointers in C. It details the definitions, syntax, and examples of one and two-dimensional arrays, string handling functions, structure definitions, and the concept of pointers including their use in self-referential structures. Additionally, it includes sample programs demonstrating operations such as calculating the minimum, maximum, sum, and average of array elements.
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/ 113

Arrays

Strings

Structures

Pointers
PPS UNIT-II
Unit – II: Arrays, Strings, Structures and Pointers

Arrays: one and two dimensional arrays, creating, accessing and


manipulating elements of arrays

Strings: Introduction to strings, handling strings as array of


characters, basic string functions available
in C (strlen, strcat, strcpy, strstr etc.), arrays of strings

Structures: Defining structures, initializing structures, unions, Array of


structures

Pointers: Idea of pointers, Defining pointers, Pointers to Arrays and


Structures, Use of Pointers in selfreferential structures, usage of self
referential structures in linked list (no implementation)
Enumeration data type

PPS UNIT-II
Arrays

PPS UNIT-II
Data Types:

C Types

Void Basic Types Enumerated Derived

Floating
Integral
Point

PPS UNIT-II
PPS UNIT-II
Arrays:

Array is a collection of homogenous data items referred by a single name.

An array is defined as finite ordered collection of homogenous data, stored


in contiguous memory locations.

C provides two types of arrays:


Fixed-length array
Variable-length array

PPS UNIT-II
Arrays:

Syntax:
type arrayName[arraysize];

PPS UNIT-II
Declaring Arrays:

int a[10];
a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7] a[8] a[9]

float p[20];
p[0] p[1] p[2] p[3] p[4] p[5] p[6] p[7] p[8] p[9] p[10] p[11] p[12] p[13] p[14] p[15]p[16]p[17] p[18] p[19]

char z[5];
z[0] z[1] z[2] z[3] z[4]

int p[ ]; // Error

PPS UNIT-II
Accessing Array Elements:
#include <stdio.h>
25 32
int main( )
{ a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7] a[8] a[9]
int a[10];
float p[20];
p[0] p[1] p[2] p[3] p[4] p[5] p[6] p[7] p[8] p[9] p[10] p[11] p[12] p[13] p[14] p[15]p[16]p[17] p[18] p[19]
char z[5];
printf("\nSize is %d",sizeof(a));
a[0]=25;
a[1]=32; G D
p[1]=12.46; z[0] z[1] z[2] z[3] z[4]
p[15]=34.90;
z[2]='G';
Output:
z[4]='D';
printf("\nElement 0 in a: %d",a[0]); Size is 40
printf("\nElement 1 in a: %d",a[1]); Element 0 in a: 25
printf("\nElement 1 in p: %f",p[1]); Element 1 in a: 32
printf("\nElement 15 in p: %f",p[15]); Element 1 in p: 12.460000
printf("\nElement 2 in z: %c",z[2]); Element 15 in p: 34.900002
printf("\nElement 4 in z: %c",z[4]); Element 2 in z: G
} PPS UNIT-II Element 4 in z: D
Initializing Arrays:

76776 58326 84759 15832 -1758 5621 -4756 9856 2563 1452

int a[10];
a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7] a[8] a[9]

18 23 49 57 32
int b[5]={18,23,49,57,32}; b[0] b[1] b[2] b[3] b[4]

25 35 0 0 0
int c[5]={25,35}; c[0] c[1] c[2] c[3] c[4]

0 0 0 0 0
int d[5]={0}; d[0] d[1] d[2] d[3] d[4]

10 20 30 40
int e[ ]={10,20,30,40}; e[0] e[1] e[2] e[3]

PPS UNIT-II
Accessing Array Elements:
#include <stdio.h>
25 32
int main( )
{ a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7] a[8] a[9]
int a[10];
float p[20];
p[0] p[1] p[2] p[3] p[4] p[5] p[6] p[7] p[8] p[9] p[10] p[11] p[12] p[13] p[14] p[15]p[16]p[17] p[18] p[19]
char z[5];
printf("\nSize is %d",sizeof(a));
a[0]=25;
a[1]=32; G D
p[1]=12.46; z[0] z[1] z[2] z[3] z[4]
p[15]=34.90;
z[2]='G';
Output:
z[4]='D';
printf("\nElement 0 in a: %d",a[0]); Size is 40
printf("\nElement 1 in a: %d",a[1]); Element 0 in a: 25
printf("\nElement 1 in p: %f",p[1]); Element 1 in a: 32
printf("\nElement 15 in p: %f",p[15]); Element 1 in p: 12.460000
printf("\nElement 2 in z: %c",z[2]); Element 15 in p: 34.900002
printf("\nElement 4 in z: %c",z[4]); Element 2 in z: G
} PPS UNIT-II Element 4 in z: D
Accessing Array Elements:

#include <stdio.h>
int main( )
{
int a[5];
printf("\nEnter 5 elements:");
scanf("%d",&a[0]); Output:
scanf("%d",&a[1]); Enter 5 elements: 25 45 65 75 85
scanf("%d",&a[2]);
scanf("%d",&a[3]); The 5 elements are: 25 45 65 75 85
scanf("%d",&a[4]);

printf("\nThe 5 elements are:");


printf("\t%d",a[0]);
printf("\t%d",a[1]); 25 45 65 75 85
printf("\t%d",a[2]); a[0] a[1] a[2] a[3] a[4]
printf("\t%d",a[3]);
printf("\t%d",a[4]);
}
PPS UNIT-II
Accessing Array Elements:
#include <stdio.h> #include <stdio.h>
int main( ) int main( )
{ {
int a[5]; int a[5],i;
printf("\nEnter 5 elements:");
scanf("%d",&a[0]); printf("\nEnter 5 elements");
scanf("%d",&a[1]); for(i=0;i<5;i++)
scanf("%d",&a[2]); {
scanf("%d",&a[3]); scanf("%d",&a[i]);
scanf("%d",&a[4]); }

printf("\nThe 5 elements are:"); printf("\nThe 5 elements are:");


printf("\t%d",a[0]); for(i=0;i<5;i++)
printf("\t%d",a[1]); {
printf("\t%d",a[2]); printf("\t%d",a[i]);
printf("\t%d",a[3]); }
printf("\t%d",a[4]);
} }
PPS UNIT-II
Accessing Array Elements:
#include <stdio.h> Output:
int main( ) Enter 5 elements: 25 45 65 75 85
{
int a[5],i; The 5 elements are: 25 45 65 75 85

printf("\nEnter 5 elements");
for(i=0;i<5;i++)
25 45 65 75 85
{
scanf("%d",&a[i]); a[0] a[1] a[2] a[3] a[4]
}

printf("\nThe 5 elements are:");


for(i=0;i<5;i++)
{
printf("\t%d",a[i]);
}

}
PPS UNIT-II
Accessing Array Elements:
#include <stdio.h>
#include <stdio.h> int main( )
int main( ) {
{ int a[50],i,n;
int a[5],i; printf("\nEnter number of elements");
scanf("%d",&n);
printf("\nEnter 5 elements");
for(i=0;i<5;i++) printf("\nEnter %d elements",n);
{ for(i=0;i<n;i++)
scanf("%d",&a[i]); {
} scanf("%d",&a[i]);
}
printf("\nThe 5 elements are:");
for(i=0;i<5;i++) printf("\nThe elements are:");
{ for(i=0;i<n;i++)
printf("\t%d",a[i]); {
} printf(“ %d ",a[i]);
} }
}
PPS UNIT-II
Accessing Array Elements:
#include <stdio.h>
int main( ) a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7] a[8] a[9] a[10] a[11] a[12] a[13] a[14] a[15] a[16] a[17]

{
int a[50],i,n;
printf("\nEnter number of elements"); Output:
scanf("%d",&n); Enter number of elements 6
printf("\nEnter %d elements",n); Enter 6 elements
for(i=0;i<n;i++) 34
{ 56
scanf("%d",&a[i]); 78
} 81
25
printf("\nThe elements are:"); 69
for(i=0;i<n;i++)
{ The elements are: 34 56 78 81 25 69
printf(“ %d ",a[i]);
}
} PPS UNIT-II
Accessing Array Elements:
#include <stdio.h> #include <stdio.h>
int main( ) int main( )
{ {
int a[50],i,n; int a[50],i,n;
printf("\nEnter number of elements"); printf("\nEnter number of elements");
scanf("%d",&n); scanf("%d",&n);

printf("\nEnter %d elements",n); printf("\nEnter %d elements",n);


for(i=0;i<n;i++) for(i=0;i<n;i++)
{ {
scanf("%d",&a[i]); scanf("%d",&a[i]);
} }
printf("\nThe elements are:"); printf("\nThe elements are:");
for(i=0;i<n;i++) for(i=n-1;i>=0;i--)
{ {
printf(“ %d ",a[i]); printf(“ %d ",a[i]);
} }
} }
PPS UNIT-II
Accessing Array Elements:
#include <stdio.h>
int main( )
{
int a[50],i,n;
printf("\nEnter number of elements"); Output:
scanf("%d",&n); Enter number of elements 6
printf("\nEnter %d elements",n); Enter 6 elements
for(i=0;i<n;i++) 34
{ 56
scanf("%d",&a[i]); 78
} 81
25
printf("\nThe elements are:"); 69
for(i=n-1;i>=0;i--)
{ The elements are: 69 25 81 78 56 34
printf(“ %d ",a[i]);
}
} PPS UNIT-II
Program: Sum and Average
int main( )
{
Output:
int a[50],i,n,sum;
float avg; Enter number of elements
printf("\nEnter number of elements"); 4
scanf("%d",&n);
printf("\nEnter %d elements",n); Enter 4 elements
for(i=0;i<n;i++) 6
{ 7
scanf("%d",&a[i]); 9
} 4
sum=0;
for(i=0;i<n;i++) Sum:26
{ Average:6.500000
sum=sum+a[i];
}
printf("\nSum:%d",sum);
avg=(float)sum/n;
printf("\nAverage:%f",avg); PPS UNIT-II
}
Program: Minimum
#include <stdio.h>
int main( )
{
int a[50],i,n,min; Output:
printf("\nEnter number of elements"); Enter number of elements 5
scanf("%d",&n);
printf("\nEnter %d elements",n); Enter 5 elements
for(i=0;i<n;i++) 65
{ 45
scanf("%d",&a[i]); 75
} 35
min=a[0]; 85
for(i=1;i<n;i++)
{ Minimum : 35
if(a[i]<min)
min=a[i];
}
printf("\nMinimum : %d",min);
} PPS UNIT-II
Program: Minimum and Maximum
int main( )
{
int a[50],i,n,min,max;
printf("\nEnter number of elements"); Output:
scanf("%d",&n); Enter number of elements 5
printf("\nEnter %d elements",n);
for(i=0;i<n;i++) Enter 5 elements
scanf("%d",&a[i]); 65
min=a[0]; 45
max=a[0]; 75
35
for(i=1;i<n;i++)
85
{
if(a[i]<min) Minimum : 35
min=a[i]; Maximum : 85
if(a[i]>max)
max=a[i];
}
printf("\nMinimum : %d",min);
printf("\nMaximum : %d",max);
} PPS UNIT-II
Program: Minimum, Maximum, Sum and Average
int main( ) avg=(float)sum/n;
{ printf("\nMinimum : %d",min);
int a[50],i,n,min,max,sum; printf("\nMaximum : %d",max);
float avg; printf("\nSum : %d",sum);
printf("\nEnter number of elements"); printf("\nAverage : %f",avg);
scanf("%d",&n); }
printf("\nEnter %d elements",n); Output:
for(i=0;i<n;i++)
scanf("%d",&a[i]); Enter number of elements 4
min=a[0]; Enter 4 elements
max=a[0]; 6
sum=a[0]; 8
for(i=1;i<n;i++) 9
{ 7
sum=sum+a[i];
if(a[i]<min) Minimum : 6
min=a[i]; Maximum : 9
if(a[i]>max) Sum : 30
max=a[i]; Average : 7.500000
PPS UNIT-II
}
Two Dimensional Arrays:

An array of arrays is known as Two Dimensional Array.

data_type array_name[size1][size2]

PPS UNIT-II
Two Dimensional Arrays:

0 1 2 3
int a[5][6]; 4 5
0
1
2

3
4

0 1 2 3
0 4 5
1
int b[5][6];
2
3
4

PPS UNIT-II
Two Dimensional Arrays:

0 1 2 3
int a[5][6]; 45 4 5
0
1
2
a[0][2]=45;
3 98
a[3][4]=98; 4

0 1 2 3
0 4 5
1
int b[5][6];
2
3
4

PPS UNIT-II
Two Dimensional Arrays: Initialization

int d[2][4] = { {10, 80, 30, 60}, {70, 50, 40, 20} };
0 1 2
0 3
10 80 30 60
1 70 50 40 20
int g[2][3] = {58, 45, 74, 62, 96, 34};
0 1
0 2
58 45 74
1 62 96 34

int a[ ][3]={{2,3,4},{5,6,7}};

int b[ ][3]={2,3,4,5,6,7};

int c[2][ ]={{2,3,4},{5,6,7}}; //Error

int d[ ][ ]={{2,3,4},{5,6,7}}; //Error


PPS UNIT-II
Two Dimensional Arrays: Accessing
int main( )
{
int a[2][3],i,j;
a[0][0]=10;
a[0][1]=20;
Output:
a[0][2]=30;
a[1][0]=40;
Array Elements are:
a[1][1]=50;
a[1][2]=60;
10 20 30
printf("\nArray Elements are:\n"); 40 50 60
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
{
printf(" %d ",a[i][j]);
}
printf("\n");
}
} PPS UNIT-II
Two Dimensional Arrays: Accessing
#include <stdio.h>
int main( )
{
int a[2][3],i,j;

printf("\nEnter 6 Elements");
for(i=0;i<2;i++)
for(j=0;j<3;j++) Output:
scanf("%d",&a[i][j]);

printf("\nArray Elements are:\n"); Enter 6 Elements 2 4 6 8 10 12


for(i=0;i<2;i++)
{ Array Elements are:
for(j=0;j<3;j++) 2 4 6
{ 8 10 12
printf(" %d ",a[i][j]);
}
printf("\n");
}
PPS UNIT-II
}
Two Dimensional Arrays: Accessing
int main( ) Output:
{ Enter number of rows and columns:
int a[20][20],m,n,i,j; 3
4
printf("\nEnter number of rows and columns:"); Enter 12 Elements
1
scanf("%d%d",&m,&n);
2
printf("\nEnter %d Elements",m*n); 3
for(i=0;i<m;i++) 4
for(j=0;j<n;j++) 5
scanf("%d",&a[i][j]); 6
1
printf("\nArray Elements are:\n"); 2
for(i=0;i<m;i++) 3
{ 4
for(j=0;j<n;j++) 5
{ 6
printf(" %d ",a[i][j]); Array Elements are:
1 2 3 4
}
5 6 1 2
printf("\n");
3 4 5 6
}
PPS UNIT-II
}
PPS UNIT-II
Matrix Addition:

#include <stdio.h>
int main( )
{ for(i=0;i<m;i++)
int a[20][20],b[20][20],c[20][20],m,n,i,j; for(j=0;j<n;j++)
c[i][j]=a[i][j]+b[i][j];
printf("\nEnter number of rows and columns:");
scanf("%d%d",&m,&n); printf("\nMatrix C=A+B:\n");
for(i=0;i<m;i++)
printf("\nEnter %d Elements of Matrix A",m*n); {
for(i=0;i<m;i++) for(j=0;j<n;j++)
for(j=0;j<n;j++) {
scanf("%d",&a[i][j]); printf(" %d ",c[i][j]);
}
printf("\nEnter %d Elements of Matrix B",m*n); printf("\n");
for(i=0;i<m;i++) }
for(j=0;j<n;j++) }
scanf("%d",&b[i][j]);

PPS UNIT-II
Matrix Addition:

Output:

Enter number of rows and columns: 2 3

Enter 6 Elements of Matrix A


123456

Enter 6 Elements of Matrix B


10 20 30 40 50 60

Matrix C=A+B:
11 22 33
44 55 66

PPS UNIT-II
Matrix Transpose:
int main( )
{
int a[20][20],m,n,i,j; Output:

printf("\nEnter number of rows and columns:"); Enter number of rows and columns:
scanf("%d%d",&m,&n); 2
3
printf("\nEnter %d Elements of Matrix A",m*n);
for(i=0;i<m;i++) Enter 6 Elements of Matrix A
for(j=0;j<n;j++) 1
scanf("%d",&a[i][j]); 2
printf("\nMatrix Transpose:\n"); 3
for(j=0;j<n;j++) 4
{ 5
for(i=0;i<m;i++) 6
{
printf(" %d ",a[i][j]); Matrix Transpose:
} 1 4
printf("\n"); 2 5
} 3 6
}
PPS UNIT-II
Multi Dimensional Arrays:

int a[2][3][4]; 0 1
0 1 0 1
2 3 2 3
0
1
2

PPS UNIT-II
Multi Dimensional Arrays:
0 1
int a[4];
2 3

0 1
2 3
0
int b[3][4];
1
2

0 1
0 1 0 1
2 3 2 3
0
int c[2][3][4]; 1
2

PPS UNIT-II
Multi Dimensional Arrays:

int a[2][3][4]; 0 1
0 1 0 1
2 3 2 3 38
0
1
2 72

a[0][2][1]=72;

a[1][0][3]=38;

PPS UNIT-II
Strings

PPS UNIT-II
Strings:
In C language Strings are defined as an array of characters (ASCII
characters).

A string in C is a sequence of zero or more characters followed by a NULL


'\0' character.

String constants have double quote marks around them.

We can assign a string constant to a char array - either with no size


specified, or you can specify a size, but leave a space for the null
character.

PPS UNIT-II
Strings Initialization:

char c[ ]="abcd";

char c[5]="abcd";

char c[ ]={'a','b','c','d','\0'};

char c[5]={'a','b','c','d','\0'};

char d[20]="abcd";

a b c d \0

PPS UNIT-II
Strings Input / Output:

scanf The scanf( ) function can read the input till a


whitespace, newline or EOF
gets
The gets( ) functioncan read input till it a newline
printf or EOF

puts

char p[50];
char q[50];

scanf("%s",p);
gets(q);

printf("%s",p);
puts(q);

PPS UNIT-II
Strings Input / Output:

#include <stdio.h> #include <stdio.h>


int main( ) int main( )
{ {
char p[50]; char p[50];

printf("Enter a String:"); printf("Enter a String:");


scanf("%s",p); gets(p);
printf("The String is %s", printf("The String is %s", p);
p); }
}

Output: Output:

Enter a String:Welcome to India Enter a String: Welcome to India


The String is Welcome The String is Welcome to India

PPS UNIT-II
String Manipulation Functions:
string.h header file #include<string.h>

int strlen(char *string) - Determine the length of a string.

char *strcpy (char *dest, char *src) - Copy src string into dest string.

char *strncpy(char *dest, char *src, int n) - Copy first n characters.

int strcmp(char *string1, char *string2) - Compare string1 and string2.

int strncmp(char *string1, char *string2, int n) - Compare first n characters.

char *strcat(char *dest, const char *src); - Concatenate src to dest.

char *strncat(char *dest, const char *src, int n); - Concatenate n chars.

char *strrev(char *string) – Reverses a string


PPS UNIT-II
String Manipulation Functions:

char *strchr(char *string, int c) - First occurrence of character c in string.

char *strrchr(char *string, int c) - Last occurrence of character c in


string.

char *strstr(char *string2, char string*1) - Find first occurrence of string1


in string2.

char *strtok(char *s, const char *delim) - Parse the string s into tokens
using delim as delimiter.

PPS UNIT-II
String Conversion Functions:

Functions available in Stdlib.h are

double atof(const char *str) - Converts the string pointed to by the


argument str to a floating-point number.

int atoi(const char *str) - Converts the string pointed to by the argument
str to an integer .

long int atol(const char *str) - Converts the string pointed to by the
argument str to a long integer.

double strtod(const char *str, char **endptr) - Converts the string pointed
to by the argument str to a floating-point number (type double).

long int strtol(const char *str, char **endptr, int base) - Converts the string
pointed to by the argument str to a long integer (type long int).

PPS UNIT-II
String Manipulation Functions:
#include <stdio.h>
#include <string.h>
int main()
{
char p[50],q[50],r[50],s[50];
int m,n;
printf("Enter First String:");
gets(p);
printf("Enter Second String:");
gets(q);
m=strlen(p); Output:
n=strlen(q);
printf("\nLength of String1:%d",m); Enter First String:Welcome
printf("\nLength of String1:%d",n); Enter Second String:Hyderabad
strcpy(r,p);
strncpy(s,p,5); Length of String1:7
printf("\nString Copy:%s",r); Length of String1:9
printf("\nString Copy n:%s",s); String Copy:Welcome
strcat(p,q); String Copy n:Welco
strncat(r,q,5); String Concat:WelcomeHyderabad
printf("\nString Concat:%s",p); String Concat n:WelcomeHyder
printf("\nString Concat n:%s",r);
PPS UNIT-II
}
String Manipulation Functions: Comparison
#include <stdio.h> Output-1:
#include <string.h>
Enter First String:Welcome
int main()
Enter Second String:Welcome
{
char p[50],q[50]; String are equal
int c;

printf("Enter First String:"); Output-2:


gets(p);
printf("Enter Second String:"); Enter First String:Welcome
Enter Second String:Hyderabad
gets(q);
c=strcmp(p,q);
String2 is less
if(c==0)
printf("\nString are equal"); Output-3:
else if(c<0)
printf("\nString1 is less"); Enter First String:Hyderabad
else Enter Second String:Welcome
printf("\nString2 is less");
} String1 is less
PPS UNIT-II
String Manipulation Functions: Reverse

#include <stdio.h>
#include <string.h>
int main( ) Output:
{
Enter a String:Welcome
char p[50];
String after reverse:emocleW
printf("Enter a String:");
gets(p);

strrev(p);
printf("\nString after reverse:%s",p);

PPS UNIT-II
String Manipulation Functions:

#include <stdio.h>
#include <string.h>
int main( )
{
char p[50],q[50];
int c;

printf("Enter a String:");
gets(p);
strcpy(q,p);
strrev(q);
c=strcmp(p,q);
if(c==0)
printf("\nString is Palindrome");
else
printf("\nString is not Palindrome");
}

PPS UNIT-II
String Manipulations:

#include <stdio.h>
#include <string.h> Output:
int main( )
{ String is:Welcome
char p[50]="Welcome";
printf("\nString is:%s",p); String is:Wel

p[3]='\0';
printf("\nString is:%s",p);

}
W e l c o m e \0
0 1 2 3 4 5 6 7 8 9 10 11

W e l \0 o m e \0
0 1 2 3 4 5 6 7 8 9 10 11

PPS UNIT-II
String Manipulations:
#include <stdio.h>
#include <string.h>
int main( )
{
W e l c o m e \0
char p[50];
int n; 0 1 2 3 4 5 6 7 8 9 10 11

printf("Enter a String:");
gets(p); Output:

n=0; Enter a String: Welcome


while( p[n]!='\0' )
{ String length is:7
n++;
}

printf("\nString length is:%d",n);


}
PPS UNIT-II
String Manipulations:
#include <stdio.h>
#include <string.h> a H y d e r a b a d \0
int main( ) 0 1 2 3 4 5 6 7 8 9 10 11
{
char a[50],b[50]; b
int i; 0 1 2 3 4 5 6 7 8 9 10 11

printf("Enter a String:");
gets(a);
b H y d e r a b a d \0
i=0; 0 1 2 3 4 5 6 7 8 9 10 11
while( a[i]!='\0' )
{ Output:
b[i]=a[i];
i++; Enter a String: Hyderabad
}
b[i]='\0'; String b is: Hyderabad

printf("\nString b is:%s",b);
} PPS UNIT-II
String Manipulations:
int main( )
{ a W e l c o m e \0
char a[50],b[50];
0 1 2 3 4 5 6 7 8 9 10 11
int i,j;
printf("Enter a String:");b H y d \0
gets(a);
0 1 2 3 4 5 6 7 8 9 10 11
printf("Enter b String:");
gets(b);
a W e l c o m e H y d \0
i=0;
while( a[i]!='\0' ) 0 1 2 3 4 5 6 7 8 9 10 11
{
i++;
}
j=0; Output:
while(b[ j ]!='\0')
{
Enter a String: Welcome
a[i]=b[ j ];
i++;
j++; Enter b String: Hyd
}
a[i]='\0'; String a is: WelcomeHyd
printf("\nString a is:%s",a);
PPS UNIT-II
}
String Manipulations:
int main( )
{
char a[50],b[50];
int i,j; a V e h i c l e \0
printf("Enter a String:"); 0 1 2 3 4 5 6 7 8 9 10 11
gets(a);
i=0; b e l c i h e V \0
while( a[i]!='\0' ) 0 1 2 3 4 5 6 7 8 9 10 11
{
i++;
}
i--; Output:
j=0;
while(i>=0) Enter a String:Vehicle
{
b[ j ]=a[i];
i--; String b is:elciheV
j++;
}
b[ j ]='\0';
printf("\nString b is:%s",b);
} PPS UNIT-II
Structures

PPS UNIT-II
Structures:

•Structure type declaration


•Variable declaration
•Initialization
•Accessing Structures
•Pointer to structures
•Nested structures
•Structures with arrays
•Structures with pointers
•Array of structures
•Structures and functions
•Self referential structures

PPS UNIT-II
Structures:

A structure is a collection of related elements, possibly of different types,


having a single name.
Each element in a structure is called a field.
A field differs from a variable primarily in that it is part of a structure.
Syntax:

struct name typedef struct


{ {
field list field list
}; }name;

PPS UNIT-II
Structures:
#include<stdio.h>
struct Sample x y z
{ s1 85 7.2 J
int x;
float y;
char z;
}; x y z
s2
int main( )
{
struct Sample s1,s2;

s1.x=85;
s1.y=7.2;
s1.z='J';

printf("\n %d",s1.x);
printf("\n %f",s1.y);
printf("\n %c",s1.z);
PPS UNIT-II
}
Structures: Declaring Structures
struct name typedef struct
{ {
field list field list
}; }name;

struct Sample typedef struct


{ {
int x; int x;
float y; float y;
char z; char z;
}; } Sample;

PPS UNIT-II
Structures: Declaring Structure variables
#include<stdio.h>
struct Sample
{
int x; #include<stdio.h>
float y; struct Sample
char z; { #include<stdio.h>
}; int x; typedef struct
float y; {
int main( ) char z; int x;
{ }s1,s2; float y;
struct Sample s1; void main() char z;
} { }Sample;
struct Sample s3;
} int main( )
{
Sample s1;
}

PPS UNIT-II
Structures: Initializing

#include<stdio.h>
struct Sample
{ x y z
int x; 90 80.5 P
s1
float y;
char z;
};
void main( )
{
struct Sample s1={90,80.5,'P'};
printf("\n %d",s1.x);
printf("\n %f",s1.y);
printf("\n %c",s1.z);
}

PPS UNIT-II
Structures: Accessing the fields
#include<stdio.h>
struct Sample
{
int x;
float y;
char z;
};
x y z
int main( ) s1 85 7.2 J
{
struct Sample s1;
s1.x=85;
s1.y=7.2;
s1.z='J';
printf("\n %d",s1.x);
printf("\n %f",s1.y);
printf("\n %c",s1.z);
}
PPS UNIT-II
Structures: Accessing the fields
#include<stdio.h>
struct Sample
{ x y z
int x;
s1 38 8.6 G
float y;
char z;
};
void main( )
{
struct Sample s1;
printf("\nEnter structure z value:");
scanf("%c",&s1.z);
printf("\nEnter structure x value:");
scanf("%d",&s1.x);
printf("\nEnter structure y value:");
scanf("%f",&s1.y);
printf("\n %d",s1.x);
printf("\n %f",s1.y);
printf("\n %c",s1.z);
} PPS UNIT-II
Structures: Accessing the fields
struct Sample
{
int x;
float y; x y z
char z; s1 60 70.5 M
}s1,s2;
int main( )
{
s1.x=60; x y z
s1.y=70.5; 25 31.6 D
s2
s1.z='M';
s2.x=25;
s2.y=31.6;
s2.z='D';
printf("\n %d",s1.x);
printf("\n %f",s1.y);
printf("\n %c",s1.z);
printf("\n %d",s2.x);
printf("\n %f",s2.y);
printf("\n %c",s2.z); PPS UNIT-II
}
x y z
Structures: s1 60 70.5 M
#include<stdio.h>
struct Sample x y z
{ s2 25 31.6 D
int x;
float y; x y z
char z; s2 60 70.5 M
};
x y z
int main( )
{ s2 50 70.5 M
struct Sample s1={60,70.5,'m'};
struct Sample s2={25,31.6,'d'}; Output:
printf("\ns1:%d %f %c",s1.x,s1.y,s1.z);
printf("\ns2:%d %f %c",s2.x,s2.y,s2.z); s1:60 70.500000 m
s2=s1; s2:25 31.600000 d
printf("\ns1:%d %f %c",s1.x,s1.y,s1.z); s1:60 70.500000 m
printf("\ns2:%d %f %c",s2.x,s2.y,s2.z); s2:60 70.500000 m
s2.x=50; s1:60 70.500000 m
printf("\ns1:%d %f %c",s1.x,s1.y,s1.z); s2:50 70.500000 m
printf("\ns2:%d %f %c",s2.x,s2.y,s2.z);
}

PPS UNIT-II
Structures: Nested Structures
struct ECE
{
x y e1 z
int a;
int b; 25 8.9 56 33 12.8 R
float c;
};
struct Sample
{ x y e1 z
int x; 25 6.3 75 91 12.8 R
float y;
struct ECE e1;
char z;
};
int main( )
{
struct Sample s1={25,8.9,{56,33,12.8},'R’};
s1.y=6.3;
s1.e1.a=75;
s1.e1.b=91;
printf("\n %d",s1.x);
printf("\n %d",s1.e1.b);
} PPS UNIT-II
Structures: Arrays in Structures

#include<stdio.h> x y z
struct Sample
{ s1 25 30 35 40 45 70.5 M
int x[5];
float y; x y z
char z;
}; s1 25 30 66 40 82 8.9 M
int main()
{
struct Sample s1={{25,30,35,40,45},12.8,'R'};
s1.y=8.9;
s1.x[2]=66;
s1.x[4]=82;
printf("\n %d",s1.x[0]);
printf("\n %d",s1.x[2]);
printf("\n %f",s1.y);
printf("\n %c",s1.z);
} PPS UNIT-II
Structures:
struct ECE
{
int a;
int b;
float c;
};
struct Sample int main( )
{ {
int x[5]; struct Sample s1={{25,30,35,40,45},8.9,
float y; {56,33,12.8},'R'};
struct ECE e1; printf("\n %d",s1.x[0]);
char z; printf("\n %d",s1.x[4]);
}; printf("\n %f",s1.y);
printf("\n %d",s1.e1.a);
printf("\n %d",s1.e1.b);
printf("\n %f",s1.e1.c);
printf("\n %c",s1.z);
}
Structures: Array of Structures
struct Student Enter Roll number, Name and Marks :1 John 67
{ Enter Roll number, Name and Marks :2 Mike 82
int roll; Enter Roll number, Name and Marks :3 Robert 98
char name[20]; Enter Roll number, Name and Marks :4 Rossy 75
int marks; Enter Roll number, Name and Marks :5 Peter 69
}; Student Information:

int main( ) Roll number Name Marks


{ 1 John 67
int i; 2 Mike 82
struct Student s[10]; 3 Robert 98
for(i = 0; i < 5; i++) 4 Rossy 75
{ 5 Peter 69
printf("Enter Roll number, Name and Marks :");
scanf("%d%s%d",&s[i].roll,s[i].name,&s[i].marks);
}
printf("Student Information:\n\n");

printf("\nRoll number\tName\tMarks\n");
for (i = 0; i < 5; ++i)
printf("\n%d\t%s\t%d",s[i].roll,s[i].name,s[i].marks);
} PPS UNIT-II
Structures: Memory Allocation

#include<stdio.h> #include<stdio.h>
struct Sample struct Sample
{ {
int x; int x;
float y; double y;
char z; char z;
}; };
void main() void main()
{ {
struct Sample s1; struct Sample s2;
printf("%d",sizeof(s1)); printf("%d",sizeof(s2));
} }

Output: Output:

12 24
PPS UNIT-II
Bit Field:
C allow integer members to be stored into memory spaces smaller than
the compiler would ordinarily allow.

These space-saving structure members are called bit fields, and their
width in bits can be explicitly declared.

The declaration of a bit-field has the form inside a structure:

struct structurename
{
type member_name : width ;
};

type The type may be int, signed int, unsigned int.

member_name The name of the bit-field.


The number of bits in the bit-field. The width must be less
width
than or equal to the bit width of the specified type.

PPS UNIT-II
Bit Field:
struct Sample
{
unsigned int a;
unsigned int b;
}; Output:
struct Demo
{ Memory size occupied by s1 : 8
unsigned int x : 5;
unsigned int y : 5; Memory size occupied by d1 : 4
};
void main( )
{
struct Sample s1;
struct Demo d1;
printf( "Memory size occupied by s1 : %d\n", sizeof(s1));
printf( "Memory size occupied by d1 : %d\n", sizeof(d1));
}

PPS UNIT-II
Unions:
union FIRST
{
A union, is a collection of variables int x;
of different types, just like a char y;
structure. float z;
};
However, with unions, we can only void main( )
store information in one field at any {
one time. union FIRST f1;
f1.x=25;
f1.y='S';
f1.z=798.34;
printf("\n %d",f1.x);
printf("\n %c",f1.y);
printf("\n %f",f1.z);
printf("\n %d",sizeof(f1));
}

PPS UNIT-II
Unions: union First
struct First {
{ int x;
int x; char y;
char y; float z;
float z; };
}; int main( )
int main( ) {
{ union First f1;
struct First f1; f1.x=25;
f1.x=25; f1.y='S';
f1.y='S'; f1.z=798.34;
f1.z=798.34; printf("\n %d",f1.x);
printf("\n %d",f1.x); printf("\n %c",f1.y);
printf("\n %c",f1.y); printf("\n %f",f1.z);
printf("\n %f",f1.z); printf("\n %d",sizeof(f1));
printf("\n %d",sizeof(f1)); }
}
Output:
Output: 1145542083
25 ├
S 798.340027
798.340027 4
PPS UNIT-II
12
Enumerated Types:

The enumerated type is a user-defined type based on the standard integer


type.
In an enumerated type each integer value is given an identifier called an
enumeration constant.

enum typeName {identifier list}

PPS UNIT-II
Pointers

PPS UNIT-II
Pointers:
A pointer is a special kind of variable.

A pointer is a constant or variable that contains an address that can be


used to access data.

Pointers are designed for storing memory address i.e. the address of
another variable.

Declaring a pointer is the same as declaring a normal variable except


you stick an asterisk '*' in front of the variable’s identifier.

There are two new operators you will need to know to work with pointers.
The address of operator '&' and the "dereferencing" operator '*'.
Both are unary operators.

A pointer that points to no variable contains the special null-pointer


constant, NULL.

PPS UNIT-II
Declaring Pointers:

PPS UNIT-II
Declaring Pointers:

56248
int a=25; a 25

int *p; 63964


p NULL

p = &a;

PPS UNIT-II
Pointers First Program:
#include<stdio.h>
061FE14
int main( )
{ a 25
int a=25;
int *p;
061FE18
p=&a; p NULL

printf("\n a = %d",a);
printf("\n*p = %d",*p);

printf("\n address of a = %p",&a); Output:


printf("\n address of a = %p",p);
a = 25
}
*p = 25
address of a = 000000000061FE14
address of a = 000000000061FE14

PPS UNIT-II
Pointers:
56248
int main( )
{ a 10
20
30
int a=10;
int *p;
63964
p=&a;
p NULL
printf("\na = %d",a);
printf("\n*p = %d",*p);
Output:
a=20;
printf("\na = %d",a);
printf("\n*p = %d",*p); a = 10
*p = 10
*p=30; a = 20
printf("\na = %d",a); *p = 20
printf("\n*p = %d",*p); a = 30
*p = 30
printf("\n address of a = %p",&a); address of a = 0060FEF8
printf("\n address of a = %p",p); address of a = 0060FEF8
} PPS UNIT-II
Pointers:
int main( ) a Z n 15 x 3.3
{
char a='z';
int n=15;
float x=3.3; p q r
char *p;
int *q;
float *r;

p=&a; Output:
q=&n;
r=&x; Size of a is 1
Size of n is 4
printf("\nSize of a is %d",sizeof(a)); Size of x is 4
printf("\nSize of n is %d",sizeof(n)); Size of p is 4
printf("\nSize of x is %d",sizeof(x)); Size of q is 4
printf("\nSize of p is %d",sizeof(p)); Size of r is 4
printf("\nSize of q is %d",sizeof(q));
printf("\nSize of r is %d",sizeof(r));
}
PPS UNIT-II
Pointers:
int main( )
{
char a='z';
int n=15;
float x=3.3;
char *p;
int *q;
float *r;

p=&a;
q=&n; Error
r=&x;

p=&n;

printf("\nSize of a is %d",sizeof(a));
printf("\nSize of n is %d",sizeof(n));
printf("\nSize of x is %d",sizeof(x));
printf("\nSize of p is %d",sizeof(p));
printf("\nSize of q is %d",sizeof(q));
printf("\nSize of r is %d",sizeof(r));
} PPS UNIT-II
Pointers:

#include<stdio.h>
int main( )
{
int a=10,b=20,c=30;
int *p;

p=&a;
printf("\n %d",*p);

p=&b;
printf("\n %d",*p); Output:

p=&c; 10
printf("\n %d",*p); 20
} 30

PPS UNIT-II
Pointers:
#include<stdio.h> a 10 b 20 c 30
int main( )
{
int a,b,c;
int *p; p q r
int *q;
int *r;

p=&a; Output:
q=&b;
r=&c; Enter two values:10 20

printf("\nEnter two values:"); *p=10 *q=20 *r=30


scanf("%d%d",&a,&b); a=10 b=20 c=30

c=a+b;

printf("\n*p=%d *q=%d *r=%d",*p,*q,*r);


printf("\na=%d b=%d c=%d",a,b,c);
}

PPS UNIT-II
Pointers:
#include<stdio.h> #include<stdio.h>
int main( ) int main( )
{ {
int a,b,c; int a,b,c;
int *p; int *p;
int *q; int *q;
int *r; int *r;

p=&a; p=&a;
q=&b; q=&b;
r=&c; r=&c;

printf("\nEnter two values:"); printf("\nEnter two values:");


scanf("%d%d",&a,&b); scanf("%d%d",p,q);

c=a+b; *r=*p+*q;

printf("\n*p=%d *q=%d *r=%d",*p,*q,*r); printf("\n*p=%d *q=%d *r=%d",*p,*q,*r);


printf("\na=%d b=%d c=%d",a,b,c); printf("\na=%d b=%d c=%d",a,b,c);
} }

PPS UNIT-II
Pointers:
a 10
int main( )
{
int a=10;
int *p;
int **q;
p
p=&a;
q=&p;
q
printf("\n %d",a);
printf("\n %d",*p);
printf("\n %d",**q);

printf("\n %p",&a);
printf("\n %p",p);
printf("\n %p",*q);

PPS UNIT-II
Arrays and Pointers:
The name of an array is a pointer constant to the first element.
Consider an array a[10].
Then the array name ‘a’ contains the address of the first element ‘a[0]’.
That is ‘a’ contains ‘&a[0]’.
When an array name is dereferenced, it refers only to the first element, not
the whole array.
int main( )
{
int a[5]={11,22,33,44,55};
Output:

printf("\n %p %p", a, &a[0]); 0060FEEC 0060FEEC


11 11
printf("\n %d %d", *a, a[0]); 22 22

printf("\n %d %d", *(a+1),a[1]);


}
PPS UNIT-II
Pointers:

100

100

100

100

101

101

101

101

102

102
C

C
0

4
a 11 22 33 44 55 66 77 88 99 110

0 1 2 3 4 5 6 7 8 9
#include<stdio.h>
int main( )
{
int a[10]={11,22,33,44,55,66,77,88,99,110}; Output:

printf("\n %d %d", *a,a[0]); 11 11


printf("\n %d", *(a+0)); 11
printf("\n %d", *(a+1)); 22
printf("\n %d", *(a+2)); 33
printf("\n %d", *(a+3)); 44
printf("\n %d", *(a+4)); 55
printf("\n %d", *(a+5)); 66
printf("\n %d", *(a+6)); 77
printf("\n %d", *(a+7)); 88
printf("\n %d", *(a+8)); 99
printf("\n %d", *(a+9)); 110
}
PPS UNIT-II
Arrays and Pointers:

As the name of an array is a pointer constant, we can store it in another


pointer variable and use it the same way as the name of the array.
int *p;
p=a;
a 1000
a[0] *(a+0) *(p+0) p[0] 11
0 11 1000
p 1000
22 1004
a[1] *(a+1) *(p+1) p[1] 22
1
2 33 1008 a[2] *(a+2) *(p+2) p[2] 33

3 44 100C
a[3] *(a+3) *(p+3) p[3] 44
4 55 1010
a[4] *(a+4) *(p+4) p[4] 55

PPS UNIT-II
Arrays and Pointers:

100

100

100

100

101

101

101

101

102

102
C

C
0

4
a 11 22 33 44 55 66 77 88 99 110
int main( ) 0 1 2 3 4 5 6 7 8 9
{
int a[10]={11,22,33,44,55,66,77,88,99,110};
int *p;
printf("\n %d %d", *a, a[0]);
p=&a[0]; Output:
printf("\n %d", *p); p 1000
printf("\n %d", *(p+0)); 11 11
printf("\n %d", *(p+1)); 11
printf("\n %d", *(p+2)); 11
printf("\n %d", *(p+3)); 22
printf("\n %d", *(p+4)); 33
printf("\n %d", *(p+5)); 44
printf("\n %d", *(p+6)); 55
printf("\n %d", *(p+7)); 66
printf("\n %d", *(p+8)); 77
printf("\n %d", *(p+9)); 88
} 99
PPS UNIT-II 110
Arrays and Pointers:
int main( )
{
int a[10]={11,22,33,44,55,66,77,88,99,110};
int *p;

100

100

100

100

101

101

101

101

102

102
C

C
0

4
p=a+2;
a 11 22 33 44 55 66 77 88 99 110
printf("\n %d", *p); 0 1 2 3 4 5 6 7 8 9
printf("\n %d", *(p+0));
printf("\n %d", *(p+1));
Output:
printf("\n %d", *(p+2)); p 1008 33
printf("\n %d", *(p+3));
33
printf("\n %d", *(p+4));
44
printf("\n %d", *(p+5));
55
printf("\n %d", *(p+6));
66
printf("\n %d", *(p+7));
77
printf("\n %d", *(p-1));
88
printf("\n %d", *(p-2));
99
110
}
22
11
PPS UNIT-II
Arrays and Pointers:

p=a+2;

a[0] *(a+0) ? ? 11
a 1000
a[1] *(a+1) ? ? 22
0 11 1000
p 1008 a[2] *(a+2) ? ? 33
1 22 1004
2 33 1008 a[3] *(a+3) ? ? 44

3 44 100C a[4] *(a+4) ? ? 55


4 55 1010

PPS UNIT-II
Arrays and Pointers:

p=a+2;

a[0] *(a+0) *(p-2) p[-2] 11


a 1000
a[1] *(a+1) *(p-1) p[-1] 22
0 11 1000
p 1008 a[2] *(a+2) *(p+0) p[0] 33
1 22 1004
2 33 1008 a[3] *(a+3) *(p+1) p[1] 44

3 44 100C a[4] *(a+4) *(p+2) p[2] 55


4 55 1010

PPS UNIT-II
Arrays and Pointers:
Two Dimensional arrays
int main( )
int main( ) {
{ int b[3][4]={{5,10,15,20},
int b[3][4]={{5,10,15,20}, {25,30,35,40},
{25,30,35,40}, {45,50,55,60}};
{45,50,55,60}}; int *r;

printf("\n%d %d",**b,b[0][0]); r=&b[0][0];

printf("\n%d",*(*(b+1))); printf("\n%d",*r);

printf("\n%d",*(*(b+2)+2)); printf("\n%d",*(r+1));

} printf("\n%d",r[10]);
}

PPS UNIT-II
Array of pointers:

int main( )
{
int a=6,b=2,c=8,d=4,e=9,i;
int *p[5];
p[0]=&a; b d
2 4
p[1]=&b;
p[2]=&c; a 6 c 8 e 9
p[3]=&d;
p[4]=&e;
for(i=0;i<5;i++)
printf("\n%d",*p[i]); p
}

PPS UNIT-II
Array of pointers:
int main( )
int main( )
{
{
int a=6,b=2,c=8,d=4,e=9,i;
int a[5]={11,22,33,44,55};
int *p[5];
int *p;
p[0]=&a;
p=&a[0];
p[1]=&b;
printf("\n %d", *(p+0));
p[2]=&c;
printf("\n %d", *(p+1));
p[3]=&d;
printf("\n %d", *(p+2));
p[4]=&e;
printf("\n %d", *(p+3));
for(i=0;i<5;i++)
printf("\n %d", *(p+4));
printf("\n%d",*p[i]);
}
}
PPS UNIT-II
Pointers in Structures:
struct Sample
{ x y z
int x;
s1 25 R
float *y;
char z;
};
int main( )
{
float m=5.67;
m 5.67
struct Sample s1;
s1.x=25;
s1.y=&m;
s1.z='R';
Output:
printf("\n %d",s1.x);
25
printf("\n %f",*s1.y);
5.670000
printf("\n %c",s1.z);
R
}
PPS UNIT-II
Pointers to Structures: int main( )
{
#include<stdio.h> struct Sample s1={25,8.9,’R’};
struct Sample struct Sample *s2;
{
int x; s2=&s1;
float y;
char z; (*s2).x=45;
}; (*s2).y=7.1;
(*s2).z='G';
x y z
printf("\nx=%d",s1.x);
s1 65
45
25 2.3
7.1
8.9 K
G
R printf("\ny=%f",s1.y);
printf("\nz=%c",s1.z);

s2->x=65;
s2->y=2.3;
s2->z='K';

s2 printf("\nx=%d",s1.x);
printf("\ny=%f",s1.y);
printf("\nz=%c",s1.z);
PPS UNIT-II }
Pointers to Structures: x y z
struct Sample s1 25 8.9 R
{
int x; s1.x=35
float y; (*s2).x=45
char z;
}; s2->x=65
int main( )
{ s2
struct Sample s1={25,8.9,’R’};
struct Sample *s2; Output:

s2=&s1; x=25
y=8.900000
printf("\nx=%d",s1.x); z=R
printf("\ny=%f",s1.y);
printf("\nz=%c",s1.z); x=25
y=8.900000
printf("\nx=%d",s2->x); z=R
printf("\ny=%f",s2->y);
printf("\nz=%c",s2->z);
} PPS UNIT-II
Pointers and Structures: struct Sample
{
struct Sample int x;
{ float y;
int x; char z;
float *y; };
char z; int main( )
}; {
int main( ) struct Sample s1={25,8.9,’R’};
{ struct Sample *s2;
float m=5.67;
struct Sample s1; s2=&s1;
s1.x=25;
s1.y=&m; (*s2).x=45;
s1.z=‘R’;
s2->x=65;
*s1.y=3.8;
x y z
x y z
s1 25 8.9 R
s1 25 R

m 5.67
s2
PPS UNIT-II
Self referential structures:

A self-referential structure is a structure which contains a pointer of the


same type (same structure) as its member.

struct struct_name
{
datatype datatypename;
struct struct_name * pointer_name;
};

struct Sample
{
int x;
float y;
char z;
struct Sample *p1;
};
PPS UNIT-II
Self referential structures:
struct Sample int main( )
{ {
int x; struct Sample s1;
float y; struct Sample s2={12,45.6,'D',NULL};
char z;
struct Sample *p1; s1.x=85;
}; s1.y=7.2;
s1.z='J';
s1.p1=&s2;

s1 85 7.2 J printf("\n %d",s1.x);


printf("\n %f",s1.y);
printf("\n %c",s1.z);
printf("\n %d",(s1.p1)->x);
printf("\n %f",s1.p1->y);
s2 12 45.6 D NULL printf("\n %c",s1.p1->z);
printf("\n %p",s1.p1->p1);
}
PPS UNIT-II
Pointer Arithmetic:

Valid operations on pointers are:


 Assignment of pointers
 Adding or subtracting a pointer and integer
 Subtracting two pointers
 Comparing two pointers
 Incrementing or decrementing the pointers
 Assigning the value 0 to the pointer.

Invalid operations on pointers are:


 Addition, Multiplication, Division of two pointers
 Multiplication, Division of pointer with a number

PPS UNIT-II
Lvalue and Rvalue:

In C, an expression is either an lvalue or an rvalue. As you know, every


expression has a value.

An lvalue expression must be used whenever the object is receiving a


value.
An rvalue expression can be used to supply a value.

PPS UNIT-II
Pointer Arithmetic: Program1

#include<stdio.h>
int main( )
{
int a,*b,*c;
b=&a;
c=b; Output:
b=c;
if(b!=&a) Equal
printf("\nNot Equal");
else
printf("\nEqual");
}

PPS UNIT-II
Pointer Arithmetic: Program2

int main( )
{
int a[ ]={6,2,8,4,9},*b;
for(b=a;b<=a+4;b++) Output:
printf("\n%d",*b);
} 6
2
8
4
9

PPS UNIT-II
Pointer Arithmetic: Program3

int main( )
{
int a[ ]={6,2,8,4,9},b[ ]={7,5,3,9};
b=a;
a=b;
printf("\n%d",*b); Output:
}
Error Lvalue required

PPS UNIT-II
Pointer Arithmetic: Program4
int main( )
{
int a[5],*p,i;
float b[5],*q;
for(i=0;i<5;i++)
{
a[i]=1; b[i]=0.3;
} Output:
p=&a[3];
q=&b[3]; 1 0.3
*p=5; 1 0.3
*(p-1)=7; 7 0.4
*(p+1)=9; 8 0.2
*q=.2; 9 0.6
*(q-1)=.4;
*(q+1)=.6;
for(i=0;i<5;i++)
printf("\n%d\t%f",a[i],b[i]);
}
PPS UNIT-II
Pointer Arithmetic: Program5
int main()
{
int a[ ]={6,2,8,4,9},*p,n;
p=a+2;
n=*p;
printf("\nOutput1:%d",n);
n=*++p;
printf("\nOutput2:%d",n); Output:
n=*(--p);
printf("\nOutput3:%d",n); Output1:8
n=*p- -; Output2:4
printf("\nOutput4:%d",n); Output3:8
n=(*p)++; Output4:8
printf("\nOutput5:%d",n); Output5:2
n=++(*p); Output6:4
printf("\nOutput6:%d",n); Output7:3
n=--*p; Output8:8
printf("\nOutput7:%d",n);
n=*(++p);
printf("\nOutput8:%d",n);PPS UNIT-II
}
Memory Allocation Functions:
Three dynamic memory allocation functions:
malloc
calloc
realloc
One memory collection function:
free

void* malloc (size);


void* calloc(element-count, element-size)
void * realloc(void* ptr, size_t newSize)
void free(void* ptr)

PPS UNIT-II
Memory Allocation Functions:
#include<stdio.h>
#include<stdlib.h>
int main( )
{
int *p,*q;

p=(int*)malloc(sizeof(int));
*p=20;
printf("\n%d",*p);

q=(int*)calloc(10,sizeof(int));
*(q+0)=10;
*(q+1)=20;
*(q+2)=30;
printf("\n%d",*(q+0));
printf("\n%d",*(q+1));
printf("\n%d",*(q+2));

free(p);
free(q);
}
PPS UNIT-II
Void pointers:

A pointer to void is a generic pointer that can be used to represent any


data type during compilation or runtime.

PPS UNIT-II
Thank You

PPS UNIT-II 113

You might also like