Module - 5
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.
Example:
int a = 10;
* is used to declare the pointer and also to dereference (access the value at the
address).
int a = 5;
& Address-of
ptr = &x;
int main() {
struct myStructure s1;
return 0;
}
Access Structure Members
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;
s2.myNum = 20;
s2.myLetter = 'C';
What About Strings in Structures?
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 };
return 0;
}
C Unions
• 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() {
}; 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() {
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