Thread: How to pass a matrix/array from main to a function

  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    5

    How to pass a matrix/array from main to a function

    Hello,
    http://codeviewer.org/view/code:a70
    As you can see i have a bunch of matrix and an array in my main function and i have a bunch of other functions listed above. I'm trying to pass the matrix and array to those other functions and in those functions do some checks and edit the values of those matrix and array that are in the main. I don't think im suppose to return, am i passing them correctly?


    so....
    declare matrix/arrays in main
    pass them to functions
    do some checks
    if it checks out the condition, edit the values of those matrix and array in the function that i passed them to(to do so i would have to pass them? if so how)

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    use the name of the array:


    Code:
    /*Just a tutorial for a Cboard poster right now.
    
    status: na
    */
    
    #include <stdio.h>
    
    #define SIZEofArray 12
    
    void myFunction(char array[], int);
    
    int main(void) {
      int i, shift;
      char array[12] = { "Is this OK?" };
    
      putchar('\n');
      for(i = 0; i < SIZEofArray; i++)
        printf("%c", array[i]);
    
      printf("\n\n How many letters should I shift upward? [0-26]: ");
      scanf("%d", &shift);
    
      myFunction(array, shift);
      putchar('\n');
    
      for(i = 0; i < SIZEofArray; i++)
        printf("%c", array[i]);
    
      printf("\n\n\t\t\t     press enter when ready");
      while((i = getchar()) != '\n');
      i = getchar();
    
      return 0;
    }
    void myFunction(char array[], int myShift) {
      int i;
      for(i = 0; i < SIZEofArray; i++)
        if(array[i] >= 'A')
          array[i] += myShift;
    
    
    }
    Last edited by Adak; 12-09-2009 at 06:57 PM.

  3. #3
    Registered User
    Join Date
    Dec 2009
    Posts
    5
    I'm not following.

    So i just have to declare the functions at the top "void myFunction(char array[]);"
    Also i noticed in function, the parameters or whatever you want to call them, you don't specify the elements(0-11)/space of the array [12] void myFunction(char array[]), is that suppose to be left without numbers?
    Last edited by Inukami; 12-09-2009 at 06:54 PM.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    The last dimension is OK to leave blank. All others should be filled in. This is not the only way to pass an array, but it is the simplest and best to use - especially for beginners.

    I edited my earlier post. Try running that program, and see if it start to make sense for you.

    Note that the array values, are changed, in myFunction().


    Yeah, we're *REALLY* fond of calling the parameters, parameters -

  5. #5
    Registered User
    Join Date
    Dec 2009
    Posts
    5
    w-t-f

    26 errors...

    I get a lot of these errors:
    Size of the type 'int[]' is unknown or zero
    what does that mean, im guessing its because i didn't set a value in the function for the arrays
    http://codeviewer.org/view/code:a72


    arrr so frustrating..

    C:\Borland\BCC55\Bin>bcc32 ProjectGame.c
    Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland
    ProjectGame.c:
    Error E2453 ProjectGame.c 3: Size of the type 'int[]' is unknown or zero
    Error E2453 ProjectGame.c 12: Size of the type 'int[]' is unknown or zero in fun
    ction TotalScore
    Error E2453 ProjectGame.c 12: Size of the type 'int[]' is unknown or zero in fun
    ction TotalScore
    Error E2453 ProjectGame.c 12: Size of the type 'int[]' is unknown or zero in fun
    ction TotalScore
    Error E2453 ProjectGame.c 14: Size of the type 'int[]' is unknown or zero in fun
    ction TotalScore
    Error E2453 ProjectGame.c 14: Size of the type 'int[]' is unknown or zero in fun
    ction TotalScore
    Error E2453 ProjectGame.c 14: Size of the type 'int[]' is unknown or zero in fun
    ction TotalScore
    Error E2453 ProjectGame.c 16: Size of the type 'int[]' is unknown or zero in fun
    ction TotalScore
    Error E2453 ProjectGame.c 16: Size of the type 'int[]' is unknown or zero in fun
    ction TotalScore
    Error E2453 ProjectGame.c 16: Size of the type 'int[]' is unknown or zero in fun
    ction TotalScore
    Error E2453 ProjectGame.c 23: Size of the type 'int[]' is unknown or zero
    Error E2453 ProjectGame.c 23: Size of the type 'int[]' is unknown or zero
    Error E2453 ProjectGame.c 30: Size of the type 'int[]' is unknown or zero in fun
    ction WinTieLoss
    Error E2453 ProjectGame.c 30: Size of the type 'int[]' is unknown or zero in fun
    ction WinTieLoss
    Error E2453 ProjectGame.c 32: Size of the type 'int[]' is unknown or zero in fun
    ction WinTieLoss
    Error E2453 ProjectGame.c 32: Size of the type 'int[]' is unknown or zero in fun
    ction WinTieLoss
    Error E2453 ProjectGame.c 33: Size of the type 'int[]' is unknown or zero in fun
    ction WinTieLoss
    Error E2453 ProjectGame.c 33: Size of the type 'int[]' is unknown or zero in fun
    ction WinTieLoss
    Error E2453 ProjectGame.c 35: Size of the type 'int[]' is unknown or zero in fun
    ction WinTieLoss
    Error E2453 ProjectGame.c 35: Size of the type 'int[]' is unknown or zero in fun
    ction WinTieLoss
    Error E2453 ProjectGame.c 37: Size of the type 'int[]' is unknown or zero in fun
    ction WinTieLoss
    Error E2453 ProjectGame.c 37: Size of the type 'int[]' is unknown or zero in fun
    ction WinTieLoss
    Error E2453 ProjectGame.c 38: Size of the type 'int[]' is unknown or zero in fun
    ction WinTieLoss
    Error E2453 ProjectGame.c 38: Size of the type 'int[]' is unknown or zero in fun
    ction WinTieLoss
    Error E2453 ProjectGame.c 42: Size of the type 'int[]' is unknown or zero in fun
    ction WinTieLoss
    Error E2228 ProjectGame.c 42: Too many error or warning messages in function Win
    TieLoss
    *** 26 errors in Compile ***

    C:\Borland\BCC55\Bin>


    EDIT: OKAY so this seemed to have fixed all the errors and it actually ran soo... not sure >_>
    http://codeviewer.org/view/code:a73

    I just have to finish the "DISPLAY()" function to display all the arrays and matrix now.... >_<
    Last edited by Inukami; 12-09-2009 at 08:12 PM.

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You have to declare the size of all other dimensions, except the last one. This doesn't have that:

    Code:
    #
    void WinTieLoss(int Teams[][], int WinTieLossRecord[][])
    That code viewer has the WORST font and colors.

  7. #7
    Registered User
    Join Date
    Dec 2009
    Posts
    5
    haha which one do you use? Also can you explain why i dont have to declare the last dimensions?

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I use whatever the forum chooses. I'm not sure what it is, but it's almost square in dimension, so it's good when you use the code tags.

    That codeviewer admin seems like he's trying to play a joke with that font and color combo's!

    The array has to keep the size of the last dimension, internally. So, you're off the hook with that dimension.

    There are other ways to pass arrays, and you don't need to give any dimensional info at all - but that's not really using an array - you're then using raw memory and pointers. The code for that type of data handling, won't win any beauty contests, either.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling C in Visual Studio 2005
    By emanresu in forum C Programming
    Replies: 3
    Last Post: 11-16-2009, 04:25 AM
  2. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  5. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM