seperating initialising and printing of arrays into functions

This is a discussion on seperating initialising and printing of arrays into functions within the C Programming forums, part of the General Programming Boards category; Hello all, I was trying to learn some C programming skills. Presently I am just doing very basic stuff. I ...

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    37

    Angry seperating initialising and printing of arrays into functions

    Hello all,

    I was trying to learn some C programming skills. Presently I am just doing very basic stuff. I created a 2d array, which is defined by the user with scanf. I am able to initialise the array and print the array when this is done in the same function. However i am unable to initialise and print the array when I seperate these two tasks into 2 seperate function.

    I have only a limited understanding of how arguments are passed to array(I surprised myself when I actually got my one function to work).

    Here is an outline of the code:

    Code:
    #include<stdio.h>
    
    int print_array();
    in_initialise_array();
    
    main(){
    
    int rows;
    int columns;
    scanf("%d %d", &rows, &columns);
    
    initialise_array(rows, columns);
    
    /*print_array(rows, columns);*/
    }
    
    
    int initialise_array(x, y)
    {
    char matrix[x][y];
    int i, j;
    
    for(i=0;i<x; i++)
     for(j=0; j<y; j++)
      matrix[i][j]='*';
    
    
    for(i=0;i<x; i++)
     {
       for(j=0;j<y;j++)
        {
          printf("%c ", matrix[i][j]);
         }
       printf("\n");
     }
    }
    
    
    int print_array(x, y)
    {
    int i, j;
    
    char matrix[x][y];
    
    for(i=0;i<x; i++)
     {
       for(j=0;j<y;j++)
        {
          printf("%c ", matrix[i][j]);
         }
       printf("\n");
     }
    
    }

    If there are any typos, appologies, I couldnt work out how to copy from my VMware into this message board. I had to type this in manually from my VMWare session

    A couple of thing:

    1. The argument in the functions(Icalled them x and y) I can call these anything right? C will know in the line:

    Code:
    print_array(rows, columns)
    that x is rows and y is columns?

    2. I suspect i dont have to declare the array matrix a second time in print_array().

    When I tried to seperate out the 2 function it would not print properly.

    Can anyone give me an insight please?
    Last edited by Tom Bombadil; 03-11-2009 at 12:30 PM. Reason: I missed out a section of code when typing it in

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    After I corrected the typo on line 4, I got this output:

    Code:
    [root~/C] ./a.out
    3 3
    * * * 
    * * * 
    * * *
    In answer to some of your questions:
    • no, the variable names in the function call and the function preamble don't have to match
    • no, there is no point in declaring a new matrix in print_array

    However, you need to pass a pointer to the matrix to the print_array function, or there will be no matrix to do anything with.
    Also, since you are not returning any values from your functions, you should declare them void:
    Code:
    void print_array();
    void initialise_array();
    The actual preamble should match.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

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