Itp Unit-4
Itp Unit-4
SYLLABUS
I. Pointers,
Unions
web-D Page 1
SEM :- 1-1 (R23) introduction to programming UNIT-4
1.) Pointers in C:
Example:
int num = 10;
int* ptr; // Declare a pointer to an integer
ptr = # // Assign the address of 'num' to 'ptr'
The address operator (&) is used to get the memory address of a variable.
It returns the location in memory where the variable is stored.
web-D Page 2
SEM :- 1-1 (R23) introduction to programming UNIT-4
Example:
int x = 42;
int* ptr = &x; // 'ptr' now holds the address of 'x'
The dereferencing operator (*) is used to access the value stored at a memory address
pointed to by a pointer.
It allows you to manipulate the actual data stored in memory.
Example:
int y = *ptr; // 'y' now holds the value stored at the address
pointed by 'ptr'
int main() {
int num = 10;
int* ptr = #
return 0;
}
web-D Page 3
SEM :- 1-1 (R23) introduction to programming UNIT-4
Output:
Value of num: 10
Address of num: 0x7ffe47d51b4c
Address stored in ptr: 0x7ffe47d51b4c
Remember, pointers allow you to manipulate memory directly, but use them carefully to
avoid memory leaks and bugs! 🚀🚀🚀
web-D Page 4
SEM :- 1-1 (R23) introduction to programming UNIT-4
#include <stdio.h>
int main() {
int a = 22;
int *p = &a;
printf("p = %u\n", p); // Original address
p++;
printf("p++ = %u\n", p); // Incremented address
p--;
printf("p-- = %u\n", p); // Restored to original address
// Similar behavior for float and char pointers
return 0;
}
Output:
p = 1441900792
p++ = 1441900796
p-- = 1441900792
web-D Page 5
SEM :- 1-1 (R23) introduction to programming UNIT-4
o Example:
4. Comparison of Pointers:
o You can compare pointers using relational operators (<, >, <=, >=, ==, !=).
o Example:
Remember that pointer arithmetic operates on memory addresses, not actual values. It’s a
powerful tool for working with arrays, dynamic memory allocation, and data structures in
C.
1. Array of Pointers:
o An array of pointers is a collection of indexed pointer variables, each pointing
to a memory location. It’s useful when we want to point to multiple memory
locations of the same data type.
o Syntax: pointer_type *array_name[array_size];
o Example:
#include <stdio.h>
web-D Page 6
SEM :- 1-1 (R23) introduction to programming UNIT-4
int main() {
int var1 = 10;
int var2 = 20;
int var3 = 30;
int* ptr_arr[3] = { &var1, &var2, &var3 };
for (int i = 0; i < 3; i++) {
printf("Value of var%d: %d\tAddress: %p\n", i + 1,
*ptr_arr[i], ptr_arr[i]);
}
return 0;
}
Output:
web-D Page 7
SEM :- 1-1 (R23) introduction to programming UNIT-4
#include <stdio.h>
int main() {
char* arr[3] = { "Btech", "Btechify", "www.Btechify.com" };
for (int i = 0; i < 3; i++) {
printf("%s\n", arr[i]);
}
return 0;
}
Output:
Btech
Btechify
www.Btechify.com
Remember that pointers allow us to manipulate memory addresses directly, making them
essential for efficient array handling in C.
web-D Page 8
SEM :- 1-1 (R23) introduction to programming UNIT-4
1. Structures:
o A structure is a user-defined data type that allows you to group items of
different types into a single type.
o It provides a way to encapsulate related data fields.
o The struct keyword is used to define a structure.
o The size of a structure is equal to or greater than the total size of all its
members.
o Example:
#include <stdio.h>
struct Person {
char company[50];
int lifespan;
};
int main() {
struct Person person1;
strcpy(person1.company, “vinay prem");
person1.lifespan = 30;
printf("Name: %s\n", person1.company);
printf("Age: %d\n", person1.lifespan);
return 0;
}
Output:
2. Unions:
o A union is similar to a structure but with a key difference: all its members
share the same memory location.
o Only one member can store data at a given instance.
web-D Page 9
SEM :- 1-1 (R23) introduction to programming UNIT-4
#include <stdio.h>
union Data {
int i;
float f;
char str[20];
};
int main() {
union Data data;
data.i = 10;
printf("Data.i: %d\n", data.i);
data.f = 3.14;
printf("Data.f: %f\n", data.f);
strcpy(data.str, "Btechify, C");
printf("Data.str: %s\n", data.str);
return 0;
}
Output:
Data.i: 10
Data.f: 3.140000
Data.str: GeeksforGeeks, C
3. Enumeration (Enums):
o An enum allows you to create custom data types with a set of named integer
constants.
o It simplifies code readability.
o Example:
o
web-D Page 10
SEM :- 1-1 (R23) introduction to programming UNIT-4
#include <stdio.h>
enum Cafes {
Dyu_Art_Cafe,
Tea_Villa_Cafe,
The_Hole_in_the_Wall_Cafe,
// ...
};
User-defined data types like structures and unions enhance code organization, flexibility,
and abstraction in C programming.
web-D Page 11