Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
15 views20 pages

Arrays in C Language

Lecture 6 covers arrays in C, detailing their structure, operations, and memory management. It explains how to declare, initialize, and manipulate arrays, including passing them to functions and using pointers for efficient operations. The lecture also introduces dynamic array creation and the concept of array pointers.

Uploaded by

Muhammad Haroon
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)
15 views20 pages

Arrays in C Language

Lecture 6 covers arrays in C, detailing their structure, operations, and memory management. It explains how to declare, initialize, and manipulate arrays, including passing them to functions and using pointers for efficient operations. The lecture also introduces dynamic array creation and the concept of array pointers.

Uploaded by

Muhammad Haroon
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/ 20

Lecture 6 Arrays

1. Array and operations in C


2. Array operations by pointers

1
1. Array and operations
• The concepts of data structures
– A data structure defines how a collection of certain types of data
values are represented, organized, stored, and operated in
programs and computers.
– In (algorithms) programs, (abstract) data structures are used to
represent a collection of data items of certain types together
with a set of operations that can be performed on the data items.

• The simplest data structure is a list of variables


– The list of local variables of a function are instanced in contiguous
memory space in call stack when the function is called.
– The list of global variables are instanced in contiguous memory
space in the data region.
2
How to represent a large number of data items?
• Using a list of variables is doable when the number of
data items to be represented is fixed and small.

• Using a list of variables is not doable when the number


of data items is large.

• Array is a solution to solve this problem for the case


when the data items are of the same data type.
– Classify data items into groups, a group contains data
items of the same type
– Use array to represent data items of the group.

3
Concepts of arrays
1. An array is a collection of data values (elements) of the
same data type.
2. Array elements are organized in linear order and stored in
contiguous memory locations one after another.
3. The linear relation of array elements is determined by the
adjacency of elements in memory.

These allow us to access any array element directly by its


ordering position (index) in memory.

4
Array syntax
1. Array declaration syntax: data_type array_name[length];
data_type -- data type of array elements
array_name -- the name (identifier) of array
length -- total number of array elements
Tell compiler to allocate sizeof(data_type)*length bytes contiguous
memory cells (memory block) for the array.
2. Array elements: array_name[0] … array_name[length-1]
3. Array element at index i : array_name[i], for i = 0, …, length-1.

– Random accessing array element:


given index i, set or get value by array_name[i]
– Traversal array by index:
for (int i = 0; i<length; i++) process(array_name[i]);

5
Array Addresses
• An array element is a variable, e.g. array_name[0] is a
variable name, can be used set and get value, its address is
&array_name[0]

• The address of an array is the address of its first element,


represented by the array name, array_name.
– it is the address of the first element, i.e., &array_name[0],
– it is the address of the first memory cell of the block of array_name[0].

6
Example
int a[10]; // declares an array of int type, name a, length = 10
// allocates a memory block of 10*4 bytes
// access by a[index], index from 0 to 9, a[0], …, a[9]
// the smallest index 0, maximum index 9
sizeof(a) // gets total number of bytes of array a, i.e., 40 bytes

sizeof(a[0]) // gets the number of bytes of a[0], i.e. 4

sizeof(a)/sizeof(a[0]) // gets the length of array a

a[2] = 10; // assign/set value to array element a[2]


int b = a[2]; // get the value of array element at position/index 2, assign to b

a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7] a[8] a[9]

lower address high address

7
Array traversal example
#include <stdio.h>
#define SIZE 5
int list[ SIZE ]; // declare an array of int data as global variables
int main(){
int i;
for (i=0;i < SIZE; i++ ) {
list[ i ] = i + 1;
printf("list[%d] : %d, address : %lu\n",i, list[i], &list[i]);
} list[0] : 1, address : 4214888
return 0; list[1] : 2, address : 4214892
list[2] : 3, address : 4214896
} list[3] : 4, address : 4214900
list[4] : 5, address : 4214904
list is instanced in data region 8
#include <stdio.h>
#define SIZE 5
int main(){
int list[ SIZE ]; // declare an array of int data type, local variables
int i = 0;
for (i=0;i < SIZE; i++ ) {
list[ i ] = i + 1;
printf("list[%d] : %d, address : %lu\n", i, list[i], &list[i]);
}
return 0; list[0] : 1, address : 6422264
} list[1] : 2, address : 6422268
list[2] : 3, address : 6422272
list[3] : 4, address : 6422276
list[4] : 5, address : 6422280
list is instanced in stack region

9
Array initialization
int a[5]; // no initialization, the elements holds whatever it was there.

int a[5] = { 4, 1, 7, 2, 1 };
Tell compiler to generate instructions to set values to elements in
order, each element gets a value.

int a[5] = { 4, 1, 0 }; // first three elements get values 4, 1, 0, the rest 0

int a[5] = { 0}; // all elements are initialized to 0

int a[] ={1, 2, 3, 4, 5}; // declare length 5 and get the initial values

10
Passing arrays to Functions
Passing by array element values
void func(int num) void main()
{ {
printf("%d", num); int a[5] ={1, 2, 3, 4, 5};
} func(a[3]);
}

Passing by element reference (i.e., address of element)


void func(int *num) void main()
{ {
printf("%d", *num); int a[5] ={1, 2, 3, 4, 5};
} func(&a[3]);
}

Passing by array name, i.e., the address of the first element


together with the length of the array.
void func(int arr[], int n) void main()
{ {
int i; int a[5] ={1, 2, 3, 4, 5};
for(i=0;i<n;i++) func(a, 5);
printf("%d ", arr[i]); }
}
11
Example
#include<stdio.h>
void inc(int a[], int n) { Before increase
for(int i = 0; i < n; i ++ ) a[i]++; x[0] : 0
} x[1] : 1
x[2] : 2
After increase
int main( void ) {
x[0] : 1
int x[3]; x[1] : 2
for(int i = 0; i < 3; i++ ) x[i] = i; x[2] : 3
printf("Before increase\n");
for(int i = 0; i < 3; i++ ) printf("x[%i] : %i\n", i, x[i] );
inc(x, 3); // pass array name
printf("After increase\n");
for(int i = 0; i < 3; i++ ) printf("x[%i] : %i\n", i, x[i] );
return 0;
}

12
Creating arrays by dynamic method
• Arrays created by declaration method are statically or
automatically allocated arrays. Memory blocks of arrays are
allocated in the stack region if they are declared in functions, or
in data region if they declared outside all functions.

• Arrays can also be created by using the dynamic method, namely


using stdlib function malloc() function to allocate memory space
for an array. Arrays created by the dynamic method are called
dynamic arrays. Memory blocks of dynamic arrays are allocated in
the heap region at runtime.

13
Example a[0]:0, address:12326352
#include<stdio.h> a[1]:1, address:12326356
a[2]:2, address:12326360
#include<stdlib.h>
a[3]:3, address:12326364
int main() { a[4]:4, address:12326368
int length = 5;
a is in heap region
int *a = (int*) malloc(sizeof(int)*length);
// this allocates a memory block of 20 bytes in the heap region.
for (int i=0; i<length; i++) {
a[i] = i; // or *(a + i) = i;
printf("a[%d]:%d, address:%lu\n", i, a[i], &a[i]);
// or printf("a[%d]:%d, address:%lu\n", i, *(a+i), a+i);
}
free(a); // release the memory block of array pointe by a
return 0;
}

14
2. Array operations by pointers
• Array can be operated efficiently by using pointers.
Example: If pointer ptr holds the address of an array element, then
ptr++ represents the address of the next element, ptr-- represents
the address of the previous element.
int a[] = {1, 2, 3};
int *ptr = &a[0]; // or equivalently int *ptr = a;
printf (“%d”, *ptr); // output 1
ptr++; // equivalent to ptr = ptr+1;
printf (“%d”, *ptr); // output 2
ptr--;
printf (“%d”, *ptr); // output 1
15
Examples
int a[10];
int *p = &a[0];  int *p = a ;
*p = 1;  a[0] = 1;
(p+1)  &a[1]
(p+i)  & a[i]
*(p+i)  a[i]

16
The following three programs are equivalent
main(){
int i, a[5]; (1)
for (i = 0; i < 5; i++) a[i] = i;
for (i = 0; i < 5; i++) printf("%d ",a[i]);
}

main(){ (2)
int i, a[5];
for (i = 0; i < 5; i++) *(a+i) = 1;
for (i = 0; i < 5; i++) printf("%d ", *(a+i) );
}

main(){ (3)
int i, a[5];
(1), (2) and (3) do the same thing
int *p = a, *pend = a + 4; (1) and (2) are of the same
for (p = a; p <= pend; p++) *p = 1 ; efficiency
printf("\n"); (3) is the most efficient one, with
for (p = a; p <=pend; p++) printf("%d ",*p); less computation for index and
} addressing
17
void selection_sort(int x[], int n) {

Selection sort int i, j, k, t;


for (i = 0; i<n-1; i++){
k = i;
#include <stdio.h> for (j = i + 1; j < n; j++)
if (x[j] > x[k]) k = j;
Void sort(int *, int);
void sort_pointer(int *, int); if (k!=i) { // swap
t = x[i];
int main(){ x[i]=x[k];
int a[10] = {5, 9, 0, 8, 7, 4, 6, 3, 2, 1}; x[k] = t;
int i, *p = a; }
for (i = 0; i<10; i++) printf("%d ", *p++); }
printf("\n"); }
void selection_sort_pointer(int *x, int n) {
// selection_sort(a, 10);
selection_sort_pointer(a, 10); int i, j, k, t;
for (i = 0; i<n-1; i++){
for (p = a; p < a+10; p++) printf("%d ", *p); k = i;
printf("\n"); for (j = i + 1; j < n; j++)
return 0; if (*(x+j) > *(x+k)) k = j;
}
if (k!=i){ //swap
t = *(x+i);
*(x+i) = *(x+k);
Increasing order or decreasing order? *(x+k) = t;
}
}} 18
Pointer array (Array of pointers)
• C allows array of pointers, namely, each array element is a pointer.
This is a very useful feature for data representation.
Example:
int *ptr[5]; // declare an array of 5 int type pointer values (addresses of int data)
int p=1, q=2, r=3, s=4, t=5;
ptr[0]=&p;
ptr[1]=&q;
ptr[2]=&r;
ptr[3]=&s;
ptr[4]=&t;
printf(“\n %d”, *ptr[3]); // what it prints?
19
Array pointer
• An array pointer is a pointer pointing to an array.
– An array can be viewed as a derived data type. An array pointer is a pointer
pointing to such a data type variable.
– An array pointer can be declared by syntax: data_type (*arr_ptr_name)[k];
– Then arr_ptr_name is a pointer to an array of type data_type of length k.
–*arr_ptr_name represents a pointer pointing the first element of
the array
Example:
int a[5];
int (*ptr)[5]; // different from int *ptr[5];
int *p = a; // p points to a[0], p++ will increase by sizeof(int)
ptr = &a; // ptr and p has the same value, but ptr++ will increase by sizeof(int)*5.
*ptr points a[0], *ptr + i points to a[i] , *(*ptr + i) gives a[i]
20

You might also like