1. Chapter 1: Address, Pointers, Arrays, and Structures
Dr. Amna Ali A Mohamed
Faculty of information technology
2. Introduction
What Do We Mean by Data?
Data in programming functions like nouns, which are objects that store and
represent information.
These objects could be numbers, text, or complex structures like lists and
databases.
Actions on data are the verbs, representing operations that manipulate the data.
These include actions like:
• Adding new data (inserting elements into a list or database).
• Deleting unnecessary or outdated data.
• Reading data from a file or database.
• Writing data to a file or memory.
3. Introduction (Cont.)
Understanding Data Representation
Computers store data as sequences of bits (0s and 1s), but human-readable
formats are more abstract.
For instance:
• A single integer is stored as binary but is represented in decimal for
human understanding.
• A character is stored using encoding schemes like ASCII or Unicode but
appears as a readable letter.
• A list (array) is stored in contiguous memory locations but is displayed
as a structured collection of values.
4. 1. Data Abstraction
Data abstraction is the process of separating the physical storage of
data from how it is used in a program.
It allows programmers to work with high-level data structures (such as
arrays, linked lists, and trees) without needing to understand the
underlying memory details.
5. 1. Data Abstraction (Cont.)
Key Benefit of Data Abstraction
• Simplifies programming by allowing a focus on logic rather than
implementation details.
• Enables the design of scalable and modular programs.
• Supports the creation of reusable components that can work across different
hardware and software platforms.
Data Abstraction: Data abstraction hides the physical representation of data and
only exposes logical operations.
• Example: An integer is represented differently across computers, yet operations
remain the same.
6. 2. Data Structures
Definition: A structured way to store and organize data
efficiently.
Example:
A library organizes books logically for easy access.
Difference between Abstract Data Type (ADT) and Data
Structure:
o ADT defines the logical view.
o Data Structure provides an actual implementation.
7. 3. Overview of Data Structures
Data Structure Strengths Weaknesses
Arrays Fast access Fixed size
Linked Lists Dynamic size Slower access
Stacks Easy LIFO operations Limited access
Queues FIFO processing Limited access
Trees Hierarchical organization Complex implementation
Hash Tables Fast lookups Potential collisions
8. C Language
1. Address & Memory Allocation
Each variable has:
Value (data stored in memory).
Address (location in memory).
Explanation
Memory allocations to the variables can
be explained using the following
variables:
1. 100,i 10
2. 200, j 20
3. 300,k 30
9. 1. Address & Memory Allocation
Explanation
Memory allocations to the variables can be explained using the
following variables:
1. 100,i 10
2. 200, j 20
3. 300,k 30
Points to Remember
• Each variable has two attributes: address and value.
• The address is the location in memory where the value of the variable is stored.
• During the lifetime of the variable, the address is not changed but the value may change.
10. 2. Pointers
• A pointer is a variable whose value is also an address. As described earlier, each
variable has two attributes: address and value.
• A variable can take any value specified by its data type.
• For example, if the variable i is of the integer type, it can take any value permitted
in the range specified by the integer data type.
• A pointer to an integer is a variable that can store the address of that integer.
11. 2. Pointers (cont.)
• A pointer stores the address of another variable.
• Allows indirect access to a variable's value.
#include <stdio.h>
int main() {
int i = 10; // Declare integer variable
int *ia; // Declare pointer to integer I
a = &i; // Assign address of i to ia
printf("The address of i is %pn", ia); // Print address of I
printf("The value at that location is %dn", *ia); // Print value using pointer
*ia = 50; // Modify value using pointer
printf("The value of i is %dn", i); // Print updated value of i
return 0; }
12. 2. Pointers (cont.) Syntax:
Declare: int *ptr.
Assign address: ptr = &var.
Access value: *ptr (dereferencing).
#include <stdio.h>
int main() {
int i = 10; // Declare integer variable
int *ia; // Declare pointer to integer I
a = &i; // Assign address of i to ia
printf("The address of i is %pn",ia); // Print address of I
printf("The value at that location is %dn", *ia); // Print value using pointer
*ia = 50; // Modify value using pointer
printf("The value of i is %dn", i); // Print updated value of i
return 0; }
13. 3. Arrays
• An array stores multiple values of the same type.
• Elements are accessed using an index (starting from 0).
• Array name is a pointer constant pointing to the first element.
#include <stdio.h>
int main() {
int a[5]; // Declare an array of size 5
for(int i = 0; i < 5; i++) {
a[i] = i; // Assign values to array elements }
for(int i = 0; i < 5; i++) {
printf("value in array %dn", a[i]); // Print array elements
}
return 0; }
14. 3. Arrays
Points to Remember
1. An array is a composite data structure in which you can store multiple
values. Array elements are accessed using subscript.
2. The subscript operator has the highest precedence. Thus if you write
a[2]++,it increments the value at location 2 in the array.
3. The valid range of subscript is 0 to size −1.
15. Address of Array Elements
• Each array element has a memory address.
• Elements are stored in consecutive memory locations.
#include <stdio.h>
int main() {
int a[5];
for(int i = 0; i < 5; i++) {
a[i] = i; // Assign values to array
}
for(int i = 0; i < 5; i++) {
printf("value in array %d and address is %pn", a[i],
&a[i]);
}
return 0;
}
16. Accessing Arrays Using Pointers
• Arrays can be accessed using pointers.
• Pointer arithmetic moves the pointer to the next element.
#include <stdio.h>
int main() {
int a[5] = {10, 20, 30, 40, 50};
int *ptr = a; // Assign base address of array to pointer
for(int i = 0; i < 5; i++) {
printf("value in array %d and address is %pn", *ptr, ptr);
ptr=ptr+1; // Move pointer to next element
}
return 0;
17. Accessing Arrays Using Pointers
Points to Remember
1. Array elements can be accessed using pointers.
2. The array name is the pointer constant which can be assigned to any pointer variable.
#include <stdio.h>
int main() {
int a[5] = {10, 20, 30, 40, 50};
int *ptr = a; // Assign base address of array to pointer
for(int i = 0; i < 5; i++) {
printf("value in array %d and address is %pn", *ptr, ptr);
ptr=ptr+1; // Move pointer to next element
}
return 0;
18. Manipulating arrays using pointers
• Pointers can be used to manipulate arrays by incrementing or decrementing the pointer.
• Pointer arithmetic automatically adjusts the pointer based on the data type size.
#include <stdio.h>
int main() {
int a[5] = {10, 20, 30, 40, 50};
int *ptr = a; // Assign base address of array to pointer
for(int i = 0; i < 5; i++) {
printf("value in array %d and address is %pn", *ptr, ptr);
ptr++; // Move pointer to next element
}
return 0;
19. Manipulating arrays using pointers
Explanation:
• ptr++ moves the pointer to the next element in the array.
• The pointer is incremented by the size of the data type (e.g., 2 bytes for int).
#include <stdio.h>
int main() {
int a[5] = {10, 20, 30, 40, 50};
int *ptr = a; // Assign base address of array to pointer
for(int i = 0; i < 5; i++) {
printf("value in array %d and address is %pn", *ptr, ptr);
ptr++; // Move pointer to next element
}
return 0;
20. Two-Dimensional Arrays
• A two-dimensional array is an array of arrays.
• It is stored in row-major order (all elements of a row are stored consecutively).
• Elements are accessed using two indices: array[row][column].
#include <stdio.h>
int main() {
int a[3][2] = {{1, 2}, {3, 4}, {5, 6}}; // Declare and initialize 2D array
for(int i = 0; i < 3; i++) {
for(int j = 0; j < 2; j++) {
printf("a[%d][%d] = %d, Address = %pn", i, j, a[i][j],
&a[i][j]);
}
}
21. Two-Dimensional Arrays
#include <stdio.h>
int main() {
int a[3][2] = {{1, 2}, {3, 4}, {5, 6}}; // Declare and initialize 2D array
for(int i = 0; i < 3; i++) {
for(int j = 0; j < 2; j++) {
printf("a[%d][%d] = %d, Address = %pn", i, j, a[i][j],
&a[i][j]);
}
}
Explanation:
• The array a has 3 rows and 2 columns.
• Elements are accessed using a[i][j], where i is the row index and j is the column index.
• The addresses of elements are printed to show the memory layout (row-major order).
22. Two-Dimensional Arrays
#include <stdio.h>
int main() {
int a[3][2] = {{1, 2}, {3, 4}, {5, 6}}; // Declare and initialize 2D array
for(int i = 0; i < 3; i++) {
for(int j = 0; j < 2; j++) {
printf("a[%d][%d] = %d, Address = %pn", i, j, a[i][j],
&a[i][j]);
}
}
Explanation:
• The array a has 3 rows and 2 columns.
• Elements are accessed using a[i][j], where i is the row index and j is the column index.
• The addresses of elements are printed to show the memory layout (row-major order).
23. 1. What is the output of the following code?
Multiple Choice Questions
#include <stdio.h>
int main() {
int x = 10;
printf("Address of x: %p", &x);
return 0;
}
a) 10
b) Address of x
c) Compilation error
d) Runtime error
2. What should be filled in the blank to correctly assign the address of x to
the pointer ptr?
#include <stdio.h>
int main() {
int x = 10;
int *ptr; ptr = _____; // Missing part
printf("Value of x: %d", *ptr);
return 0; }
a) x
b) &x
c) *x
d) %x
24. 3. What is the output of the following code?
#include <stdio.h>
int main() {
int a[3] = {10, 20, 30};
int *ptr = a;
printf("%d", *(++ptr ));
return 0; }
a) 10
b) 20
c) 30
d) Compilation error
Multiple Choice Questions
Editor's Notes
#5:الميزة الرئيسية لتجريد البيانات
تبسيط البرمجة من خلال السماح بالتركيز على المنطق بدلاً من تفاصيل التنفيذ.
تمكين تصميم برامج قابلة للتطوير والتعديل.
دعم إنشاء مكونات قابلة لإعادة الاستخدام يمكنها العمل عبر منصات الأجهزة والبرامج المختلفة.
#9:When you declare variables i, j, k, memory is allocated for storing the values of the variables.
For example, 2 bytes are allocated for i, at location 100, 2 bytes are allocated for j at location 200, and 2 bytes allocated for k at location 300.
Here 100 is called the address of i, 200 is called address of j, and 300 is called the address of k.
When you execute the statement i = 10, the value 10 is written at location 100, which is specified in the figure.
Now, the address of i is 100 and the value is 10.
During the lifetime of variables, the address will remain fixed and the value may be changed.
Similarly, value 20 is written at address 200 for j.
During execution, addresses of the variables are taken according to the type of variable, that is, local or global.
Local variables usually have allocation in stack while global variables are stored in runtime storage.
#16:Try this:
ptr=ptr+2; // Move pointer to next element