0% found this document useful (0 votes)
110 views

Itp Unit-4

Introduction to programming in C unit 4
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
110 views

Itp Unit-4

Introduction to programming in C unit 4
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

SEM :- 1-1 (R23) introduction to programming UNIT-4

SYLLABUS
I. Pointers,

II. dereferencing and address operators,

III. pointer and address arithmetic,

IV. array manipulation using pointers,

V. User-defined data types-Structures and

Unions

web-D Page 1
SEM :- 1-1 (R23) introduction to programming UNIT-4

1.) Pointers in C:

 A pointer is a variable that stores the memory address of another variable.


 It allows you to manipulate data indirectly by accessing the memory location it points
to.
 Pointers are powerful tools for low-level memory access and dynamic memory
management.

 Example:
 int num = 10;
 int* ptr; // Declare a pointer to an integer
 ptr = # // Assign the address of 'num' to 'ptr'

Address Operator (&):

 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'

Dereferencing Operator (*):

 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'

Example: Using Pointers and Dereferencing


#include <stdio.h>

int main() {
int num = 10;
int* ptr = &num;

printf("Value of num: %d\n", num);


printf("Address of num: %p\n", &num);
printf("Address stored in ptr: %p\n\n", ptr);

printf("Dereferencing content in ptr using *ptr:\n");


printf("Value of *ptr: %d\n", *ptr);

*ptr = 6; // Modify the value using pointer


printf("Modified value of *ptr: %d\n", *ptr);
printf("Modified value of num: %d\n", num);

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

Dereferencing content in ptr using *ptr:


Value of *ptr: 10
Modified value of *ptr: 6
Modified value of num: 6

Remember, pointers allow you to manipulate memory directly, but use them carefully to
avoid memory leaks and bugs! 🚀🚀🚀

3.) Pointer and address arithmetic


Certainly! In C, pointer arithmetic involves manipulating memory addresses using
pointers. Let’s explore the key operations:

1. Increment and Decrement of a Pointer:


o When a pointer is incremented, it advances by the size of the data type it points
to. For example:
 If an integer pointer points to address 1000, incrementing it will move it
to address 1004 (since an int occupies 4 bytes).

web-D Page 4
SEM :- 1-1 (R23) introduction to programming UNIT-4

 Similarly, decrementing an integer pointer will move it back by 4 bytes.


o Example:

#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

2. Addition of Integer to a Pointer:


o When you add an integer value to a pointer, it’s first multiplied by the size of
the data type and then added to the pointer.
o Example:

int arr[5] = {10, 20, 30, 40, 50};


int *ptr = arr; // Points to the first element
ptr += 2; // Moves to the third element (30)

3. Subtraction of Integer from a Pointer:


o Similar to addition, subtraction works by adjusting the pointer based on the
data type size.

web-D Page 5
SEM :- 1-1 (R23) introduction to programming UNIT-4

o Example:

ptr -= 1; // Moves back to the second element (20)

4. Comparison of Pointers:
o You can compare pointers using relational operators (<, >, <=, >=, ==, !=).
o Example:

int *ptr1 = &arr[0];


int *ptr2 = &arr[2];
if (ptr1 < ptr2) {
printf("ptr1 comes before ptr2\n");
}

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.

4.) Array Manipulation In C


Certainly! Let’s delve into array manipulation using pointers in C. Pointers are
powerful tools for working with arrays, and they allow us to perform various operations
efficiently. I’ll cover some common scenarios:

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:

Value of var1: 10 Address: 0x7fff1ac82484


Value of var2: 20 Address: 0x7fff1ac82488
Value of var3: 30 Address: 0x7fff1ac8248c

o In this example, each element of the array ptr_arr is a pointer pointing to an


integer.
2. Array of Pointers to Characters (Strings):
o One common application of an array of pointers is to store multiple strings as
an array of pointers to characters.
o Syntax: char *array_name[array_size];
o Example:

char* arr[5] = { "gfg", "Btech", "Btechify", "Btechify.com",


"www.btechify.com" };

o Storing strings this way saves memory compared to a fixed-size array of


strings.
o Example:
o

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

3. Pointer Arithmetic for Array Access:


o We can use pointer arithmetic to access elements of an array.
o Example:

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


int* p = arr; // p points to the first element of arr
printf("%d\n", *(p + 1)); // prints 2

Remember that pointers allow us to manipulate memory addresses directly, making them
essential for efficient array handling in C.

5.) User-defined data types

Certainly! Let’s explore user-defined data types in C, specifically focusing on


structures and unions:

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:

Name: vinay prem


Age: 30

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

o The size of a union is determined by its largest member.


o Example:

#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,
// ...
};

Here, Dyu_Art_Cafe will be assigned 0, Tea_Villa_Cafe will be 1, and so on.

User-defined data types like structures and unions enhance code organization, flexibility,
and abstraction in C programming.

web-D Page 11

You might also like