Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dynamic Memory with a 2D Array of Pointers

Status
Not open for further replies.

Public32

IS-IT--Management
Feb 27, 2003
6
CA
I need to create a "Table" (2D array) whose instance are lookup tables of directories. Each table entry will consist of two strings of NO fixed length: a key, which is what we are looking up, and a value, which is what we are trying to find. For example, if our table represents a telephone directory that lists names and numbers only, the key will be names and the values will be the corresponding telephone numbers.

A table row consists of TWO POINTERS. One to DYNAMICALLY ALLOCATED key array and one to (corresponding) dynamically allocated value array. The two arrays are dynamically allocated so that they can be only as long as required - NO fixed size. The size of the table is NOT fixed either. Initially, the table largeenough for only up to 2 entries will be dynamically allocated.

How do you define this. We are basically looking for a 2D dynamically allocated array of char pointers (array of chars). What is the definition...

Thanks
 
>strings of NO fixed length

This is what std::string is for.

>We are basically looking for a 2D dynamically allocated array of char...

Really? Aren't you really just after a dictionary where the key is a string and the value is a string?

#include <map>
#include <string>
...
// std::map<KeyType, ValueType>
typedef std::map<std::string, std::string> TelephoneDir;

...
TelephoneDir dir;
dir[&quot;Donald Duck&quot;] = &quot;1234567&quot;;
...
TelephoneDir::const_iterator itFind = dir.find(&quot;Donald Duck&quot;);
if (itFind!=map.end())
{
std::string phoneDonaldDuck = (*itFind).second;
}
else
{
// No Donald Duck in map
}


/Per

if (typos) cout << &quot;My fingers are faster than my brain. Sorry for the typos.&quot;;
 
Sorry, forgot the CODE tags...

Code:
#include <map>
#include <string>
...
// std::map<KeyType, ValueType>
typedef std::map<std::string, std::string> TelephoneDir;

...
TelephoneDir dir;
dir[&quot;Donald Duck&quot;] = &quot;1234567&quot;;
...
TelephoneDir::const_iterator itFind = dir.find(&quot;Donald Duck&quot;);
if (itFind!=map.end())
{
  std::string phoneDonaldDuck = (*itFind).second;
}
else
{
   // No Donald Duck in map
}

/Per

if (typos) cout << &quot;My fingers are faster than my brain. Sorry for the typos.&quot;;
 
Hmmm... close but no cigar

As i said in the description of the problem we must use 2D DYNAMIC ARRAY OF POINTERS.

furtermore, I can't use string. only char; so in a sense an array of char.

My only problems is how to define the 2d array of pointers DYNAMICALLY

I am guessing it would be something like this.

typedef char* ptr;

ptr * Directory = new char[row][column];



 
>As i said in the description of the problem we must use 2D DYNAMIC ARRAY OF POINTERS.

Why must you do such a thing? Is it a school project?

>I can't use string. only char; so in a sense an array of char.

Sure you can. It's C++ aint it?

>My only problems is how to define the 2d array of pointers DYNAMICALLY

Ok then, use stc::vector its the safe way to handle dynamic arrays.
Simply make a vector of vectors.


/Per

if (typos) cout << &quot;My fingers are faster than my brain. Sorry for the typos.&quot;;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top