I made 2 functions one that returns 2-Dim Character Array and the other one returning 2-Dim ******* Array. In case of character array it works but in case of ******* array instead of displaying those numbers in Arrays its showing memory locations. Here is the code.
#include <iostream.h>
#include <conio.h>
char **edit(char **str); // Prototype for 2-Dim Character Array
int **Dim (int **num); // Prototype for 2-Dim ******* Array
void main()
{
char **str;
int **num;
str = new char* [255];
num = new int* [255];
edit(str); // Call for 2-Dim Character Array
Dim (num); // Call for 2-Dim ******* Array
for(int j=0;j<3;j++)
{
cout<<str[j]<<endl;
cout<<num[j]<<endl; // The Problem lies over here, Address is getting printed
}
delete str;
delete num;
}
int **Dim (int **num)
{
int pNums[][3] = {3,4,5,6,5,4,6,1,9,5,5,1,0,0,6};
for (int i=0; i<3; i++)
{
num[i] = new int[20];
num[i] = pNums[i];
}
return num;
}
Can anyone help in sorting this?
Ejaz
July 29th, 2005, 02:30 AM
First of all, please use the code tags, while posing the code, its very hard to read the code, all aligned one sided.
Next, there are many things that are wrong in the code. You are using depricated header, (although haven't been used).
#include <iostream.h> should be #include <iostream>
Return type of main should be int. void is non-standard.
You are using inappropriate deallocation (delete) for 'new[]' data -- Effective C++ #5).
char ** is a pointer which pointers to another char pointer, its not a 2-D character array.
You are passing address of auto variable into caller spaces in edit(...) & Dim(...).
You are not using the return values of the functions, so whats the point of returning any value, if its not going to be used?
The whole thing can be simplified if you use STL (vector & string) . Is it an assignment? I mean, can you use STL?
maverick786us
July 29th, 2005, 03:12 AM
I need to go through it first
NMTop40
July 29th, 2005, 03:29 AM
and this was posted yesterday so it's a repeat post.
maverick786us
July 29th, 2005, 03:46 AM
No its not a repeat post. I tried the coding given in yesterday's answer and somehow it didn't work, therefore i had to post it.
Its simple. It is working in case of characters but not *******s.
NMTop40
July 29th, 2005, 04:05 AM
#include <iostream.h> // wrong header. use <iostream>
#include <conio.h> // I think this is a non-standard header for keyboard i/o?
char **edit(char **str); // Prototype for 2-Dim Character Array
/*
no it's a prototype for a function "edit" that takes as a
parameter a char** and returns a char**. char** is a
pointer to a pointer to a char (or an array of chars).
It is not a pointer to a 2-d array.
*/
int **Dim (int **num); // Prototype for 2-Dim ******* Array
/* see above */
void main() // but main() should return int
{
char **str;
int **num;
str = new char* [255]; // now str is an array of 255 pointers. But arrays are "evil"
num = new int* [255]; // num is an array of 255 pointers. Pointers are also "evil"
// (though not quite as evil as arrays).
edit(str); // Call for 2-Dim Character Array
Dim (num); // Call for 2-Dim ******* Array
for(int j=0;j<3;j++)
{
cout<<str[j]<<endl;
cout<<num[j]<<endl; // The Problem lies over here, Address is getting printed
// what do you expect it to output?: How do you output an int* ?
}
delete str; // wrong form of delete, use delete[]
delete num; // wrong form of delete. use delete []
}
char **edit(char **str)
{
char *pNames[] = {"Maverick","Marlin","Slider"}; // but they are const char*
for(int j=0;j<3;j++)
{
str[j] = new char[255]; // you call new here but never call delete on this
str[j] = pNames[j]; // instead you immediately create a leak by assigning the pointer to
// point to something else. (You presumably intended to use strcpy() )
}
return str;
}
int **Dim (int **num)
{
int pNums[][3] = {3,4,5,6,5,4,6,1,9,5,5,1,0,0,6}; // not of type int[N][3] for any N. (***)
for (int i=0; i<3; i++)
{
num[i] = new int[20];
num[i] = pNums[i]; // oops I did it again!
}
return num;
}
I don't know what compiler you are on that even allows you to compile the line where I've marked (***).
Anyway, unless this is legacy code you have picked up and are trying to do a "quick fix" without breaking anything else, you should consider a rethink about the way you have approached all of this. (I don't know exactly what you are trying to achieve with all these arrays).
Use the STL collection classes unless you have good reason not to. And there rarely is a reason not to, except for their lack of portability across libraries or networks.
maverick786us
July 29th, 2005, 04:12 AM
I am using VC++ 6.0 Compiler. OK I will try STL Libraries or Vector in this case thanks
maverick786us
July 29th, 2005, 04:18 AM
I never had any intention to put this question. It was just that yesterday Mt. HumtyDumpty answered my yesterday's post
and he told me that the same way it is going to work for integer array, but it didn't, thats why i posted this question along with the code
upashu2
July 29th, 2005, 04:27 AM
cout<<str[j]<<endl;
cout<<num[j]<<endl; // The Problem lies over here, Address is getting printed see the definition in your code
char **str;
int **num; both *num and *str will be address, since *str will point to one dimensional array of character, and it is considered string in C and by printf() , cout, etc. so when writing cout<<str[j] , it prints the string whose first character is str[j][0]. But *num is still is pointer .So writing cout<<num[j], it will print its value that is address. if it were strings, it would be printed its contents.To print integer array, use loop and print each contents indiviaully as num[i][j].
There are several mistakes in your code, some already pointed out by Ejaz .
char *pNames[] = {"Maverick","Marlin","Slider"};
for(int j=0;j<3;j++)
{
str[j] = new char[255]; ///allocating memory and storing pointer in str[j]
str[j] = pNames[j]; ///loosing pointer of above memory and overwritten by pName[j], pointer to local object
}
You are doing same thing in your other function also.
See modified code below
#include <iostream>
#include <cstring>
using namespace std;
char **edit(char **str); // Prototype for 2-Dim Character Array
int **Dim (int **num); // Prototype for 2-Dim ******* Array
int main(int argc, char* argv[])
{
char **str;
int **num;
str = new char* [255];
num = new int* [255];
edit(str); // Call for 2-Dim Character Array
Dim (num); // Call for 2-Dim ******* Array
int **Dim (int **num)
{
int pNums[3][5] = {3,4,5,6,5,4,6,1,9,5,5,1,0,0,6};
for (int i=0; i<3; i++)
{
num[i] = new int[5];
for(int j=0;j<5;j++)
num[i][j] = pNums[i][j];
}
return num;
}
NMTop40
July 29th, 2005, 04:28 AM
If possible you should try to upgrade to version 7.0 or later. VC6.0 is not very good with templates and has problems supporting some of the features of the C++ standard.
NMTop40
July 29th, 2005, 05:08 AM
int pNums[3][5] = { {3,4,5,6,5},{4,6,1,9,5},{5,1,0,0,6}};
I think that's the grouping. (It may be 5 groups of 3. I can never remember, probably because I never do it this way ).
But best avoided anyway.
[/code]
Better overall is:
#include <iostream>
#include <string>
#include <vector>
#include "matrix.h" // with the matrix from the FAQ.
#include <cassert>