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
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
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.
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 ?
Yes, but not in this way. We must use dynamic allocation (new or malloc).Originally posted by Unregistered
so instead of defining the width and height ourselves
can we have user define it through scanf ?
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.
'new' and 'delete' are C++ keywords and they have no function in C. What you would want to do in C is this: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 }
Quzah.Code:char **new2DCharArray( int x, int y ) { char **c = NULL; if( x > 0 && y > 0 ) c = malloc( x * y * sizeof( char ) ); return c; }
Hope is the first step on the road to disappointment.
Oops, this is the C forum, rightOriginally 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.
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.