Pointers, Arrays, Multidimensional - Michigan State …cse251/lecture11.pdf · Pointers, Arrays,...
Transcript of Pointers, Arrays, Multidimensional - Michigan State …cse251/lecture11.pdf · Pointers, Arrays,...
![Page 1: Pointers, Arrays, Multidimensional - Michigan State …cse251/lecture11.pdf · Pointers, Arrays, Multidimensional Arrays ... 1 Programming in C. Review: Pointers ... pointer to the](https://reader031.fdocuments.us/reader031/viewer/2022020114/5af3e9227f8b9a190c8c5061/html5/thumbnails/1.jpg)
Pointers, Arrays, Multidimensional Arrays
• Pointers versus arrays– Lots of similarities
• How to deal with 2D, 3D, multidimensional arrays (for storing matrices and other 2D or 3D data!)(for storing matrices and other 2D or 3D data!)
CSE 251 Dr. Charles B. OwenProgramming in C1
![Page 2: Pointers, Arrays, Multidimensional - Michigan State …cse251/lecture11.pdf · Pointers, Arrays, Multidimensional Arrays ... 1 Programming in C. Review: Pointers ... pointer to the](https://reader031.fdocuments.us/reader031/viewer/2022020114/5af3e9227f8b9a190c8c5061/html5/thumbnails/2.jpg)
Review: PointersAddress Memory Name
Pointers are variables that store memory addresses
Address Memory
0xeffffa94
Name
a15
int a = 15;
0xeffffa98 b0xeffffa94
int a 15;int *b = &a;
printf(“%x %x %d\n”, b, &b, *b);// prints effffa94 effffa98 15// prints effffa94 effffa98 15
CSE 251 Dr. Charles B. OwenProgramming in C2
![Page 3: Pointers, Arrays, Multidimensional - Michigan State …cse251/lecture11.pdf · Pointers, Arrays, Multidimensional Arrays ... 1 Programming in C. Review: Pointers ... pointer to the](https://reader031.fdocuments.us/reader031/viewer/2022020114/5af3e9227f8b9a190c8c5061/html5/thumbnails/3.jpg)
int number; int *ptr = &number;
Using pointers in scanf function
Read & as “at”
printf(“Enter an integer: ”);scanf(“%d” &number);
scanf functionas at
scanf( %d , &number);printf(“Enter another integer: “);scanf(“%d”, ptr);
Don’t have to put & before ptr. It’s already an “at”
printf(“Number = %d, *ptr = %d\n”, number, *ptr);
already an at .
Example output:
CSE 251 Dr. Charles B. OwenProgramming in C3
![Page 4: Pointers, Arrays, Multidimensional - Michigan State …cse251/lecture11.pdf · Pointers, Arrays, Multidimensional Arrays ... 1 Programming in C. Review: Pointers ... pointer to the](https://reader031.fdocuments.us/reader031/viewer/2022020114/5af3e9227f8b9a190c8c5061/html5/thumbnails/4.jpg)
int multiply( int *, int);
int main()
Passing pointers (addresses) toint main()
{int number = 3;int *ptr &n mber;
(addresses) to functions
int *ptr = &number;printf(“1: %d\n”, multiply( &number, 2 ) );printf(“2: %d\n”, multiply( ptr, 3 ) );
}}
int multiply (int *a, int factor) {{
return (*a) * factor;}
CSE 251 Dr. Charles B. OwenProgramming in C4
![Page 5: Pointers, Arrays, Multidimensional - Michigan State …cse251/lecture11.pdf · Pointers, Arrays, Multidimensional Arrays ... 1 Programming in C. Review: Pointers ... pointer to the](https://reader031.fdocuments.us/reader031/viewer/2022020114/5af3e9227f8b9a190c8c5061/html5/thumbnails/5.jpg)
Review: Arrays
An array is a contiguous chunk of memory to store multiple values
int grades[]={74,59,95,85,71,45,99,82,76};
0xeffffa00 0xeffffa04 0xeffffa08 0xeffffa0c 0xeffffa10 0xeffffa14 0xeffffa18 0xeffffa1c 0xeffffa20
grades 74 59 95 85 71 45 99 82 76
index 0 1 2 3 4 5 6 7 8
CSE 251 Dr. Charles B. OwenProgramming in C5 A
![Page 6: Pointers, Arrays, Multidimensional - Michigan State …cse251/lecture11.pdf · Pointers, Arrays, Multidimensional Arrays ... 1 Programming in C. Review: Pointers ... pointer to the](https://reader031.fdocuments.us/reader031/viewer/2022020114/5af3e9227f8b9a190c8c5061/html5/thumbnails/6.jpg)
int sumArray( int [], int);
int main() Passing arrays to functions{
int list[] = {1, 2, 3, 4};printf(“Sum = %d\n”, sumArray( list , 4 ));
functions
}
int sumArray (int list[], int arraySize) {{
int sumvalue = 0;for (int i=0; i<arraySize; i++)
sumvalue += list[i];sumvalue += list[i];return sumvalue;
}
CSE 251 Dr. Charles B. OwenProgramming in C6
![Page 7: Pointers, Arrays, Multidimensional - Michigan State …cse251/lecture11.pdf · Pointers, Arrays, Multidimensional Arrays ... 1 Programming in C. Review: Pointers ... pointer to the](https://reader031.fdocuments.us/reader031/viewer/2022020114/5af3e9227f8b9a190c8c5061/html5/thumbnails/7.jpg)
Array Nameffe2de0c ffe2de10 ffe2de14 ffe2de18The array name is a
pointer to the first l t f th
1 2 3 4
[1] [2] [3][0]
listffe2de0c ffe2de10 ffe2de14 ffe2de18
element of the array [1] [2] [3][0]
int list[]={1,2,3,4};
Output: ffe2de0c ffe2de0c 1
int list[] {1,2,3,4};printf(“%x, %x, %d”, list, &list[0], *list);
Output: ffe2de0c ffe2de0c 1
CSE 251 Dr. Charles B. OwenProgramming in C7
![Page 8: Pointers, Arrays, Multidimensional - Michigan State …cse251/lecture11.pdf · Pointers, Arrays, Multidimensional Arrays ... 1 Programming in C. Review: Pointers ... pointer to the](https://reader031.fdocuments.us/reader031/viewer/2022020114/5af3e9227f8b9a190c8c5061/html5/thumbnails/8.jpg)
Pointers and Arrays p
int *p, int list[] {1 2 3 4};
1 2 3 4
[1] [2] [3][0]
list
int list[]={1,2,3,4}; p = list; /* equivalent to p = &list[0] */printf(“%d\n”, *p); /* prints the value “1” */
[ ] [ ] [ ][ ]
YYou can use apointer to access
the array
CSE 251 Dr. Charles B. OwenProgramming in C8
![Page 9: Pointers, Arrays, Multidimensional - Michigan State …cse251/lecture11.pdf · Pointers, Arrays, Multidimensional Arrays ... 1 Programming in C. Review: Pointers ... pointer to the](https://reader031.fdocuments.us/reader031/viewer/2022020114/5af3e9227f8b9a190c8c5061/html5/thumbnails/9.jpg)
Pointer and [] P
Any pointer to a block of memory can use the [] syntax, even if it is
not declared as an array! 1 2 3 4Listnot declared as an array!
[1] [2] [3][0]
int *p, pint list[]={1,2,3,4}; p = list;i tf(“%d\ ” [2]) // i t 3
int *v; and int v[]; /* Mean the same thing */
printf(“%d\n”, p[2]); // prints 3
CSE 251 Dr. Charles B. OwenProgramming in C9
![Page 10: Pointers, Arrays, Multidimensional - Michigan State …cse251/lecture11.pdf · Pointers, Arrays, Multidimensional Arrays ... 1 Programming in C. Review: Pointers ... pointer to the](https://reader031.fdocuments.us/reader031/viewer/2022020114/5af3e9227f8b9a190c8c5061/html5/thumbnails/10.jpg)
Array indexing [] *list – Contents pointed to by list*(list + 2) – Contents at list[2]
Indexing an array is just a way
list
ff 2d 0 ff 2d 10 ff 2d 14 ff 2d 18
list + 2
of finding a particular address in that block1 2 3 4
[1] [2] [3][0]
ffe2de0c ffe2de10 ffe2de14 ffe2de18
[1] [2] [3][0]
int list[] = {1 2 3 4} // array of 4 intsint list[] = {1,2,3,4} // array of 4 intsprintf(“%d”, list[2]);
This is equivalent toprintf(“%d”,*(list+2));printf( %d , (list+2));
CSE 251 Dr. Charles B. OwenProgramming in C10 B
![Page 11: Pointers, Arrays, Multidimensional - Michigan State …cse251/lecture11.pdf · Pointers, Arrays, Multidimensional Arrays ... 1 Programming in C. Review: Pointers ... pointer to the](https://reader031.fdocuments.us/reader031/viewer/2022020114/5af3e9227f8b9a190c8c5061/html5/thumbnails/11.jpg)
Pointer Arithmetic
When we add to a pointer, such as (p + 1), we don’t literally add 1 to the pointer address
Instead we add one “address” to the pointer
CSE 251 Dr. Charles B. OwenProgramming in C11
![Page 12: Pointers, Arrays, Multidimensional - Michigan State …cse251/lecture11.pdf · Pointers, Arrays, Multidimensional Arrays ... 1 Programming in C. Review: Pointers ... pointer to the](https://reader031.fdocuments.us/reader031/viewer/2022020114/5af3e9227f8b9a190c8c5061/html5/thumbnails/12.jpg)
Pointer Arithmeticint list[] = {1, 2, 3, 4};int *p = list; /* same as p = &list[0] */printf(“%x”,p); /* prints ffe2de0c */
p
printf( %x ,p); / prints ffe2de0c /
ffe2de0c ffe2de10 ffe2de14 ffe2de18
1 2 3 4
CSE 251 Dr. Charles B. OwenProgramming in C12
![Page 13: Pointers, Arrays, Multidimensional - Michigan State …cse251/lecture11.pdf · Pointers, Arrays, Multidimensional Arrays ... 1 Programming in C. Review: Pointers ... pointer to the](https://reader031.fdocuments.us/reader031/viewer/2022020114/5af3e9227f8b9a190c8c5061/html5/thumbnails/13.jpg)
Pointer Arithmeticint list[] = {1, 2, 3, 4};int *p = list; /* same as p = &list[0] */printf(“%x”,p); /* prints ffe2de0c */printf( %x ,p); / prints ffe2de0c /p = p + 1; /* p increases by 4 */printf(“%x”,p); /* prints ffe2de10 */
pThink of pointer arithmetic as add
1 2 3 4
ffe2de0c ffe2de10 ffe2de14 ffe2de181 “location” instead of one byte or addressbyte or address.
CSE 251 Dr. Charles B. OwenProgramming in C13
![Page 14: Pointers, Arrays, Multidimensional - Michigan State …cse251/lecture11.pdf · Pointers, Arrays, Multidimensional Arrays ... 1 Programming in C. Review: Pointers ... pointer to the](https://reader031.fdocuments.us/reader031/viewer/2022020114/5af3e9227f8b9a190c8c5061/html5/thumbnails/14.jpg)
Pointer Arithmeticd bl li t2[] {1 0 2 0 3 0}double list2[] = {1.0, 2.0, 3.0};double *p = list2; /* same as p = &list2[0] */printf(“%x”, p); /* prints ffe2de0c */
p
p ( p) p
1.0
ffe2de0c ffe2de10 ffe2de14 ffe2de18
2.0 3.0
ffe2de1c ffe2de20
1.0 2.0 3.0
CSE 251 Dr. Charles B. OwenProgramming in C14 C
![Page 15: Pointers, Arrays, Multidimensional - Michigan State …cse251/lecture11.pdf · Pointers, Arrays, Multidimensional Arrays ... 1 Programming in C. Review: Pointers ... pointer to the](https://reader031.fdocuments.us/reader031/viewer/2022020114/5af3e9227f8b9a190c8c5061/html5/thumbnails/15.jpg)
Pointer Arithmeticd bl li t2[] {1 0 2 0 3 0}double list2[] = {1.0, 2.0, 3.0};double *p = list2; /* same as p = &list2[0] */printf(“%x”,p); /* prints ffe2de0c */p ( p) pp = p + 1; /* P increases by 8 bytes */printf(“%x”,p); /* prints ffe2de14 */
P
1.0
ffe2de0c ffe2de10 ffe2de14 ffe2de18
2.0 3.0
ffe2de1c ffe2de20
CSE 251 Dr. Charles B. OwenProgramming in C15
![Page 16: Pointers, Arrays, Multidimensional - Michigan State …cse251/lecture11.pdf · Pointers, Arrays, Multidimensional Arrays ... 1 Programming in C. Review: Pointers ... pointer to the](https://reader031.fdocuments.us/reader031/viewer/2022020114/5af3e9227f8b9a190c8c5061/html5/thumbnails/16.jpg)
Pointer Arithmetic on Arrays*(l ) f h l h• *(list+1) references the next element in the array (equivalent to list[1])
• Be careful: *(++list) works too but now we have lost our pointer to the beginning of the array!!!our pointer to the beginning of the array!!!– Equivalent to: list = list + 1; *list;
CSE 251 Dr. Charles B. OwenProgramming in C16
![Page 17: Pointers, Arrays, Multidimensional - Michigan State …cse251/lecture11.pdf · Pointers, Arrays, Multidimensional Arrays ... 1 Programming in C. Review: Pointers ... pointer to the](https://reader031.fdocuments.us/reader031/viewer/2022020114/5af3e9227f8b9a190c8c5061/html5/thumbnails/17.jpg)
sizeof() operatori t i
Returns the number of bytes needed toint i;
int *ptr4i = &i;int IntArray[] = {1, 2, 3, 4, 5};double j;
of bytes needed to store a variable or a data typedouble j;
double *ptr4j = &j;double doubleArray[] = {1.0, 2.0, 3.0, 4.0,5.0};
i tf("Si f i t i %d b t \ " i f(i t))
data type
printf("Sizeof integer is %d bytes\n", sizeof(int));printf("Sizeof double is %d bytes\n", sizeof(double));
printf("Sizeof i is %d bytes\n", sizeof(i));printf("Sizeof pointer for i is %d bytes\n", sizeof(ptr4i));
printf("Sizeof j is %d bytes\n", sizeof(j));printf("Sizeof pointer for j is %d bytes\n", sizeof(ptr4j));
printf("Sizeof intArray is %d bytes\n", sizeof(intArray));printf("Sizeof doubleArray is %d bytes\n", sizeof(doubleArray));
CSE 251 Dr. Charles B. OwenProgramming in C17
![Page 18: Pointers, Arrays, Multidimensional - Michigan State …cse251/lecture11.pdf · Pointers, Arrays, Multidimensional Arrays ... 1 Programming in C. Review: Pointers ... pointer to the](https://reader031.fdocuments.us/reader031/viewer/2022020114/5af3e9227f8b9a190c8c5061/html5/thumbnails/18.jpg)
sizeof() operator
CSE 251 Dr. Charles B. OwenProgramming in C18
![Page 19: Pointers, Arrays, Multidimensional - Michigan State …cse251/lecture11.pdf · Pointers, Arrays, Multidimensional Arrays ... 1 Programming in C. Review: Pointers ... pointer to the](https://reader031.fdocuments.us/reader031/viewer/2022020114/5af3e9227f8b9a190c8c5061/html5/thumbnails/19.jpg)
When we pass an arrayWh i thWhen we pass an array, we are passing the array address
int sumArray( int [ ] int);int sumArray( int [ ], int);
int main() {{int list[] = {1, 2,3, 4); …
A ( li 4 )sumArray( list , 4 );
…
CSE 251 Dr. Charles B. OwenProgramming in C19
![Page 20: Pointers, Arrays, Multidimensional - Michigan State …cse251/lecture11.pdf · Pointers, Arrays, Multidimensional Arrays ... 1 Programming in C. Review: Pointers ... pointer to the](https://reader031.fdocuments.us/reader031/viewer/2022020114/5af3e9227f8b9a190c8c5061/html5/thumbnails/20.jpg)
When we pass an array
This will work too (because array name is a pointer to the beginning of the array)
int sumArray( int *, int);int main() {{int list[] = {1, 2,3, 4); …sumArray( list , 4 );
…
CSE 251 Dr. Charles B. OwenProgramming in C20
![Page 21: Pointers, Arrays, Multidimensional - Michigan State …cse251/lecture11.pdf · Pointers, Arrays, Multidimensional Arrays ... 1 Programming in C. Review: Pointers ... pointer to the](https://reader031.fdocuments.us/reader031/viewer/2022020114/5af3e9227f8b9a190c8c5061/html5/thumbnails/21.jpg)
When we pass an array
• But this DOES NOT work!
int sumArray( int *, int);y( , );int main() {int *list {1 2 3 4);int *list = {1, 2, 3, 4); …sumArray( list , 4 );
…
CSE 251 Dr. Charles B. OwenProgramming in C21
![Page 22: Pointers, Arrays, Multidimensional - Michigan State …cse251/lecture11.pdf · Pointers, Arrays, Multidimensional Arrays ... 1 Programming in C. Review: Pointers ... pointer to the](https://reader031.fdocuments.us/reader031/viewer/2022020114/5af3e9227f8b9a190c8c5061/html5/thumbnails/22.jpg)
Pointers *list – Contents pointed to by list*(list + 2) – Contents at list[2]
Indexing an array is just a
list
ff 2d 0 ff 2d 10 ff 2d 14 ff 2d 18
list + 2
way of finding a particular address in that block1 2 3 4
[1] [2] [3][0]
ffe2de0c ffe2de10 ffe2de14 ffe2de18
[1] [2] [3][0]
int list[] = {1 2 3 4} // array of 4 intsint list[] = {1,2,3,4} // array of 4 intsprintf(“%d”, list[2]);
This is equivalent toprintf(“%d”,*(list+2));printf( %d , (list+2));
CSE 251 Dr. Charles B. OwenProgramming in C22 1
![Page 23: Pointers, Arrays, Multidimensional - Michigan State …cse251/lecture11.pdf · Pointers, Arrays, Multidimensional Arrays ... 1 Programming in C. Review: Pointers ... pointer to the](https://reader031.fdocuments.us/reader031/viewer/2022020114/5af3e9227f8b9a190c8c5061/html5/thumbnails/23.jpg)
2‐D Arrays
int cave[ArraySize][ArraySize];
Column
1 2 3 40
0 1 2 3
Column
1 2 3 45 6 7 89 10 11 12
0
1
2Row
9 10 11 1213 14 15 16
2
3
CSE 251 Dr. Charles B. OwenProgramming in C23
![Page 24: Pointers, Arrays, Multidimensional - Michigan State …cse251/lecture11.pdf · Pointers, Arrays, Multidimensional Arrays ... 1 Programming in C. Review: Pointers ... pointer to the](https://reader031.fdocuments.us/reader031/viewer/2022020114/5af3e9227f8b9a190c8c5061/html5/thumbnails/24.jpg)
2D Arraysint myMatrix[3][4] = { {1 2 3 4} {5 6 7 8} {9 10 11 12} };int myMatrix[3][4] = { {1,2,3,4},{5,6,7,8},{9,10,11,12} };
0 1 2 3
Column
1 2 3 45 6 7 8
myMatrix[0][1] → 2
0 1 2 3
0
1Row 5 6 7 89 10 11 12
M t i [2][3] 12
2
Row
myMatrix[2][3] → 12myMatrix[row][col]
CSE 251 Dr. Charles B. OwenProgramming in C24
![Page 25: Pointers, Arrays, Multidimensional - Michigan State …cse251/lecture11.pdf · Pointers, Arrays, Multidimensional Arrays ... 1 Programming in C. Review: Pointers ... pointer to the](https://reader031.fdocuments.us/reader031/viewer/2022020114/5af3e9227f8b9a190c8c5061/html5/thumbnails/25.jpg)
Physically, in one block of memoryint myMatrix[2][4] = { {1,2,3,4},{5,6,7,8} };
ffe2de0c ffe2de10 ffe2de14 ffe2de18 ffe2de1c ffe2de20 ffe2de24 ffe2de28
1 2 3 4 5 6 7 8ffe2de0c ffe2de10 ffe2de14 ffe2de18 ffe2de1c ffe2de20 ffe2de24 ffe2de28
Array elements are stored in row major order
row 1 row 2
Array elements are stored in row major orderRow 1 first, followed by row2, row3, and so on
CSE 251 Dr. Charles B. OwenProgramming in C25
![Page 26: Pointers, Arrays, Multidimensional - Michigan State …cse251/lecture11.pdf · Pointers, Arrays, Multidimensional Arrays ... 1 Programming in C. Review: Pointers ... pointer to the](https://reader031.fdocuments.us/reader031/viewer/2022020114/5af3e9227f8b9a190c8c5061/html5/thumbnails/26.jpg)
2D Array Name and Addresses[ ][ ] { { } { } }
ffe2de0c ffe2de10 ffe2de14 ffe2de18 ffe2de1c ffe2de20 ffe2de24 ffe2de28
int myMatrix[2][4] = { {1,2,3,4},{5,6,7,8} };
1 2 3 4 5 6 7 8ffe2de0c ffe2de10 ffe2de14 ffe2de18 ffe2de1c ffe2de20 ffe2de24 ffe2de28
myMatrix: pointer to the first element of the 2D arraymyMatrix[0]: pointer to the first row of the 2D arraymyMatrix[1]: pointer to the second row of the 2D array*myMatrix[1] is the address of element myMatrix[1][0]
CSE 251 Dr. Charles B. OwenProgramming in C26
![Page 27: Pointers, Arrays, Multidimensional - Michigan State …cse251/lecture11.pdf · Pointers, Arrays, Multidimensional Arrays ... 1 Programming in C. Review: Pointers ... pointer to the](https://reader031.fdocuments.us/reader031/viewer/2022020114/5af3e9227f8b9a190c8c5061/html5/thumbnails/27.jpg)
Accessing 2D Array Elements[ ][ ] { { } { } }int myMatrix[2][4] = { {1,2,3,4} , {5,6,7,8} };
1 2 3 4 5 6 7 81 2 3 4 5 6 7 8
Indexing: myMatrix[i][j] is same asg y [ ][j]*(myMatrix[i] + j)(*(myMatrix + i))[j]*((*(myMatrix + i)) + j)*((*(myMatrix + i)) + j)*(&myMatrix[0][0] + 4*i + j)
CSE 251 Dr. Charles B. OwenProgramming in C27
![Page 28: Pointers, Arrays, Multidimensional - Michigan State …cse251/lecture11.pdf · Pointers, Arrays, Multidimensional Arrays ... 1 Programming in C. Review: Pointers ... pointer to the](https://reader031.fdocuments.us/reader031/viewer/2022020114/5af3e9227f8b9a190c8c5061/html5/thumbnails/28.jpg)
Declaration#define ROWS 3#define COLS 5
int table[ROWS][COLS];
void display (table);
CSE 251 Dr. Charles B. OwenProgramming in C28
![Page 29: Pointers, Arrays, Multidimensional - Michigan State …cse251/lecture11.pdf · Pointers, Arrays, Multidimensional Arrays ... 1 Programming in C. Review: Pointers ... pointer to the](https://reader031.fdocuments.us/reader031/viewer/2022020114/5af3e9227f8b9a190c8c5061/html5/thumbnails/29.jpg)
void display( int x[ROWS][COLS] ) {
2D Arrays often require nested loops – two
for (int i=0; i < ROWS; i++) {
for (int j=0; j < COLS; j++ )
pvariables
( j ; j ; j ){
printf(" x[%d][%d]: %d", i, j, x[i][j]);}}printf("\n");
}printf("\n");
}
CSE 251 Dr. Charles B. OwenProgramming in C29
![Page 30: Pointers, Arrays, Multidimensional - Michigan State …cse251/lecture11.pdf · Pointers, Arrays, Multidimensional Arrays ... 1 Programming in C. Review: Pointers ... pointer to the](https://reader031.fdocuments.us/reader031/viewer/2022020114/5af3e9227f8b9a190c8c5061/html5/thumbnails/30.jpg)
Table A = { {13, 22, 9, 23},{17, 5, 24, 31, 55},{4 19 29 41 61} };
13 22 9 23 ?17 5 24 31 55{4, 19, 29, 41, 61} };4 19 29 41 61
Table B = {1, 2, 3, 4,5, 6, 7, 8, 9, 10 11 12 13 14 };
1 2 3 4 56 7 8 9 1011 12 13 14 ?10, 11, 12, 13, 14 }; 11 12 13 14 ?
CSE 251 Dr. Charles B. OwenProgramming in C30
![Page 31: Pointers, Arrays, Multidimensional - Michigan State …cse251/lecture11.pdf · Pointers, Arrays, Multidimensional Arrays ... 1 Programming in C. Review: Pointers ... pointer to the](https://reader031.fdocuments.us/reader031/viewer/2022020114/5af3e9227f8b9a190c8c5061/html5/thumbnails/31.jpg)
passing 2d arrays
In passing a multi‐dimensional array, the first array size does not have to be specified. The second (and any subsequent) dimensions must be given!
int myFun(int list[][10]);
CSE 251 Dr. Charles B. OwenProgramming in C31
![Page 32: Pointers, Arrays, Multidimensional - Michigan State …cse251/lecture11.pdf · Pointers, Arrays, Multidimensional Arrays ... 1 Programming in C. Review: Pointers ... pointer to the](https://reader031.fdocuments.us/reader031/viewer/2022020114/5af3e9227f8b9a190c8c5061/html5/thumbnails/32.jpg)
#define ROWS 3#define COLS 5
int addMatrix( int [ ][COLS] );int addMatrix( int [ ][COLS] );
int main() {int a[][COLS] = { {13 22 9 23 12} {17 5 24 31 55} {4 19 29 41 61} };int a[][COLS] = { {13, 22, 9, 23, 12}, {17, 5, 24, 31, 55}, {4, 19, 29, 41, 61} };printf(“Sum = %d\n”, addMatrix( a ) );
}
int addMatrix( int t[ ][COLS] )int addMatrix( int t[ ][COLS] ) {int i, j, sum = 0;for (i=0; i<ROWS; i++)for (i=0; i<ROWS; i++) for (j=0; j<COLS; j++) sum += t[i][j];
return sum;;}
CSE 251 Dr. Charles B. OwenProgramming in C32 1