Pointers
Pointers
2
Data Types
- char
- Array
Derived Data types
- Pointer
- Structure
- Union
int num [100]; -100 integer values can be stored in num array
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
return 0;
}
#include <stdio.h>
int main () {
int i, num[5];
printf("Enter 5 values:"); // For loop to print the array values
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
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:");
int i, j, num[2][4]; {
{ }
}
Challenge: 48
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
printf("%d", sizeof(arr)/sizeof(arr[0])); b) 4
} c) 6
d) 7
Challenge: 53
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
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 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
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
#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
p=&a; b) Address of a
printf("%d",*&*p); c) Error
}
d) Address of p
Challenge: 58
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;
};
{10, “Ram”,98.6};
union student
{
How to access union members?
int roll_no;
char name[20]; Members
float marks;
};
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
#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 ‘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.