Thread: Scan and print an array in separate functions? (problem with code)

  1. #1
    Registered User
    Join Date
    Aug 2019
    Posts
    2

    Question Scan and print an array in separate functions? (problem with code)

    I'm learning about arrays and functions for a school assignment, so i wrote a test program to check if i understood the theory, but when i print the array the output is wrong.

    I just started learning and i honestly have no idea what i'm doing wrong.

    Code:
    #include <stdio.h>
    
    
    #define TAMF 2
    #define TAMC 3
    void scanArray(int array[][]);
    void printArray(int array[][]);
    
    
    int main()
    {
        int array[TAMF][TAMC];
        scanArray(array);
        printArray(array);
    }
    
    
    void scanArray(int array[][])
    {
        int i,j;
        for(i=0; i<TAMF;i++) {
            for(j=0;j<TAMC;j++){
                printf("Input array[%d][%d]:\n>", i, j);
                scanf("%d", &array[i][j]);
            }
        }
    }
    
    
    void printArray(int array[][])
    {
        printf("\n------array------\n");
        int i,j;
        for(i=0; i<TAMF;i++){
            for(j=0;j<TAMC;j++){
                printf("%d\t",array[i][j]);
            }
            printf("\n");
        }
    }
    For example, if i input:
    1 2 3 4 5 6

    It outputs:
    4 5 6
    4 5 6
    Last edited by maya_s; 08-31-2019 at 09:16 PM.

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    How does your code even compile? Because It shouldn't. You can't pass a 2D array to a function without telling it its dimensions. All but the leftmost dimension must be passed, like this:
    Code:
    void scanArray(int array[][TAMC]);
    void printArray(int array[][TAMC]);
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Aug 2019
    Posts
    2
    Quote Originally Posted by GReaper View Post
    How does your code even compile? Because It shouldn't. You can't pass a 2D array to a function without telling it its dimensions. All but the leftmost dimension must be passed, like this:
    Code:
    void scanArray(int array[][TAMC]);
    void printArray(int array[][TAMC]);
    I'm using a mobile app to write and compile code, so maybe that's why? No idea.

    Anyways, thank you! I somehow completely forgot about the rules for implicitly typed arrays. I'm still just blindly coding by trial and error, to be honest. Again, thank you for taking the time answer, there's no problem after i fixed that.

  4. #4
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    Quote Originally Posted by GReaper View Post
    How does your code even compile? Because It shouldn't. You can't pass a 2D array to a function without telling it its dimensions. All but the leftmost dimension must be passed, like this:
    Code:
    void scanArray(int array[][TAMC]);
    void printArray(int array[][TAMC]);
    Complementing GReaper information, here's a brief explanation on why incomplete arrays must have its "lower" dimensions specified. When you declare a complete array like:
    Code:
    int array[3][4];   // array[dx][dy];
    The compiler allocates 12 integers (3*4), sequentially. The 'array' identifier "points" to the first element (array[0][0]), and when you use "[]" operator, this calculation will be done:
    Code:
    int x = array[2][3];  // example reading array[2][3];
    int *p = (int *)array + 4*2 + 3;
    int y = *p;  // same thing
    Notice the lower dimension is used to place the correct offset as in:
    Code:
    ptr + dy*x + y
    Where "dy" is the lower dimension... Without the lower dimension, the compiler has no clue about each "line" size...
    Last edited by flp1969; 09-01-2019 at 02:49 AM.

  5. #5
    null pointer Structure's Avatar
    Join Date
    May 2019
    Posts
    338
    I'm still just blindly coding by trial and error
    me too...
    "without goto we would be wtf'd"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How do i scan and print an integer?
    By ziggy786 in forum C Programming
    Replies: 3
    Last Post: 01-28-2013, 06:05 PM
  2. Functions in a separate file
    By Fox101 in forum C Programming
    Replies: 1
    Last Post: 01-29-2008, 02:53 PM
  3. scan an image and print it
    By hephaest in forum C Programming
    Replies: 18
    Last Post: 09-08-2006, 04:34 PM
  4. Using strtok on separate functions
    By Thumper333 in forum C Programming
    Replies: 2
    Last Post: 10-24-2004, 02:19 PM
  5. Replies: 0
    Last Post: 02-21-2002, 06:05 PM

Tags for this Thread