arrays,strings,structures
arrays,strings,structures
Strings
Structures
Pointers
PPS UNIT-II
Unit – II: Arrays, Strings, Structures and Pointers
PPS UNIT-II
Arrays
PPS UNIT-II
Data Types:
C Types
Floating
Integral
Point
PPS UNIT-II
PPS UNIT-II
Arrays:
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("\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]
}
}
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);
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};
printf("\nEnter 6 Elements");
for(i=0;i<2;i++)
for(j=0;j<3;j++) Output:
scanf("%d",&a[i][j]);
#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:
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).
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:
puts
char p[50];
char q[50];
scanf("%s",p);
gets(q);
printf("%s",p);
puts(q);
PPS UNIT-II
Strings Input / Output:
Output: Output:
PPS UNIT-II
String Manipulation Functions:
string.h header file #include<string.h>
char *strcpy (char *dest, char *src) - Copy src string into dest string.
char *strncat(char *dest, const char *src, int n); - Concatenate n chars.
char *strtok(char *s, const char *delim) - Parse the string s into tokens
using delim as delimiter.
PPS UNIT-II
String Conversion Functions:
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;
#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:
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:
PPS UNIT-II
Structures:
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;
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:
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.
struct structurename
{
type member_name : width ;
};
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:
PPS UNIT-II
Pointers
PPS UNIT-II
Pointers:
A pointer is a special kind of variable.
Pointers are designed for storing memory address i.e. the address of
another variable.
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.
PPS UNIT-II
Declaring Pointers:
PPS UNIT-II
Declaring Pointers:
56248
int a=25; a 25
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);
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
c=a+b;
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;
c=a+b; *r=*p+*q;
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:
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:
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
PPS UNIT-II
Arrays and Pointers:
p=a+2;
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",*(*(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:
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;
PPS UNIT-II
Lvalue and Rvalue:
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
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:
PPS UNIT-II
Thank You