0% found this document useful (0 votes)
2 views

C Programming Week11

The document provides an overview of multi-dimensional arrays in C programming, detailing their declaration, initialization, and memory layout. It explains how to access elements using both array notation and pointer arithmetic, as well as the differences between passing arguments by value and by reference. Additionally, it covers the concept of pointers, their usage with arrays, and includes a function for string copying using pointers.

Uploaded by

ABHISHEK GOUTAM
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 views

C Programming Week11

The document provides an overview of multi-dimensional arrays in C programming, detailing their declaration, initialization, and memory layout. It explains how to access elements using both array notation and pointer arithmetic, as well as the differences between passing arguments by value and by reference. Additionally, it covers the concept of pointers, their usage with arrays, and includes a function for string copying using pointers.

Uploaded by

ABHISHEK GOUTAM
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/ 4

30/10/17

Multi-dimensional Arrays
• char multi[4][10];
– multi[4] – an array of ten characters
– 4 such arrays
CS1100 multi[0] = {‘0’, ‘1’, ‘2’, ‘3’, ‘4’, ‘5’, ‘6’, ‘7’, ‘8’, ‘9’}
Introduction to Programming multi[1] = {‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’, ‘g’, ‘h’, ‘i’, ‘j’}
multi[2] = {‘A’, ‘B’, ‘C’, ‘D’, ‘E’, ‘F’, ‘G’, ‘H’, ‘I’, ‘J’}
Multi-Dimensional Arrays multi[3] = {‘9’, ‘8’, ‘7’, ‘6’, ‘5’, ‘4’, ‘3’, ‘2’, ‘1’, ‘0’}
• Individual elements are addressable:
Madhu Mutyam
Department of Computer Science and Engineering – multi[0][3] = ‘3’
Indian Institute of Technology Madras

Ted Jenson’s tutorial on pointers


Course Material – SD, SB, PSK, NSN, DK, TAG – CS&E, IIT M 1 http://pweb.netcom.com/~tjensen/ptr/cpoint.htm
2
SD, PSK, NSN, DK, TAG – CS&E, IIT M

Linear Contiguous Memory Address Pointers


multi[0] = {‘0’, ‘1’, ‘2’, ‘3’, ‘4’, ‘5’, ‘6’, ‘7’, ‘8’, ‘9’} multi[0] = {‘0’, ‘1’, ‘2’, ‘3’, ‘4’, ‘5’, ‘6’, ‘7’, ‘8’, ‘9’}
multi[1] = {‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’, ‘g’, ‘h’, ‘i’, ‘j’} multi[1] = {‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’, ‘g’, ‘h’, ‘i’, ‘j’}
multi[2] = {‘A’,‘B’,‘C’, ‘D’, ‘E’, ‘F’, ‘G’, ‘H’, ‘I’, ‘J’} multi[2] = {‘A’,‘B’,‘C’, ‘D’, ‘E’, ‘F’, ‘G’, ‘H’, ‘I’, ‘J’}
multi[3] = {‘9’, ‘8’, ‘7’, ‘6’, ‘5’, ‘4’, ‘3’, ‘2’, ‘1’, ‘0’} multi[3] = {‘9’, ‘8’, ‘7’, ‘6’, ‘5’, ‘4’, ‘3’, ‘2’, ‘1’, ‘0’}
• The data is stored in the memory as • multi is the address of location with ‘0’
0123456789abcdefghijABCDEFGHIJ9876543210 • multi+1 is the address of ‘a’
– It adds 10, the number of columns, to get this location. If we
were dealing with integers and an array with the same
starting at the address &multi[0][0] dimension the compiler would add 10*sizeof(int)
• To get to the content of the 2nd element in the 4th row we
need to use *(*(multi + 3) + 1)
SD, PSK, NSN, DK, TAG – CS&E, IIT M 3 SD, PSK, NSN, DK, TAG – CS&E, IIT M 4

Address Computation multi[row][col] ≡ *(*(multi + row) + col)


• With a little thought we can see that: • To understand more fully what is going on, let us
*(*(multi + row) + col) and replace
multi[row][col] *(multi + row) with X
yield the same results
i.e., *(*(multi+row)+col) = *(X + col)
• Because of the double de-referencing required in
the pointer version, the name of a 2-dimensional • X is like a pointer since the expression is de-
array is often said to be equivalent to a pointer to referenced and col is an integer
a pointer • Here “pointer arithmetic” is used
• That is, the address pointed to (i.e., value of)
X + col = X + col * sizeof(int)
SD, PSK, NSN, DK, TAG – CS&E, IIT M 5 SD, PSK, NSN, DK, TAG – CS&E, IIT M 6

1
30/10/17

multi[row][col] ≡ *(*(multi + row) + col) multi[row][col] ≡ *(*(multi + row) + col)


• Since we know the memory layout for 2 dimensional • Thus, to evaluate either expression, a total of 5
arrays, we can determine that in the expression (multi + values must be known:
row) as used above, (multi + row + 1) must increase by
– Address of the first element of the array, which is
an amount equal to that needed to "point to" the next
returned by the expression multi, i.e., name of the array
row, which for integers would be an amount equal to
COLS * sizeof(int) – The size of the type of the elements of the array, in this
case, sizeof(int)
• That says that if the expression *(*(multi + row) + col)
is to be evaluated correctly at run time, the compiler – The 2nd dimension of the array
must generate code which takes into consideration the – The specific index value for the first dimension, row in
value of COLS, i.e., the 2nd dimension this case
– The specific index value for the second dimension, col
remember passing arrays as parameters?
in this case
SD, PSK, NSN, DK, TAG – CS&E, IIT M 7 SD, PSK, NSN, DK, TAG – CS&E, IIT M Ted Jenson’s tutorial on pointers 8
http://pweb.netcom.com/~tjensen/ptr/cpoint.htm

multi[row][col] ≡ *(*(multi + row) + col) Array multi[4][10]


• Question:
– When we say value = *ptr; the pointer ptr is de-
referenced to get the data stored …0123456789abcdefghijABCDEFGHIJ9876543210…

– What happens in *(*(array + row) + column)?


– Why is *(array + row) not de-referenced to give, say,
an integer? multi multi +1 multi +2 multi +3

• Answer:
– It is de-referenced
Address(multi+1) = address(multi) + 10
– Remember a 2-D array is a pointer to a pointer Because the character array has 10 columns
– *(array + row) de-references to a pointer to a 1-D array Each of these is a pointer to a pointer (or pointer to a 1-D array)
– *(array + row) + 1 would do a pointer increment
SD, PSK, NSN, DK, TAG – CS&E, IIT M 9 SD, PSK, NSN, DK, TAG – CS&E, IIT M 10

Revisiting Functions on Arrays Recap


• Initializing a 2-dimensional array • Arrays
• Functions
void set_value(int m_array[][COLS])
{ • Sorting
int row, col;
for (row = 0; row < ROWS; row++)
• Pointers
{ • Strings
for (col = 0; col < COLS; col++)
{
m_array[row][col] = 1;
}
}
}

SD, PSK, NSN, DK, TAG – CS&E, IIT M 11 SD, PSK, NSN, DK, TAG – CS&E, IIT M 12

2
30/10/17

Arrays Multi-Dimensional Arrays


• A data structure containing items of same data type • Arrays with two or more dimensions can be
• Declaration: array name, storage reservation defined
– int marks[7] = {22,15,75,56,10,33,45}; 22 0 A[4][3] B[2][4][3]
• a contiguous group of memory locations 0 1 2 0 1 2 0 1 2
15 1
• named “marks” for holding 7 integer items
75 2 0 0
– elements/components - variables
• marks[0], marks[1], … , marks[6] 56 3 1 1

• marks[i], where i is a position/subscript (0≤i≤6) 10 4


2 2
– the value of marks[2] is 75 33 5
– new values can be assigned to elements 3 3
45 6
• marks[3] = 36; 0 1
SD, PSK, NSN, DK, TAG – CS&E, IIT M 13 SD, PSK, NSN, DK, TAG – CS&E, IIT M 14

Two Dimensional Arrays Functions


• Declaration: int A[4][3] : 4 rows and • Break large computing tasks into small ones
3 columns, 4×3 array • Transfer of control is affected by calling a function
• Elements: A[i][j] - element in row i A[4][3] – With a function call, we pass some parameters
and column j of array A 0 1 2 – These parameters are used within the function
• Rows/columns numbered from 0 0 – A value is computed
• Storage: row-major ordering – The value is returned to the function which initiated the
1
– elements of row 0, elements of row 1, call
etc 2 – A function could call itself, these are called recursive
function calls
• Initialization: 3
int B[2][3]={{4,5,6},{0,3,5}};
• Function prototype, function definition, and
SD, PSK, NSN, DK, TAG – CS&E, IIT M 15 function call
SD, PSK, NSN, DK, TAG – CS&E, IIT M 16

Passing Arguments to Functions Selection Vs Insertion Sort


• In C, function arguments are passed “by value” • Scanning from left to right
– values of the arguments given to the called function
in temporary variables rather than the originals
• Selection sort i
– the modifications to the parameter variables do not
– Swaps the ith element with the largest unsorted element
affect the variables in the calling function
• “Call by reference”
• Insertion sort i
– variables are passed by reference
• subject to modification by the function – Inserts the ith element into its proper place
– achieved by passing the “address of” variables

SD, PSK, NSN, DK, TAG – CS&E, IIT M 17 SD, PSK, NSN, DK, TAG – CS&E, IIT M 18

3
30/10/17

What is a Pointer? l-value and r-value


• Recap: a variable int k Addr • Given a variable k
– Names a memory location that can k 38 100 – Its l-value refers to the address of the memory location
hold one value at a time – l-value is used on the left side of an assignment
– Memory is allocated statically at *p • Ex. k = expression
compile time – Its r-value refers to the value stored in the memory
– One name ⇔ one value location
• A pointer variable int *p p 250
100 200 – r-value is used in the right hand side of an assignment
– Contains the address of a memory • Ex. var = k + …
*p
location that contains the actual value • Pointers allow one to manipulate the l-value!
m 84 250
– Memory can be allocated at runtime
– One name ⇔ many values
SD, PSK, NSN, DK, TAG – CS&E, IIT M 19 SD, PSK, NSN, DK, TAG – CS&E, IIT M 20

Accessing Arrays Using Pointers Strings


• The name of the array is the address of the first • A string is a array of characters terminated by the
element in the array null character, ‘\0’
• In C, we can replace • A string is written in double quotes
ptr = &myArray[0]; – Example: “This is a string”
with • “ ” – empty string
ptr = myArray; • Anything within single quotes gets a number
to achieve the same result associated with it
• ‘This is rejected by the C Compiler’
• Character arrays can also be accessed by pointers
SD, PSK, NSN, DK, TAG – CS&E, IIT M 21 SD, PSK, NSN, DK, TAG – CS&E, IIT M 22

Strcpy Using Pointers


char *myStrcpy(char *destination, char *source)
{
char *p = destination;
while (*source != '\0')
{
*p++ = *source++;
}
*p = '\0';
return destination;
}
Ted Jenson’s tutorial on pointers
http://pweb.netcom.com/~tjensen/ptr/cpoint.htm

SD, PSK, NSN, DK, TAG – CS&E, IIT M 23

You might also like