0% found this document useful (0 votes)
2 views24 pages

Data Struct 1

This document introduces fundamental concepts in programming related to data, including data abstraction, data structures, addresses, pointers, and arrays. It explains how data is represented in computers, the benefits of data abstraction, and the characteristics of various data structures like arrays, linked lists, and trees. Additionally, it covers memory allocation, pointer usage, and accessing and manipulating arrays using pointers, along with examples and multiple-choice questions for assessment.
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)
2 views24 pages

Data Struct 1

This document introduces fundamental concepts in programming related to data, including data abstraction, data structures, addresses, pointers, and arrays. It explains how data is represented in computers, the benefits of data abstraction, and the characteristics of various data structures like arrays, linked lists, and trees. Additionally, it covers memory allocation, pointer usage, and accessing and manipulating arrays using pointers, along with examples and multiple-choice questions for assessment.
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

Chapter 1: Address, Pointers, Arrays, and Structures

Dr. Amna Ali A Mohamed


Faculty of information technology
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.
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.
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.
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.
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.
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


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
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.
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.
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
ia = &i; // Assign address of i to ia
printf("The address of i is %p\n", ia); // Print address of Ia
printf("The value at that location is %d\n", *ia); // Print value using pointer
*ia = 50; // Modify value using pointer
printf("The value of i is %d\n", i); // Print updated value of i
return 0; }
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 %p\n",ia); // Print address of I
printf("The value at that location is %d\n", *ia); // Print value using pointer
*ia = 50; // Modify value using pointer
printf("The value of i is %d\n", i); // Print updated value of i
return 0; }
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 %d\n", a[i]); // Print array elements
}
return 0; }
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.
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 %p\n", a[i], &a[i]);
}
return 0;
}
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 %p\n", *ptr, ptr);
ptr=ptr+1; // Move pointer to next element
}
return 0;
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 %p\n", *ptr, ptr);
ptr=ptr+1; // Move pointer to next element
}
return 0;
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 %p\n", *ptr, ptr);
ptr++; // Move pointer to next element
}
return 0;
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 %p\n", *ptr, ptr);
ptr++; // Move pointer to next element
}
return 0;
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 = %p\n", i, j, a[i][j], &a[i][j]);
}
}
return 0; }
Two-Dimensional Arrays
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).
#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 = %p\n", i, j, a[i][j], &a[i][j]);
}
}
return 0; }
Two-Dimensional Arrays
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).
#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 = %p\n", i, j, a[i][j], &a[i][j]);
}
}
return 0; }
Multiple Choice Questions
1. What is the output of the following code?
#include <stdio.h>
int main() { a) 10
int x = 10; b) Address of x
printf("Address of x: %p", &x);
c) Compilation error
return 0;
}
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() { a) x
int x = 10; b) &x
int *ptr; ptr = _____; // Missing part c) *x
printf("Value of x: %d", *ptr); d) %x
return 0; }
Multiple Choice Questions
3. What is the output of the following code?
#include <stdio.h>
int main() { a) 10
int a[3] = {10, 20, 30}; b) 20
int *ptr = a; c) 30
printf("%d", *(++ptr )); d) Compilation error
return 0; }

You might also like