| | |
arrays of pointers help
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Feb 2005
Posts: 2
Reputation:
Solved Threads: 0
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
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?
C Syntax (Toggle Plain Text)
#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'} */
"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
•
•
Join Date: Feb 2005
Posts: 2
Reputation:
Solved Threads: 0
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)
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)
C Syntax (Toggle Plain Text)
#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 }
Last edited by alc6379; Feb 23rd, 2005 at 6:22 pm. Reason: added [code] tags
•
•
•
•
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],
};
![]() |
Similar Threads
- arrays and pointers !!! (C++)
- array of pointers to integer arrays (C++)
- Arrays and Pointers (C++)
- Functions - arrays/pointers re - const (C++)
- What relation does **indirection operator have with Multidimensional Arrays (C++)
- Sorting arrays of pointers with function? (C)
Other Threads in the C Forum
- Previous Thread: Probabilistic Linked Skip List
- Next Thread: Setting Console window size.
| Thread Tools | Search this Thread |
Tag cloud for C
adobe ansi api array arrays asterisks binarysearch calculate centimeter char convert copyanyfile copyimagefile copypdffile cprogramme createcopyoffile csyntax directory drawing dynamic executable fflush file fork frequency getlasterror givemetehcodez graphics gtkgcurlcompiling hacking hardware highest homework i/o inches incrementoperators infiniteloop initialization interest km lazy linked linkedlist linux linuxsegmentationfault list locate logical_drives match matrix microsoft motherboard multi mysql number open opendocumentformat opensource owf pattern pdf performance pointer pointers posix power problem probleminc program programming pyramidusingturboccodes read recursion recv repetition scanf scheduling scripting segmentationfault send shape socketprograming spoonfeeding stack standard string strings structures student suggestions systemcall test testautomation unix user variable voidmain() wab win32api windows.h






