arrays of pointers help

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Feb 2005
Posts: 2
Reputation: deejay400 is an unknown quantity at this point 
Solved Threads: 0
deejay400 deejay400 is offline Offline
Newbie Poster

arrays of pointers help

 
0
  #1
Feb 23rd, 2005
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
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,456
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 251
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: arrays of pointers help

 
0
  #2
Feb 23rd, 2005
Something akin to this?
  1. #include <stdio.h>
  2.  
  3. struct node
  4. {
  5. int i;
  6. char c;
  7. };
  8.  
  9. struct node list[] =
  10. {
  11. {1,'A'},{2,'B'},{3,'C'},{4,'D'},{5,'E'},{6,'F'},{7,'G'},{8,'H'},{9,'I'},
  12. };
  13.  
  14. struct node *plist[] =
  15. {
  16. &list[0], &list[2], &list[4], &list[6], &list[8],
  17. &list[1], &list[3], &list[5], &list[7],
  18. };
  19.  
  20. int main(void)
  21. {
  22. size_t i;
  23. puts("list:");
  24. for ( i = 0; i < sizeof list / sizeof *list; ++i )
  25. {
  26. printf("%p:list[%d]: {%d,'%c'}\n", &list[i], (int)i,
  27. list[i].i, list[i].c);
  28. }
  29. puts("plist:");
  30. for ( i = 0; i < sizeof plist / sizeof *plist; ++i )
  31. {
  32. printf("%p:plist[%d]=%p: {%d,'%c'}\n", &plist[i], (int)i, plist[i],
  33. plist[i]->i, plist[i]->c);
  34. }
  35. return 0;
  36. }
  37.  
  38. /* my output
  39. list:
  40. 0040A128:list[0]: {1,'A'}
  41. 0040A12D:list[1]: {2,'B'}
  42. 0040A132:list[2]: {3,'C'}
  43. 0040A137:list[3]: {4,'D'}
  44. 0040A13C:list[4]: {5,'E'}
  45. 0040A141:list[5]: {6,'F'}
  46. 0040A146:list[6]: {7,'G'}
  47. 0040A14B:list[7]: {8,'H'}
  48. 0040A150:list[8]: {9,'I'}
  49. plist:
  50. 0040A158:plist[0]=0040A128: {1,'A'}
  51. 0040A15C:plist[1]=0040A132: {3,'C'}
  52. 0040A160:plist[2]=0040A13C: {5,'E'}
  53. 0040A164:plist[3]=0040A146: {7,'G'}
  54. 0040A168:plist[4]=0040A150: {9,'I'}
  55. 0040A16C:plist[5]=0040A12D: {2,'B'}
  56. 0040A170:plist[6]=0040A137: {4,'D'}
  57. 0040A174:plist[7]=0040A141: {6,'F'}
  58. 0040A178:plist[8]=0040A14B: {8,'H'}
  59. */
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 2
Reputation: deejay400 is an unknown quantity at this point 
Solved Threads: 0
deejay400 deejay400 is offline Offline
Newbie Poster

Re: arrays of pointers help

 
0
  #3
Feb 23rd, 2005
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)

  1. #include <fstream>
  2. #include <iostream>
  3. #include <conio>
  4. #include <string>
  5. using namespace std;
  6.  
  7. struct node
  8. {
  9. char name[60];
  10. unsigned int dist;
  11. node *next;
  12. };
  13.  
  14. //********************************************************
  15.  
  16. int main(int argc, char **argv)
  17. {
  18.  
  19. clrscr(); //doesn't work on visual c++ compiler, maybe remove;
  20.  
  21. cout << "welcome to Location find by Daniel Constable (Feb 2005, V1.0)\n";
  22.  
  23. string fileName;
  24.  
  25. cout << "please enter location of input file\n"<< endl;
  26. cin >> fileName;
  27.  
  28. if (!infile)
  29. {
  30. cout << "Error opening file" << endl;
  31. }
  32. return (-1);
  33.  
  34. /*ADD FUNCTION TO COUNT HOW MANY LINES OF CODE, RETURN NUM_IN_RECORDS*/
  35.  
  36. int num_in_records;
  37.  
  38. int array[num_in_records]; //declares array[num_in_records
  39.  
  40.  
  41. for(int i = 0;i<numrecords;i++)
  42. {
  43. cin >> name1 >> name2 >> dist; //get records and place into array
  44. node name1, name2; //using structure node
  45. name1 = name1.name;
  46. name2 = name2.name;
  47. dist = name2.dist;
  48.  
  49. /* search(name) //use search function
  50.   if
  51. {
  52. name1 exists
  53. set new pointer at last name in linked list to name2 and dist
  54. }
  55.   else
  56. {
  57. place name1 into array
  58. }
  59.   }
  60. */
  61. while (user doesn't type 'q')
  62. {
  63. cout << "enter two locations\n";
  64. cin >> name1 >> name2;
  65.  
  66. /*
  67. djikstra's(int dist) make function to implement djikstras and return int = dist
  68. */
  69.  
  70. cout << "the distance between"<<name1<<"and"<<name2<<"is"<<d\n;
  71. }
  72.  
  73. return(0);
  74. }
  75.  
  76.  
  77. //********************************************************
  78.  
  79. bool search(int num_in_array, char name[40])
  80. /*function to search through array to see if location already exists*/
  81.  
  82. { int n=0
  83.  
  84. if (strcmp (name, array[n]->???) !=0)
  85. {
  86. if (n!= num_in_array)
  87. n++; //increment location in array
  88. else
  89. return 0; //when reached num_in_array stop
  90. }
  91. else
  92. return 1; //return 1 if name is found
  93.  
  94. }
Last edited by alc6379; Feb 23rd, 2005 at 6:22 pm. Reason: added [code] tags
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 199
Reputation: Tight_Coder_Ex is an unknown quantity at this point 
Solved Threads: 14
Tight_Coder_Ex's Avatar
Tight_Coder_Ex Tight_Coder_Ex is offline Offline
Junior Poster

Re: arrays of pointers help

 
0
  #4
Feb 23rd, 2005
Originally Posted by Dave Sinkula
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
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC