Hi,
Trying to get my head around constructing an array of pointers (I think this is what its called).
Been reading lots of online tutorials etc and just getting confused.

Basically what I'm trying to is set an array of chars, with each array cell pointing to a linked list (each cell in the linked list I want to contain an integer value and char's)

[ ]---->[][]---->[][]
array[n] [ ]---->[][]
[ ]---->[][]---etc

Any ideas about how I could implement this?

Thanks
Dan

Something akin to this?

#include <stdio.h>

struct node
{
   int i;
   char c;
};

struct node list[] =
{
   {1,'A'},{2,'B'},{3,'C'},{4,'D'},{5,'E'},{6,'F'},{7,'G'},{8,'H'},{9,'I'},
};

struct node *plist[] =
{
   &list[0], &list[2], &list[4], &list[6], &list[8],
   &list[1], &list[3], &list[5], &list[7],
};

int main(void)
{
   size_t i;
   puts("list:");
   for ( i = 0; i < sizeof list / sizeof *list; ++i )
   {
      printf("%p:list[%d]:  {%d,'%c'}\n", &list[i], (int)i, 
             list[i].i, list[i].c);
   }
   puts("plist:");
   for ( i = 0; i < sizeof plist / sizeof *plist; ++i )
   {
      printf("%p:plist[%d]=%p: {%d,'%c'}\n", &plist[i], (int)i, plist[i],
             plist[i]->i, plist[i]->c);
   }
   return 0;
}

/* my output
list:
0040A128:list[0]:  {1,'A'}
0040A12D:list[1]:  {2,'B'}
0040A132:list[2]:  {3,'C'}
0040A137:list[3]:  {4,'D'}
0040A13C:list[4]:  {5,'E'}
0040A141:list[5]:  {6,'F'}
0040A146:list[6]:  {7,'G'}
0040A14B:list[7]:  {8,'H'}
0040A150:list[8]:  {9,'I'}
plist:
0040A158:plist[0]=0040A128: {1,'A'}
0040A15C:plist[1]=0040A132: {3,'C'}
0040A160:plist[2]=0040A13C: {5,'E'}
0040A164:plist[3]=0040A146: {7,'G'}
0040A168:plist[4]=0040A150: {9,'I'}
0040A16C:plist[5]=0040A12D: {2,'B'}
0040A170:plist[6]=0040A137: {4,'D'}
0040A174:plist[7]=0040A141: {6,'F'}
0040A178:plist[8]=0040A14B: {8,'H'}
*/

Thanks for the quick response, but most of that looks very unfamiliar to me.

Sorry, I should have explained the problem better.

Basically I need to write a program in C++ that can read in an external file, which contains an arbitary ammount of two locations and a distance between them.
It then has to find the shortest distance between any two locations. (think I'm going to use Djikstra's algorithm for this)

I wanted to try and implement some kind of adjacency list, but thought if I can count how many lines are in the external file, my way would be easier to implement (my coding really isn't good at all).

heres what i've started (sorry, theres lots of notes to myslef to remind me to add things etc)

#include <fstream>
#include <iostream>
#include <conio> 
#include <string>
using namespace std;

struct node
		{
		char name[60];
		unsigned int dist;
		node *next;
		};

//********************************************************

int main(int argc, char **argv)
{
	
clrscr();		//doesn't work on visual c++ compiler, maybe remove;

cout << "welcome to Location find by Daniel Constable (Feb 2005, V1.0)\n";

string fileName;

	cout << "please enter location of input file\n"<< endl;
    	cin >> fileName;

        	if (!infile)
    		{
        	cout << "Error opening file" << endl;
        	}
	return (-1);

/*ADD FUNCTION TO COUNT HOW MANY LINES OF CODE, RETURN NUM_IN_RECORDS*/

int num_in_records;				
					 
int array[num_in_records];		//declares array[num_in_records				
				

      for(int i = 0;i<numrecords;i++)
       {
	cin >> name1 >> name2 >> dist;  //get records and place into array
	node name1, name2;		//using structure node
	name1 = name1.name;
	name2 = name2.name;
	dist  =	name2.dist;
	
/*	search(name)			//use search function
      if
	{
	 name1 exists
	 set new pointer at last name in linked list to name2 and dist 
	}
      else
	{
	 place name1 into array
	}
       }	
*/
	while (user doesn't type 'q')
	{
	 cout << "enter two locations\n";
	 cin >> name1 >> name2;

/*	
 djikstra's(int dist)  make function to implement djikstras and return int = dist
*/  

	 cout << "the distance between"<<name1<<"and"<<name2<<"is"<<d\n;
	}

return(0);
}


//********************************************************

bool	search(int num_in_array, char name[40])		
/*function to search through array to see if location already exists*/

{								int n=0

   if (strcmp (name, array[n]->???) !=0)
    {
     if (n!= num_in_array)
	n++;						                                       //increment location in array
     else
	return 0;			//when reached num_in_array stop
    }
    else						
	return 1;			//return 1 if name is found
      
}

struct node
{
int i;
char c;
};

struct node list[] =
{
{1,'A'},{2,'B'},{3,'C'},{4,'D'},{5,'E'},{6,'F'},{7,'G'},{8,'H'},{9,'I'},
};

struct node *plist[] =
{
&list[0], &list[2], &list[4], &list[6], &list[8],
&list[1], &list[3], &list[5], &list[7],
};

Nice Dave, I've been asked this question quite a bit and have never though of putting it together in this context

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.