Code:
#include <stdio.h>
#define SIZE 3
/* function prototype */
void swaparrays( int*, int* );
void printarray( int* );
int main( int argc, char *argv[] ) {
printf( "[!] Program swaps the elements of\
two integer arrays\n" );
int s1[SIZE][SIZE] = { { 1, 3, 4 },
{ 5, 1, 2 },
{ 7, 6, 9 } };
int s2[SIZE][SIZE] = { { 3, 2, 1 },
{ 5, 2, 8 },
{ 0, 1, 3 } };
printf( "Array 1:\n" );
printarray( s1[SIZE] );
printf( "Array 2:\n" );
printarray( s2[SIZE] );
/* swap arrays */
swaparrays( s1[SIZE], s2[SIZE] );
printf( "Now...\n" );
printf( "Array 1:\n" );
printarray( s1[SIZE] );
printf( "Array 2:\n" );
printarray( s2[SIZE] );
/* exit normally */
return 0;
} // END main
/*
* Function: swaparrays.
* This Function actually swaps the elements
* of the two integer 3x3 arrays.
* Arguments: takes two pointers of integers.
* Returns: nothing (void) .
*/
void swaparrays( int* a, int* b ) {
int i;
int j;
int temp;
for( i = 0; i < SIZE; i++ ) {
for( j = 0; j < SIZE; j++ ) {
temp = a[i][j];
a[i][j] = b[i][j];
b[i][j] = temp;
} // END INNER FOR LOOP
} // END OUTER FOR LOOP
} // END swaparrays
/*
* Function: printarray.
* This function prints a 3x3 integer array.
* Arguments: takes in a pointer to an integer array.
* Returns: nothing (void) .
*/
void printarray( int *a ) {
int i;
int j;
for( i = 0; i < SIZE; i++ ) {
for( j = 0; j < SIZE; j++ ) {
if( j % SIZE == 0)
putchar( '\n' );
printf( "%d %d %d", a[i][j] );
} // END INNER FOR LOOP
} // END OUTER FOR LOOP
putchar( '\n' );
} // END printarray
when try to compile with gcc it outputs:
Code:
pointers3.c: In function 'swaparrays':
pointers3.c:51: error: subscripted value is neither array nor pointer
pointers3.c:52: error: subscripted value is neither array nor pointer
pointers3.c:52: error: subscripted value is neither array nor pointer
pointers3.c:53: error: subscripted value is neither array nor pointer
pointers3.c: In function 'printarray':
pointers3.c:72: error: subscripted value is neither array nor pointer
I know something i miss with multidimensional arrays. I would like to keep this structure in my program regarding the algorithms but also i want to compile the program correctly. Any ideas?