returning a 2D array in a function

This is a discussion on returning a 2D array in a function within the C Programming forums, part of the General Programming Boards category; what i want is..... in a function i have a paramater that takes a 2D array... than i want to ...

  1. #1
    Unregistered
    Guest

    returning a 2D array in a function

    what i want is.....
    in a function i have a paramater that takes a 2D array... than i want to manipulate that array and return it..!
    the problems is it doesn't seems to return..

    here is my codes....


    #include <stdio.h>

    /** Function protype **/
    char fillArray(char map[][], char);

    int main(){
    char map[2][2];
    map = fillArray(map, 'x');
    printf("%s\n", map);
    return 0;
    }

    /* fills the array with symbol */
    char fillArray( char map[][], char symbol){
    int i,j;
    for(j=0; j<1; j++){
    for(i=0; i<1; i++){
    map[j][i]=symbol;
    }
    }
    return map;
    }

    here is my errors...

    PrintArray.c: In function `main':
    PrintArray.c:10: incompatible types in assignment
    PrintArray.c: In function `fillArray':
    PrintArray.c:20: arithmetic on pointer to an incomplete type
    PrintArray.c:23: warning: return makes integer from pointer without a cast


    i'm really new to C... so you guys have any ideas?

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,673
    Code:
    #include <stdio.h>
    
    /** Function protype **/
    void fillArray(char map[][2], char);
    
    int main () {
        char map[2][2];
        fillArray( map, 'x' );
        printf( "%2.2s\n", map[0] );    // these are not normal 'C' strings with a \0
        printf( "%2.2s\n", map[1] );    // at the end, so care is needed in printing
        return 0;
    }
    
    /* fills the array with symbol */ 
    void fillArray( char map[][2], char  symbol) {
        int i,j;
        for(j=0; j<2; j++ ) {
            for(i=0; i<2; i++ ) {
                map[j][i]=symbol;
            }
        }
    }

  3. #3
    Unregistered
    Guest
    i've try the codes you give me... thx... it works fine...

    i got a few questions..

    in....
    printf( "%2.2s\n", map[0] );
    printf( "%2.2s\n", map[1] );

    if i change to
    printf( "%s\n", map[0] );
    printf( "%s\n", map[1] );

    why is that i get this result
    xxxxw!@
    xxw!@

    i've got a java programming background
    and i notice you can't return an array in C
    is this true?

    i've also replace
    void fillArray(char map[][2], char)
    with
    void fillArray(char map[][], char)
    why do i get an compile error??

  4. #4
    Unregistered
    Guest
    also another question....

    i notice you've defined a array size in the function prototype
    void fillArray(char map[][2], char);
    what is the size of the map is dynamic...

    lets say a user input the size of the map... how would i achieve that... if we already set the size of the array?

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,673
    > why is that i get this result
    > xxxxw!@
    Didn't you read my previous post?

    > and i notice you can't return an array in C
    > is this true?
    You can't return an array (by value), but you can always return an array by reference

    > why do i get an compile error??
    Because it's wrong
    The compiler must know the sizes of all the minor dimensions - it's only the left most [] which can be left empty

    > lets say a user input the size of the map... how would i achieve that... if we already set the size of the array?
    How to dynamically allocate 2D arrays has been discussed many times before - try a board search.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 04:45 PM
  4. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM
  5. qt help
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 04-20-2002, 09:51 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21