Thread: Compilation problems ( apparently )

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    7

    Compilation problems ( apparently )

    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?

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You can't index a single pointer as if it were a two dimension array. Not without doing the math yourself:
    Code:
    x = ptr[ MAXCOLS * row + col ];

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    7
    Thanks for the reply. I tried to fix the code, but really the best thing i did, was to print some addresses. The problem i have to solve is the following:
    Write a program that contains a function with prototype
    a. void swaparrays(int*, int*); This swaps the values of two 3x3
    arrays. Initialize the values of the arrays with any numbers you want.
    As far as i understand that's is not easy task if the prototype of the function takes as arguments of type *int. If it was something like swaparrays( int*[SIZE], int*[SIZE] ), then i would have better chances. So my question is: Do i misanderstood the problem, or it is not solveable with these specific conditions the exercise has?

  4. #4
    Registered User
    Join Date
    Mar 2009
    Posts
    48
    Code:
    void swaparrays(int*, int*);
    If you are going against the specifications, the method prototype could be
    Code:
    void swaparrays(int**, int**);
    And your matrix is 2 dimensional.

    Code:
    int s1[SIZE][SIZE]
    But function expects a 1-Dimensional array, one thing you can do is
    Code:
    void swaparrays( int* a, int* b ) {
        int i;
        
        int temp;
    
        for( i = 0; i < SIZE; i++ ) {
               /*Normal swap*/
                temp = a[i];
                a[i] = b[i];
                b[i] = temp;
            
        } 
    } // END swaparrays
    And call the function in main as
    Code:
     for (i = 0; i < SIZE; i++)
           swaparrays( s1[i], s2[i] );
    and the print array method as
    Code:
    void printarray( int *a ) {
         int i;
         putchar( '\n' );
         for( i = 0; i < SIZE; i++ ) {
             printf( "%d ", a[i] );
             
         } 
         putchar( '\n' );
     } // END pr
    and in main() call your method as
    Code:
    for (i = 0; i < SIZE; i++)
           printarray( s1[i] );
    This can be done with single dimensional arrays too.(although function calls and methods would be much more simpler)l

    Sample Run
    Code:
    Array 1:
    
    1 3 4
    
    5 1 2
    
    7 6 9
    Array 2:
    
    3 2 1
    
    5 2 8
    
    0 1 3
    Now...
    Array 1:
    
    3 2 1
    
    5 2 8
    
    0 1 3
    Array 2:
    
    1 3 4
    
    5 1 2
    
    7 6 9
    Last edited by zalezog; 10-10-2009 at 11:45 AM.

  5. #5
    Registered User
    Join Date
    Oct 2008
    Posts
    7
    Wow !! Really you gave me the point i missed. Thanks very much for your reply. It puts the things in order in my head. Thanks again!
    Now regarding the printarray( int *a) function you gave me, when the array is 2x2 ( test it ), maybe and for 3x3 and so on so forth it doesn't work for me. But that's not a problem because the printarray function can be written without any specifications given by the exercise. So i completed the program which is the following:
    Code:
    // credits: zalezog for swaparrays functions and not only!
    #include <stdio.h>
    #define SIZE 2
    
    void swaparrays( int *a, int *b ) {
    	int i;
    	int temp[SIZE];	// array of pointers
    
    	for( i = 0; i < SIZE; i++ ) {
    		temp[i] = a[i];
    		a[i] = b[i];
    		b[i] = temp[i];
    	} // END FOR LOOP
    	
    } // END swaparrays
    
    void printArray( int a[][SIZE] ) {
            int i;
            int j;
    
            for( i = 0; i < SIZE; i++ ) {
                    for( j = 0; j < SIZE; j++ ) {
                            if( j % SIZE == 0 )
                                    putchar( '\n' );
                            printf( "%3d", a[i][j] );
                    } // END FOR LOOP
            } // END FOR LOOP
            putchar( '\n' );
    } // END printArray
    
    int main() {
    	int i;
    
    	int s1[SIZE][SIZE] = { {2, 3}, {6, 8} };
    	int s2[SIZE][SIZE] = { {-2, -3}, {-5, -6} };
    
    	printArray( s1 );
    	printArray( s2 );
    	
    	for( i = 0; i < SIZE; i++ ) {
    		swaparrays( s1[i], s2[i] );
    	} // END FOR LOOP
    
    	printArray( s1 );
    	printArray( s2 );
    
    	return( 0 );
    
    } // END main

  6. #6
    Registered User
    Join Date
    Mar 2009
    Posts
    48
    Quote Originally Posted by guro
    Now regarding the printarray( int *a) function you gave me, when the array is 2x2 ( test it ), maybe and for 3x3 and so on so forth it doesn't work for me.
    As long as your matrix is square of size SIZE * SIZE, it should work.

    Here's my version:
    Code:
    #include <stdio.h>
    #define SIZE 3
    
    /* function prototype */
    void swaparrays( int*, int* );
    void printarray( int* );
    
    int main( int argc, char *argv[] ) {
        int i = 0;
        //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" );
        for (i = 0; i < SIZE; i++)
           printarray( s1[i] );
        printf( "Array 2:\n" );
        for (i = 0; i < SIZE; i++)
            printarray( s2[i] );
    
        /* swap arrays */
        for (i = 0; i < SIZE; i++)
           swaparrays( s1[i], s2[i] );
    
        printf( "Now...\n" );
        printf( "Array 1:\n" );
        for (i = 0; i < SIZE; i++)
           printarray( s1[i] );
    
        printf( "Array 2:\n" );
        for (i = 0; i < SIZE; i++)
           printarray( s2[i] );
    
    
        /* 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++ ) {
    
                temp = a[i];
                a[i] = b[i];
                b[i] = 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;
         putchar( '\n' );
         for( i = 0; i < SIZE; i++ ) {
             printf( "%d ", a[i] );
              // END INNER FOR LOOP
         } // END OUTER FOR LOOP
         putchar( '\n' );
     } // END printarray

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compilation Problem w/ CR/LFs in Translated char strings
    By joeprogrammer1 in forum C Programming
    Replies: 1
    Last Post: 09-02-2009, 01:32 PM
  2. No clue how to make a code to solve problems!
    By ctnzn in forum C Programming
    Replies: 8
    Last Post: 10-16-2008, 02:59 AM
  3. compilation problems with static data member
    By viral612 in forum C++ Programming
    Replies: 1
    Last Post: 08-13-2008, 06:55 AM
  4. MS VC++ Crash on compilation
    By Magos in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 08-23-2003, 07:06 PM
  5. Compilation problems in Linux
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 08-19-2002, 02:01 PM