//////////////////////////////
Char **names = new char *[a];
for (int i = 0; i < a; i++)
{
names[i] = new char[b];
}
//////////////////////////////
char *names[a];
for (int i = 0; i < a; i++)
{
names[i] = new char[b];
}
/////////////////////////////
char names[a][b];
/////////////////////////////
Three ways to make a 2D array of chars using variables a and b. They all work fine.
The first one requires: delete [] names; when you're done with names. The others don't.
What's the difference? Is this really all allowed?
-Jay
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
The difference is that in first case you created names with new, in other cases names is automatic object what would be deallocated when you return from function.
tkorrovi
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I think I get it. I the first case the array will behave like one that was declared globally, in the other cases it will only be available within the function. right?
-Jay
-and thanks.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
//////////////////////////////
Char **names = new char *[a];
for (int i = 0; i < a; i++)
{
names[i] = new char[b];
}
//////////////////////////////
char *names[a];
for (int i = 0; i < a; i++)
{
names[i] = new char[b];
}
/////////////////////////////
char names[a][b];
/////////////////////////////
Three ways to make a 2D array of chars using variables a and b. They all work fine.
The first one requires: delete [] names; when you're done with names. The others don't.
What's the difference? Is this really all allowed?
-Jay
The difference is that in first case you created names with new, in other cases names is automatic object what would be deallocated when you return from function.
tkorrovi
I thought in general you had to
delete [] array
or you were just clearing the pointer, not the array, but I am probably wrong and stupid.
Wayne
I think I get it. I the first case the array will behave like one that was declared globally, in the other cases it will only be available within the function. right?
-Jay
-and thanks.
Are you counting on dynamically allocated memory space to be freed when the variable goes out scope?
Wayne
Here in C++. It is a 2D 'char' pointer-arrary.
#include <iostream>
#include <cstdlib>
using namespace std;
char *name[][2] = {
"item1", "Yes we have no bannas",
"item2", "No we got bannas today",
"item3", "maybe bannas another day",
"", "" // "item" "maybe another day"
}; // index message
// "" , ""
// [0] [1]
int main( )
{
// --------------------------------messages-----------------------------------
cout << name[0][0] << endl; // "item1"
cout << name[1][0] << endl; // "item2"
cout << name[2][0] << endl << endl; // "item3"
// ---------------------------------indexes-----------------------------------
cout << name[0][1] << endl; // message1 "Yes we have no bannas"
cout << name[1][1] << endl; // message2 "No we got bannas today"
cout << name[2][1] << endl << endl; // message3 "maybe bannas another day"
// ----------------------------------------------------------------------------
char str[80];
int i;
cout << "Enter keyword: ";
cin >> str; // item1 item2 item3
for(i=0; *name[i][0]; i++) //--indexes- step 1 creates: [0][0] [1][0] [2][0]
if(!strcmp(name[i][0], str)) //--indexes- step 2 compares with string 'str'
cout << name[i][1]; // prints message 'match' to screen ( note below )
// message1 message2 message3
system("PAUSE > NUL"); // [0][1] [1][1] [2][1]
return 0;
}
N@N!
Oh man sorry about the above post 'trash' !.
ok, here it is again.
Here in C++. It is a 2D 'char' pointer-arrary.
#include <iostream>
#include <cstdlib>
using namespace std;
char *name[][2] = {
"item1", "Yes we have no bannas",
"item2", "No we got bannas today",
"item3", "maybe bannas another day",
"", ""
};
int main( )
{
cout << name[0][0] << endl; // "item1"
cout << name[1][0] << endl; // "item2"
cout << name[2][0] << endl << endl; // "item3"
cout << name[0][1] << endl; // message1 "Yes we have no bannas"
cout << name[1][1] << endl; // message2 "No we got bannas today"
cout << name[2][1] << endl << endl; // message3 "maybe bannas another day"
char str[80];
int i;
cout << "Enter keyword: ";
cin >> str;
for(i=0; *name[i][0]; i++)
if(!strcmp(name[i][0], str))
cout << name[i][1];
system("PAUSE > NUL");
return 0;
}
N@N!