Module - 5
By
Priti K. Zanje
• 1. Understanding the Computer’s Memory
To understand pointers, it’s important to know how memory works:
Memory is linear: It is divided into addressable locations (bytes), each with a unique address.
Variables are stored in memory and occupy a certain number of bytes.
Each variable has:
o Value (what it stores)
o Address (where it’s stored)
Example:
int a = 10;
// 'a' is stored somewhere in memory, say address 0x7ffe2a4c
• To access the memory location of a, we use the address-of operator (&a).
Memory Address
When a variable is created in C, a memory address is
assigned to the variable.
The memory address is the location of where the variable
is stored on the computer.
• When we assign a value to the variable, it is stored in
this memory address.
• To access it, use the reference operator (&), and the
result represents where the variable is stored:
• int myAge = 43;
printf("%p", &myAge); // Outputs 0x7ffe5367e044
2. Introduction to Pointers
A pointer is a variable that stores the address of another variable.
Syntax: data_type *pointer_name;
* is used to declare the pointer and also to dereference (access the value at the
address).
Why use pointers?
Dynamic memory allocation
Array and string handling
Efficient function arguments (pass-by-reference)
Building data structures like linked lists
3. Declaring Pointer Variables in C
int a = 5;
int *p; // Declaration: p is a pointer to an int
p = &a; // p stores the address of a
printf("Value of a: %d\n", a); // 5
printf("Address of a: %p\n", &a); // e.g., 0x7ffe2a4c
printf("Pointer p: %p\n", p); // same as &a
printf("Value at p: %d\n", *p); // Dereferencing → 5
Operator Description
& Address-of
* Dereference (value at addr)
Example Code: printf("Value of ptr: %p\n", ptr);
#include <stdio.h> printf("Value at ptr: %d\n",
int main() { *ptr);
int x = 42; return 0;
int *ptr; }
ptr = &x;
printf("Value of x: %d\n", x);
printf("Address of x: %p\n", &x);
Structure
Structures (also called structs) are a way to group several
related variables into one place.
Each variable in the structure is known as a member of
the structure.
• Unlike an array, a structure can contain many different
data types (int, float, char etc)S
Create a Structure
• You can create a structure by using the struct keyword
and declare each of its members inside curly braces:
struct MyStructure { // Structure declaration
int myNum; // Member (int variable)
char myLetter; // Member (char variable)
}; // End the structure with a semicolon
• To access the structure, you must create a variable of it.
• Use the struct keyword inside the main() method,
followed by the name of the structure and then the
name of the structure variable:
• Create a struct variable with the name "s1":
struct myStructure {
int myNum;
char myLetter;
};
int main() {
struct myStructure s1;
return 0;
}
Access Structure Members
• To access members of a int main() {
// Create a structure variable of
structure, use the dot myStructure called s1
syntax (.) struct myStructure s1;
// Create a structure // Assign values to members of s1
s1.myNum = 13;
called myStructure s1.myLetter = 'B';
struct myStructure { // Print values
int myNum; printf("My number: %d\n",
s1.myNum);
char myLetter; printf("My letter: %c\n",
}; s1.myLetter);
return 0;
}
• Now you can easily create multiple structure variables with
different values, using just one structure:
/ Create different struct variables
struct myStructure s1;
struct myStructure s2;
// Assign values to different struct variables
s1.myNum = 13;
s1.myLetter = 'B';
s2.myNum = 20;
s2.myLetter = 'C';
What About Strings in Structures?
• Remember that strings in C struct myStructure {
int myNum;
are actually an array of char myLetter;
characters, and char myString[30]; // String
};
unfortunately, you can't
int main() {
assign a value to an array struct myStructure s1;
like this:
// Trying to assign a value to the
string
s1.myString = "Some text";
• An error will occur:
// Trying to print the value
prog.c:12:15: error: printf("My string:
%s", s1.myString);
assignment to expression
with array type return 0;
}
• However, there is a struct myStructure {
int myNum;
solution for this! You can char myLetter;
char myString[30]; // String
use the strcpy() function };
and assign the value to int main() {
s1.myString like this: struct myStructure s1;
// Assign a value to the string
using the strcpy function
strcpy(s1.myString, "Some text");
// Print the value
• Output My string: Some printf("My string: %s",
text s1.myString);
return 0;
}
• You can also assign values to // Create a structure
struct myStructure {
members of a structure variable int myNum;
char myLetter;
at declaration time, in a single char myString[30];
line. Just insert the values in a };
comma-separated list inside int main() {
// Create a structure variable and
curly braces {}. Note that you assign values to it
don't have to use the strcpy() struct myStructure s1 =
{13, 'B', "Some text"};
function for string values with // Print values
this technique: printf("%d %c %s", s1.myNum,
s1.myLetter, s1.myString);
return 0;
}
C Unions
• In C, union is a user-defined data type that can contain elements of
the different data types just like structure.
• But unlike structures, all the members in the C union are stored in
the same memory location.
• Due to this, only one member can store data at the given point in
time.
Union Declaration
• A union is declared similarly to a structure. Provide the name of the
union and define its member variables:
union union_name{
type1 member1;
type2 member2;
type3 member3;
. .
. .
}
• After declaration of a union then create a variable of union like below:
union_name variable_name;
We can also declare a variable at the declaration of union
union union_name{
type1 member1;
type2 member2;
type3 member3;
. .
. .
} variable_name;
Initialization and Accessing
• The value of a union variable can be accessed using the dot (.)
operator. A value can be assigned to the union variable using the
assignment operator (=).
• In a union, all the variables share the same memory, so only one
variable can store a value at a time. If we try to access the value of
another variable, the behavior will be undefined.
#include <stdio.h> int main() {
// Declare a union variable
union Student data;
// Define a union with
// different data types // Assign and print the roll number
data.rollNo = 21;
union Student { printf("%d\n", data.rollNo);
int rollNo; data.height = 5.2;
printf("%.2f\n", data.height);
float height; data.firstLetter = 'N';
char firstLetter; printf("%c", data.firstLetter);
}; return 0;
}
Output: 21
5.20
N
Size of Union
• The size of the union will always be equal to the size of the largest
member of the union. All the less-sized elements can store the data in
the same space without any overflow. Let's take a look at the code
example:
#include <stdio.h>
union A{
int x;
char y;
};
union B{
int arr[10];
char y;
};
Sizeof A: 4 Sizeof B: 40
int main() {
// Finding size using sizeof() operator
printf("Sizeof A: %ld\n", sizeof(union A));
printf("Sizeof B: %ld\n", sizeof(union B));
return 0;
}
Array of Strings in C
• In C programming language, a string is an array of character
sequences terminated by NULL, it is a one-dimensional array of
characters.
• And, the array of strings is an array of strings (character array).
What is an Array of Strings in C?
• An array of strings is a two-dimensional array of character-type arrays
where each character array (string) is null-terminated.
• To declare a string, we use the statement −
• char string[] = {'H', 'e', 'l', 'l', 'o', '\0’};
Or
• char string = "Hello";
Declare and Initialize an Array of
Strings
• To declare an array of strings, you need to declare a
two-dimensional array of character types, where the first subscript is
the total number of strings and the second subscript is the maximum
size of each string.
• To initialize an array of strings, you need to provide the multiple
strings inside the double quotes separated by the commas.
• Syntax
• To construct an array of strings, the following syntax is used −
char strings [no_of_strings] [max_size_of_each_string];
Example
• Let us declare and initialize an array of strings to store the names of
10 computer languages, each with the maximum length of 15
characters.
char langs [10][15] = {
"PYTHON", "JA `0VASCRIPT", "PHP",
"NODE JS", "HTML", "KOTLIN", "C++",
"REACT JS", "RUST", "VBSCRIPT"
};
Printing An Array of Strings
• A string can be printed using the printf() function with %s format
specifier. To print each string of an array of strings, you can use the
for loop till the number of strings.
• Example
• In the following example, we are declaring, initializing, and printing an
array of string −
OUTPUT
PYTHON
#include <stdio.h> JAVASCRI
int main (){ PT
char langs [10][15] = { PHP
NODE
"PYTHON", "JAVASCRIPT", "PHP",
JS
"NODE JS", "HTML", "KOTLIN", HTML
"C++", "REACT JS", "RUST", KOTLIN
"VBSCRIPT" }; C++
for (int i = 0; i < 10; i++){ REACT
JS
printf("%s\n", langs[i]); }
RUST
return 0; }
VBSCRIP