0% found this document useful (0 votes)
37 views102 pages

Pointers

Uploaded by

gvnviki17
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)
37 views102 pages

Pointers

Uploaded by

gvnviki17
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/ 102

practical learning redefined

One point solution for all your placement needs


C Programming
Session 3.1

2
Data Types

Primary /Fundamental Data - int


types
Derived Data types - float

User Defined Data types - double

- char
- Array
Derived Data types
- Pointer

- Structure

- Union
int num [100]; -100 integer values can be stored in num array

float a[10]; - 10 float values can be stored in this array


Collection of similar data types - Array
char b[10]; - 10 characters can be stored in this array
Derived data type - Array

int arr[4] = {1, 2, 3, 4};

- Memory is allocated 1000 1002 1004 1006

- Size of int is 2 bytes


- Address values are
 2 bytes x 4 elements
continuous
 8 bytes
Derived data type - Array

int arr[4] = {1, 2, 3, 4};


1 2 3 4
int arr[ ] = {1, 2, 3, 4}; 1000 1002 1004 1006
arr[0] arr[1] arr[2] arr[3]
int arr[4] = {1, 2, 3, 4}
1 2 3 4
1000 1002 1004 1006

Write a program to sum


- Index values
all the values stored in - Starts from 0 till n-1 (n - size)
an array. int Sum = arr[0] + arr[1] + arr[2]+ arr[3];
Sum = 10;
int arr[ 10 ] = {1, 2, 3, 4};
int arr[ 10 ] = {1, 2, 3, 4};
- Size is 10 (Memory is allocated for 10 int values)
int arr[ 10 ] = {1, 2, 3, 4};
- Size is 10 (Memory is allocated for 10 int values)
- 2 bytes x 10 elements = 20 bytes
- 4 values are assigned to arr

1000 1002 1004 1006 1008 1010 1012 1014 1016 1018
int arr[ 10 ] = {1, 2, 3, 4};
- Size is 10 (Memory is allocated for 10 int values)
- 2 bytes x 10 elements = 20 bytes
- 4 values are assigned to arr
- Unassigned elements will have Garbage Value

1 2 3 4 GV GV GV GV GV GV
1000 1002 1004 1006 1008 1010 1012 1014 1016 1018
int a [ 10 ] = {1, 2, 3, 4};
- Size is 10 (Memory is allocated for 10 int values)
- 2 bytes x 10 elements = 20 bytes
- 4 values are assigned to arr
- Unassigned elements will have Garbage Value
- Index values from 0 – 9 (Size -1)

1 2 3 4 GV GV GV GV GV GV
1000
a [0] a1002
[1] a1004
[2] a1006
[3] a1008
[4] a1010
[5] a1012
[6] a1014 [8] a1018
[7] a1016 [9]
Character Array - String

char a [5] = “TAMIL”; char a [10]; // Declaration


char a [5] = { ‘T’, ‘A’, ‘M’, ‘I’, ‘L‘\0’}; a = “TERV”; // Initialization
char a [10] = “YOUR NAME”; char
char aa [[ 4]]==“TAMIL”;
“TAMIL”;

char a [ ] = “Ed Tech”; - Compiler cannot place \0 at the end


char a [ ] = { ‘T’, ‘A’, ‘M’, ‘I’,’L' }; - Always, size of char array -
No. of char in a string + 1 (for null)
#include <stdio.h>
int main () { // For loop to scan the array values
int i, num[5];
// No. of times loop executes
printf("Enter 5 values:"); i=0 to i<5 (i.e., 5 times)
for( i = 0; i < 5; i++ )
// i=0,  scans the first value and
scanf("%d", &num[ i ] ); stores at the index 0
printf("\n The entered values are:");
for( i = 0; i < 5; i++ ) // i=1,  scans the second value
and stores at the index 1
printf("\n num[%d] is = %d ", i, num[i]);
return 0;
. . . Continues till i=4.
}
#include <stdio.h>
int main () { // For loop to print the array values
OUTPUT:
int i, num[5];
// No.5of
Enter times loop executes
values:
printf("Enter 5 values:"); Write ai=0program to get
to i<5 (i.e., the array
5 times)
12345
for( i = 0; i < 5; i++ ) values as input from the user
//
Thei=0,  prints
entered the are:
values first value from
scanf("%d", &num[ i ] );
=1 the and
using[0]scanf()
num indexprint
0 the
printf("\n The entered values are:"); num [1] =2
numbers
// i=1,  in the the
prints reverse order.
second value
for( i = 0; i < 5; i++ ) num [2] =3
printf("\n num[%d] is = %d ", i, num[i]); num [3] =4 from the index 1
num [4] =5
return 0;
. . . Continues till i=4.
}
#include <stdio.h>
int main () {
int i, num[5];
printf("Enter 5 values:");

for( i = 0; i < 5; i++ )


Write a program to get the array
scanf("%d", &num[ i ] ); values as input from the user
printf("\n The entered values are:"); using scanf() and print the

for( i = 4; i >= 0; i-- ) numbers in the reverse order.


printf("\n num[%d] is = %d ", i, num[i]);

return 0;
}
#include <stdio.h>
int main () {
int i, num[5];
printf("Enter 5 values:"); // For loop to print the array values

for( i = 0; i < 5; i++ )


scanf("%d", &num[ i ] );

printf("\n The entered values are:");

for( i = 4; i >= 0; i-- )


printf("\n num[%d] is = %d ", i, num[i]);

return 0;
}
#include <stdio.h>
int main () { // For loop to print the array values
int i, num[5];
printf("Enter 5 values:");
// No. of times loop executes
for( i = 0; i < 5; i++ ) i=4 to i>=0 (i.e., 5 times)
scanf("%d", &num[ i ] );
// i=0,  prints the last (fifth) value
printf("\n The entered values are:"); from the index 4

for( i = 4; i >= 0; i-- ) // i=1,  prints the fourth value


printf("\n num[%d] is = %d ", i, num[i]); from the index 3

return 0;
} . . . Continues till i=0.
#include <stdio.h>
int main () {
int i, num[5];
printf("Enter 5 values:"); OUTPUT:
for( i = 0; i < 5; i++ ) Enter 5 values:
scanf("%d", &num[ i ] );
12345
printf("\n The entered values are:"); The values are reversed:
num [4] =5
for( i = 4; i >= 0; i-- ) num [3] =4
printf("\n num[%d] is = %d ", i, num[i]); num [2] =3
num [1] =2
return 0;
}
num [0] =1
2D Array Col _0 Col_1

a[0][0] a[0][1]
int arr[4] = {1, 2, 3, 4};
Row_0 1 2
int arr[2][2]; a[1][0] a[1][1]
Row_1 3 4
int a[2][2] = { {1,2}, {3, 4} };

Row Column
2D Array
Col _0 Col_1 Col_2 Col_3
int arr[2][4]; a[0][0] a[0][1] a[0][2] a[0][3]
int a[2][4] = { {1,2,3,4}, Row_0 1 2 3 4
{5, 6,7,8} };
a[1][0] a[1][1] a[1][2] a[1][3]
or Row_1 5 6 7 8
int a[2][4] = {1,2,3,4,5,6,7,8};
Write a program to get the n values as input
from the user and store it in 2D array, print the
values as an output.
#include <stdio.h>
int main () printf("\n The entered values are:");

{ for( i=0; i<2; i++)

int i, j, num[2][4]; {

printf("Enter the values:"); for(j=0; j<4; j++)

for( i=0; i<2; i++) printf("\n num[%d][%d] is = %d ", i, j, num[i][j]);

{ }

for(j=0; j<4; j++) return 0;

scanf("%d", &num[ i ][j] ); }

}
Challenge: 48

Predict the output of following code:


#include<stdio.h>
int main()
{
int a[20]={1,2,3}; a) 20
printf(“%d”, sizeof(a)); b) 6
return 0;
c) Error
}
d) 40 or 80
Challenge: 49

Predict the output of following code:


#include<stdio.h>
int main()
{
int a[3]={1,2,3}; a) 2
printf(“%d”, 2[a]); b) 6
return 0;
c) 3
}
d) Error
Challenge: 50

Predict the output of following code:


#include<stdio.h>
int main()
{
printf(“%c”,2[“hai”]);
a) 2
return 0;
b) i
}
c) h

d) Error
Challenge: 51
Predict the output of following code:
#include<stdio.h>
void main()
{
int a[5] = {5, 1, 15, 20, 25};
int i, j, m; a) 3, 2, 15
i = ++a[1];
j = a[1]++; b) 2, 3, 20

m = a[i++]; c) 2, 1, 15
printf("%d, %d, %d", i, j, m);
} d) 1, 2, 5
Challenge: 52

Predict the output of following code:


To find the number of elements in
#include<stdio.h>
an array - sizeof(arr)/sizeof(arr[0])
void main()
{
float arr[] = {12.4, 2.3, 4.5, 6.7}; a) 5

printf("%d", sizeof(arr)/sizeof(arr[0])); b) 4

} c) 6

d) 7
Challenge: 53

Point out the Error :


#include<stdio.h>
int main()
{
int a[][]={{1,2},{3,4},{5,6}};
Column size is must
Error: _________________
printf(“%d”, a[1][1]);
return 0;
}
int main()
Address in C
{
Number
int Number= 5;
5
printf("Value =%d\n", Number); 26778
printf("Address =%p", &Number);
return 0;
}
int main()
Address in C
{
Number
int Number= 5;
5
printf("Value =%d\n", Number); 26778
printf("Address =%p", &Number);
return 0;
}
int main()
Address in C
{
Number
int Number= 5;
5
printf("Value =%d\n", Number); 26778
printf("Address =%p", &Number);
scanf("%d", &Value);
return 0;
& - Address of the variable value
}
& - reference operator
int main()
Address in C
{
Number
int Number= 5; 5
printf("Value =%d\n", Number); 26778
printf("Address =%p", &Number);
return 0; Value = 5
} &Value = 26778
Number
Value 5 can be accessed either by:
5
1. variable name (Number) or
2. address value (2024) *Pointers 2024

How to access a variable


using it's address?
int a;  Integer variable
*Pointers float b;  Float variable

int *ptr;  Pointer variable


int num = 10; num 10
1000
*Pointers
int *ptr;
ptr = &num;
ptr 1000
& - Address of num
2000
(i.e.,) ptr = 1000; ptr – pointing to the integer
variable (location 1000)
#include<stdio.h>
int main() { 101
int a=10, b=20, c=30; 103

int *ptr1, *ptr2, *ptr3; 105

ptr1=&a; 107

ptr2=&b; 109

ptr3=&c; 1011

return 0; 1013

} 1015
#include<stdio.h>
int main() { a 10 101
int a=10, b=20, c=30; 103

int *ptr1, *ptr2, *ptr3; b 20 105

ptr1=&a; c 30 107

ptr2=&b; 109

ptr3=&c; 1011

return 0; 1013

} 1015
#include<stdio.h>
int main() { a 10 101
int a=10, b=20, c=30; 103
int *ptr1, *ptr2, *ptr3; b 20 105
ptr1=&a; c 30 107
ptr2=&b; 109

ptr3=&c; ptr1 1011

return 0; ptr2 1013

} ptr3 1015
#include<stdio.h>
int main() { a 10 101
int a=10, b=20, c=30; 103
int *ptr1, *ptr2, *ptr3; b 20 105
ptr1=&a; // ptr1=101 c 30 107
ptr2=&b; 109

ptr3=&c; ptr1 101 1011

return 0; ptr2 105 1013

} ptr3 107 1015


#include<stdio.h> int a, *p;
int main() {
p = a;c=30; a*p = &a;
10 101
int a=10, b=20,
int *ptr;
103
ptr = &a; p = &a; *p = a;20
b 105
printf("%d\n", *ptr); 10 c 30 107
ptr = &b;
109
printf("%d\n", *ptr); 20 107
105
ptr 101 1011
ptr = &c;
printf("%d", *ptr); 1013
30
return 0; } 1015
int a, *p;
p = a; *p = &a;

p = &a; *p = a;
a – contains int value
p – contains address value  pointer variable points to integer location
&a – address of a
&p – address of p
*p – value pointed by the address
int a, *p;
p = a; *p = &a;

p = &a; *p = a;
a – contains int value
p – contains address value
&a – address of a
&p – address of p
*p – value pointed by the address
int a, *p;
p = a; *p = &a;

p = &a; *p = a;
a – contains int value
p – contains address value
&a – address of a
&p – address of p
*p – value pointed by the address
int a, *p;
p = a; *p = &a;

p = &a; *p = a;
a – contains int value
p – contains address value
&a – address of a
&p – address of p
*p – value pointed by the address
int a, *p;
p = a; *p = &a;

p = &a; *p = a;
a – contains int value
p – contains address value
&a – address of a
&p – address of p
*p – value pointed by the address
Challenge: 54

Predict the output of following code:

#include<stdio.h>
void main()
{
a) Error
int a[3][2]={{1,2},{3,4},{5,6}};
b) Garbage Value
printf(“%d, %d, %d\n”, a[2][1],*(a[2]+1),*(*(a+2)+1));
c) 6, 6, 6
}
d) 2, 3, 4
Challenge: 55

Predict the output of following code:


#include<stdio.h>
void main()
{
int a=10,*p;
int *vp; a) 1010
p=&a;
b) Garbage Value
vp=p;
printf(“%d”,*p); c) Typecasting – Error
printf(“%d”,*vp); d) 10, Garbage value
}
Challenge: 56
a 10 b 20
Predict the output :
1000 2000
void main() {
int a=10, b=20;
int *p, *q, **pp, **qq;
p=&a; p 1000 q 2000
q=&b; 3000 4000
pp=&p;
qq=&q;
}
pp 3000 qq 4000
Consider the address of a as 1000, b as 2000,
5000 6000
p as 3000, q as 4000 and pp as 5000
a = 10
p = 1000 a 10 b 20
&p = 3000 1000 2000
*p = 10
pp = 3000
&pp = 5000 p 1000 q 2000
*pp = 1000 3000 4000
**pp = 10
b = 20
q = 2000 pp 3000 qq 4000
*q = 20 5000 6000
**qq = 20
p = q; p = 2000
a = 10 a 10 b 20
p = 2000 1000 2000
&p = 3000
*p = 20
pp = 3000 p 2000 q 2000
&pp = 5000 3000 4000
*pp = 2000
**pp = 20
b = 20 pp 3000 qq 4000
q = 2000 5000 6000
*q = 20
*p =300; a = 300
a = 300 a 300 b 20
p = 1000 1000 2000
&p = 3000
*p = 300
pp = 3000 p 1000 q 2000
&pp = 5000 3000 4000
*pp = 1000
**pp = 300
b = 20 pp 3000 qq 4000
q = 2000 5000 6000
*q = 20
p =300; p = 300
a = 10 a 10 b 20
GV
p = 300 1000 2000
&p = 3000 300
*p = GV
pp = 3000 p 300 q 2000
&pp = 5000 3000 4000
*pp = 300
**pp = GV
b = 20 pp 3000 qq 4000
q = 2000 5000 6000
*q = 20
Challenge: 57

Predict the output of following code:


#include<stdio.h>
void main()
{
int a=10,*p; a) 10

p=&a; b) Address of a
printf("%d",*&*p); c) Error
}
d) Address of p
Challenge: 58

Predict the output of following code:


#include<stdio.h>
void main() {
int i=10,j=20;
int *p,*q; a) 10, 10
*p=i; b) 10, 20
q=&j;
c) Error
printf(“%d, %d”,*p,*q);
d) Garbage value, 20
}
Challenge: 59
Predict the values:
int a[5] = { 1, 3, 5, 7, 9};
int *p; // assume the starting location of array is 1000
p =&a;

*a = 1 *(a+0) = 1 a[0] = 1 *p = 1 *(p+0) = 1


&a = 1000 *(a+3) = 7 a[3] = 7 &p = GV *(p+3) = 7
Collection of similar data types - Array

struct person
int number[10];
{
Collection of different data
- Can store upto 10 numbers
types - Structure
int age;
char name[20];
};
struct student
{ How to access structure members?
int roll_no;
char name[20]; Members

float marks;
};

- semicolon (;) is must


struct student
{
How to access structure members?
int roll_no;
-Declare the structure variable
char name[20];
-using dot (.) operator
float marks;
};
struct student stud1; structure variable
stud1.roll_no = 10;
struct student
roll_no
{ (2 bytes)
int roll_no;
name
char name[20]; (20 bytes) 26 bytes

float marks; marks


}; (4 bytes)

struct student stud1;


- When the structure members are - Memory is allocated only when
declared, no memory is allocated. structure variables are created.
struct student
{ stud1 stud2 stud3
roll_no roll_no roll_no
int roll_no; (2 bytes) (2 bytes) (2 bytes)
char name[20]; name name name
(20 bytes) (20 bytes) (20 bytes)
float marks;
marks marks marks
};
(4 bytes) (4 bytes) (4 bytes)
struct student stud1, stud2, stud3;
26 bytes 26 bytes 26 bytes
- n number of Structure variables
can be created
struct student struct student
{ {
int roll_no; int roll_no;
char name[20]; char name[20];
float marks; float marks;
}; } stud1 ;
struct student stud1 ;
stud1.roll_no =10;
stud1.roll_no =10;
struct student struct student
{ {
int roll_no; int roll_no;
char name[20]; char name[20];
float marks; float marks;
}; } stud1 ={10, “Ram”, 98.6};
struct student stud1 =

{10, “Ram”,98.6};
union student
{
How to access union members?
int roll_no;
char name[20]; Members

float marks;
};

- semicolon (;) is must


union student
{
How to access structure members?
int roll_no;
-Declare the union variable
char name[20];
-using dot (.) operator
float marks;
};
union student stud1 ; union variable
stud1.roll_no = 10;
union student
{ stud1
int roll_no;
char name[20]; (20 bytes)

float marks;
};
union student stud1 ;
- When the union members are - Memory is allocated only when
declared, no memory is allocated. union variables are created.
union student
{
stud1 stud2 stud3
int roll_no;
(20 bytes) (20 bytes) (20 bytes)
char name[20];
float marks;
};
union student stud1, stud2, stud3;
- n number of union variables
can be created
#include <stdio.h>
#include <string.h>
union Data {
int a;
int b;
float c;
};
int main( ) {
union Data data1;
data1.a = 10;
data1.b = 220;
printf( "data1.a : %d\n", data1.a); Output:
printf( "data1.b : %d\n", data1.b);
return 0;
data1.a : 220
} data1.b: 220
#include <stdio.h>
#include <string.h>
union Data { data1
int a;
int b; Max size – 4 bytes (4 bytes)
float c; 10
};
int main( ) {
union Data data1;
data1.a = 10;
data1.b = 220;
printf( "data1.a : %d\n", data1.a); Output:
printf( "data1.b : %d\n", data1.b);
return 0;
data1.a : 220
} data1.b: 220
#include <stdio.h>
#include <string.h>
union Data { data1
int a;
int b; Max size – 4 bytes (4 bytes)
float c; 220
10
};
int main( ) {
union Data data1;
data1.a = 10;
data1.b = 220;
printf( "data1.a : %d\n", data1.a); Output:
printf( "data1.b : %d\n", data1.b);
return 0;
data1.a : 220
} data1.b: 220
#include <stdio.h>
#include <string.h>
union Data { data1
int a;
int b; Max size – 4 bytes (4 bytes)
float c; 220
};
int main( ) {
union Data data1;
data1.a = 10;
data1.b = 220;
printf( "data1.a : %d\n", data1.a); Output:
printf( "data1.b : %d\n", data1.b);
return 0;
data1.a : 220
} data1.b: 220
#include <stdio.h>
#include <string.h>
union Data { -Union allocates
data1one common storage
int a;
int b; Max size – 4 bytes
space for its(4all
bytes)
members
-In union we 220
float c;
}; can access only one
int main( ) { member of union at a time
union Data data1;
data1.a = 10;
data1.b = 220;
printf( "data1.a : %d\n", data1.a); Output:
printf( "data1.b : %d\n", data1.b);
return 0;
data1.a : 220
} data1.b: 220
Structure Union
-For defining structure use struct -For defining union use union
keyword keyword
-Structure occupies more memory -Union occupies less memory space
space than union than Structure
-Structure allocates separate storage -Union allocates one common
space for its every members storage space for its all members
-In Structure we can access all -In union we can access only one
members of structure at a time member of union at a time
Challenge: 60

Predict the output of following code:


struct student
{
int stuid=1234;
char stuname[5]= “abcde”; a. 1234,abcde
} s1;
void main() { b. 12341234
struct student s1;
printf( “%d,%s”,s1.stuid,s1.stuname);
c. Error
}
Challenge: 61

Predict the output of following code:


struct employee
{
int empid;
a. 13, 13
float empbasic;
} emp1={13}; b. 13,0.000000
void main() {
struct employee ; c. Error
d. 13, Garbage value
printf(“%d,%f”,emp1.empid,emp1.empbasic);
}
Challenge: 62

Predict the output of following code:


void main()
{
struct mystruct
{ a. Error in declaration
int a; b. Error in variable ‘b’ declaration
c. No Error
mystruct b;
mystruct *p; d. both a and b
};
}
Challenge: 63
Predict the output:
struct student
{ int stuid;
char stuname[5];
} s1={1234,“abcde”};
void main() a. Error
{ b. True
struct student s2={5678, “abcde”};
c. false
if(s1==s2) { printf(“true”); }
d. truefalse
else { printf(“false”); }
}
Challenge: 64

Predict the output:


union temp
{
int m1;
char ch;
};
a. 1010
void main()
{ b. 2020
union temp u1;
u1.m1=10; c. 1020
u1.ch=20;
d. Error
printf(“%d%d”,u1.m1,u1.ch);
}
Challenge: 65
Predict the output:
struct birthdate
{
int date;
int month;
int year;
};
void main() { a. 10
union student
{ b. 28
int stuid;
char stuname[2]; c. 6
struct birthdate dob;
} u1={1234, “ab”}; d. Error
printf(“%d”,sizeof(u1));
}
Challenge: 66
Predict the output:
struct mystruct
{
int x;
int y;
};
struct mystruct s1,*pp; a. 0 0
void main() 00
{
pp=&s1; b. Garbage values
printf("%d %d\n", (*pp).x, (*pp).y); c. 11
printf("%d %d\n", pp->x, pp->y);
} d. Error
- Typedef
User defined Data types
- Enum
Challenge: 67
#include<stdio.h>
enum week{Mon, Tue, Wed, Thu, Fri, Sat, Sun};
int main()
{
printf("%d", Wed); a. Error
return 0; b. Garage values
} c. 0
d. 2
Challenge: 68
#include<stdio.h>
enum week{Mon, Tue, Wed, Thu, Fri, Sat, Sun};
int main()
{
enum week day; a. Error
day = Wed; b. Garage values
printf("%d", Wed); c. 0
return 0; d. 2
}
Challenge: 69

#include<stdio.h>
enum week{Mon, Tue, Wed = 3, Thu, Fri, Sat, Sun};
int main()
{
a. Error
printf("%d, %d, %d", Mon, Wed, Sun);
b. Garage values
return 0;
c. 0, 3, 7
}
d. 1, 3, 6
Challenge: 70
#include<stdio.h>
enum year{Jan, Feb, Mar, Apr, May, Jun, Jul, Aug,
Sep, Oct, Nov, Dec};
int main() {
int i; a. Error
for (i=Jan; i<=Dec; i++) b. 0 to 11
printf("%d ", i); c. 1 to 12
return 0; d. Garbage value
}
Challenge: 71

#include <stdio.h>
enum State {Working = 1, Failed = 0, Freezed = 0};
int main()
{
a. Error
printf("%d, %d, %d", Working, Failed, Freezed);
b. 0, 0, 1
return 0;
c. 1, 0, 0
}
d. Garbage value
Challenge: 72

#include <stdio.h>
enum State {Working = 1.00, Failed = 0.00};
int main()
{
a. Error
printf("%d, %d", Working, Failed);
b. 1.00, 0.00
return 0;
c. 0, 0
}
d. Garbage value
Challenge: 73

#include <stdio.h>
enum state { working, failed };
enum result { failed, passed };

int main()
a. Compile time Error
{
return 0; b. Run-time error
} c. No output
d. Garbage value
Typedef: #include<stdio.h>
Typedef is a keyword used in C int main()
language to assign alternative {
names to existing data types. typedef char awesome;
awesome a[20] ="FACE";
printf("%s", a);
return 0;
} Output: FACE
1. Write a C program to read and print student details using
structure pointer.

Get three inputs from the user using a struct pointer:


Input: Mike
1. Name- string
101
2. Roll No- int
89.7
3. Percentage- float
Output:
and print the same in the output.
Name: Mike
Roll No: 101
Percentage: 89.7
#include <stdio.h> int main()
struct student {
{
ptr=&std;
char name[30];
scanf("%s", ptr->name); scanf("%d",&ptr->roll);
int roll;
float perc; scanf("%f",&ptr->perc);
} std, *ptr; printf("\nName:%s \nRollNo: %d \nPercentage: %.02f\n",
ptr->name,ptr->roll,ptr->perc);
return 0;
}
Input: Mike Output:
Name: Mike
101
Roll No: 101
89.7
Percentage: 89.7
2. Write a C program to read array elements using pointers and
print the value with the addresses

Get ‘n’ the number of elements to be stored in the array such that,n<=100.
Then, the read the elements of the array. Finally, print the array elements
along with their addresses. Print the value as a three-digit number and the
address as an 8-bit address.

Output:
Input: 3
AF1C1850 -> 001
123 AF1C1854 -> 002
AF1C1858 -> 003
#include <stdio.h>
int main()
{
int arr[100], n, *pa, i;
scanf("%d",&n); Input: 3
pa=&arr[0]; 123
for(i=0;i < n; i++)
Output:
scanf("%d",pa+i);
AF1C1850 -> 001
for(i=0;i<n;i++)
AF1C1854 -> 002
printf("%08X -> %03d\n",(pa+i),*(pa+i));
AF1C1858 -> 003
return 0;
}
3. Write a program in C to find the maximum number between
two numbers using a pointer.

Get two different integer numbers as an input and find the greatest
number among the two using pointers.

Input: Input:
-3 -9 12 34
Output: Output:
-3 34
#include <stdio.h>
#include <stdlib.h>
int main()
{
int num1, num2, *ptr1=&num1, *ptr2=&num2;
scanf("%d %d", ptr1, ptr2);
if(*ptr1>*ptr2)
Input:
printf("%d\n\n",*ptr1);
else 12 34

printf("%d\n\n",*ptr2); Output:
return 0; 34
}
4. Write a C program to print a string character by character
using pointer.

Get the string as an input and using pointers print the same to the stdout.

Input: Focus
Output: Focus
#include <stdio.h>
int main()
{
char str[100];
char *ptr;
scanf("%[^\n]s", str);
Input:
ptr=str;
Focus
while(*ptr!='\0')
Output:
printf("%c",*ptr++);
Focus
return 0;
}
5. Write a C program to calculate the length of the string.

Get the string as an input and find the lenght of the given string using user
defined function.

Input: Focus Input: Focus Academy


Output: 5 Output: 13
#include <stdio.h> int main()
int Length(char* ch) {
{
char str[100];
int count = 0;
scanf("%[^\n]s", str);
while (*ch != '\0')
{ int result = Length(str);
count++; printf("%d", result);
ch++; return 0;
} }
return count;
}
Thank you!

You might also like