Thread: intialise 2D arrays??

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    1

    Unhappy intialise 2D arrays??

    how do you intialise a 2D array?

    int array[][] ??

    how do we use a 2D array as a parameter in function?

    void functionName(int array[][] , int numRows , int numCols)

    or

    void functionName(int array[numRows][numCols])

    ??

    please help

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    This works fine:
    Code:
    #include <stdio.h>
    #include <conio.h>
    
    const int Width=5;
    const int Height=8;
    
    int MyArray[Width][Height]={0};
    
    void Func(int Array[Width][Height])
    {
       Array[0][0]=12345;
    }
    
    int main()
    {
       Func(MyArray);
       printf("%d", MyArray[0][0]);
       getch();
    }
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Unregistered
    Guest
    const int Width=5;
    const int Height=8;

    int MyArray[Width][Height]={0};

    so instead of defining the width and height ourselves

    can we have user define it through scanf ?

  4. #4
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Originally posted by Unregistered
    so instead of defining the width and height ourselves

    can we have user define it through scanf ?
    Yes, but not in this way. We must use dynamic allocation (new or malloc).

    Sample (creates an int array with 10 elements):
    Code:
    int NrOfElements=10; //You can make the user input this number
    int* Pointer;
    Pointer = new int[NrOfElements]; //Allocate memory
    if(Pointer!=NULL) //Check to see if allocation was successful
    {
       //Do whatever you want here
       ...
       delete[] Pointer; //You must free the memory before you quit, memory leaks will occur otherwise
    }
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by Magos

    Yes, but not in this way. We must use dynamic allocation (new or malloc).

    Sample (creates an int array with 10 elements):
    Code:
    int NrOfElements=10; //You can make the user input this number
    int* Pointer;
    Pointer = new int[NrOfElements]; //Allocate memory
    if(Pointer!=NULL) //Check to see if allocation was successful
    {
       //Do whatever you want here
       ...
       delete[] Pointer; //You must free the memory before you quit, memory leaks will occur otherwise
    }
    'new' and 'delete' are C++ keywords and they have no function in C. What you would want to do in C is this:

    Code:
    char **new2DCharArray( int x, int y )
    {
        char **c = NULL;
    
        if( x > 0 && y > 0 ) c = malloc( x * y * sizeof( char ) );
        return c;
    }
    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Originally posted by quzah


    'new' and 'delete' are C++ keywords and they have no function in C. What you would want to do in C is this:
    Quzah.
    Oops, this is the C forum, right
    My mistake...
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with 2d arrays
    By thamiz in forum C Programming
    Replies: 25
    Last Post: 05-25-2008, 05:06 AM
  2. 2D Array's, assigning chars.
    By gman89 in forum C Programming
    Replies: 9
    Last Post: 04-26-2008, 11:03 PM
  3. returning 2D arrays
    By ... in forum C++ Programming
    Replies: 2
    Last Post: 09-02-2003, 12:28 PM
  4. Initialising 2D and 3D arrays
    By fry in forum C++ Programming
    Replies: 5
    Last Post: 08-01-2002, 04:34 AM
  5. Reading 2d arrays from file?
    By Ringhawk in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2002, 09:05 PM