Array of pointers

J

Joah Senegal

Hello all, I;m trying to connect a set of linkedlists to a array of pointer.
So the array contains pointers to the linkedlists... but how do I make a
array of pointer?
 
N

Nick Keighley

Joah said:
Hello all, I;m trying to connect a set of linkedlists to a array of pointer.
So the array contains pointers to the linkedlists... but how do I make a
array of pointer?

struct List
{
int data;
List* link;
};

List* array_of_ptr [100];

but you should consider not doing this. Perhaps a std::vector of
std::list
would be a better idea.


--
Nick Keighley

The fscanf equivalent of fgets is so simple
that it can be used inline whenever needed:-
char s[NN + 1] = "", c;
int rc = fscanf(fp, "%NN[^\n]%1[\n]", s, &c);
if (rc == 1) fscanf("%*[^\n]%*c);
if (rc == 0) getc(fp);
 
J

Joah Senegal

Many thanks! It was very simple but this helpen me alot :D THANKS !


Nick Keighley said:
Joah said:
Hello all, I;m trying to connect a set of linkedlists to a array of
pointer.
So the array contains pointers to the linkedlists... but how do I make a
array of pointer?

struct List
{
int data;
List* link;
};

List* array_of_ptr [100];

but you should consider not doing this. Perhaps a std::vector of
std::list
would be a better idea.


--
Nick Keighley

The fscanf equivalent of fgets is so simple
that it can be used inline whenever needed:-
char s[NN + 1] = "", c;
int rc = fscanf(fp, "%NN[^\n]%1[\n]", s, &c);
if (rc == 1) fscanf("%*[^\n]%*c);
if (rc == 0) getc(fp);
 
J

Joah Senegal

Well,

Now I have a array of List pointers wich points to list objects. In the
class List I have method called insert... well now I;ve the following code:

if (slot[pos]==NULL)
{
slot[pos] = new List();
///// overhere I need to call List::insert, but how
}
else
{
}
}

So how to I call the new List? I can't do slot[pos].insert .. Ofcourse
that;s not possible.... anyone has experience with this ?

THANKS
!!!


Joah Senegal said:
Many thanks! It was very simple but this helpen me alot :D THANKS !


Nick Keighley said:
Joah said:
Hello all, I;m trying to connect a set of linkedlists to a array of
pointer.
So the array contains pointers to the linkedlists... but how do I make a
array of pointer?

struct List
{
int data;
List* link;
};

List* array_of_ptr [100];

but you should consider not doing this. Perhaps a std::vector of
std::list
would be a better idea.


--
Nick Keighley

The fscanf equivalent of fgets is so simple
that it can be used inline whenever needed:-
char s[NN + 1] = "", c;
int rc = fscanf(fp, "%NN[^\n]%1[\n]", s, &c);
if (rc == 1) fscanf("%*[^\n]%*c);
if (rc == 0) getc(fp);
 
T

Thomas Matthews

Joah said:
Well,

Now I have a array of List pointers wich points to list objects. In the
class List I have method called insert... well now I;ve the following code:

if (slot[pos]==NULL)
{
slot[pos] = new List();
///// overhere I need to call List::insert, but how
}
else
{
}
}

So how to I call the new List? I can't do slot[pos].insert .. Ofcourse
that;s not possible.... anyone has experience with this ?

THANKS
!!!
It is possible to call slot[pos].insert().
#include <iostream>
#include <cstdlib>
using namespace std; // because I'm lazy.

struct Node
{
unsigned int data;
struct Node * next;
void insert(void)
{
cout << "Node::insert()" << endl;
}
} slot[5];

int main(void)
{
unsigned int pos = 3;
slot[pos].insert();
return EXIT_SUCCESS;
}


--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library
 
N

Nick Keighley

Joah said:
Now I have a array of List pointers wich points to list objects. In the
class List I have method called insert... well now I;ve the following code:

if (slot[pos]==NULL)
{
slot[pos] = new List();
///// overhere I need to call List::insert, but how
}
else
{
}
}

So how to I call the new List? I can't do slot[pos].insert .. Ofcourse
that;s not possible.... anyone has experience with this ?

Assuming List has an insert member:-

slot[pos]->insert();

this is pretty basic, perhaps you should go back to your textbook?
Also *seriously* consider keeping away from C style arrays and pointers

<snip>
 
J

Jim Langston

Joah Senegal said:
Well,

Now I have a array of List pointers wich points to list objects. In the
class List I have method called insert... well now I;ve the following
code:

if (slot[pos]==NULL)
{
slot[pos] = new List();
///// overhere I need to call List::insert, but how
}
else
{
}
}

So how to I call the new List? I can't do slot[pos].insert .. Ofcourse
that;s not possible.... anyone has experience with this ?

THANKS
!!!

Your choice.

slot[pos]->insert
(*slot[pos]).insert
May need the parenthesis on that last one rearranged a little bit (I think
[] has precidence over * but am too lazy to check right now).
Joah Senegal said:
Many thanks! It was very simple but this helpen me alot :D THANKS !


Nick Keighley said:
Joah Senegal wrote:

Hello all, I;m trying to connect a set of linkedlists to a array of
pointer.
So the array contains pointers to the linkedlists... but how do I make
a
array of pointer?

struct List
{
int data;
List* link;
};

List* array_of_ptr [100];

but you should consider not doing this. Perhaps a std::vector of
std::list
would be a better idea.


--
Nick Keighley

The fscanf equivalent of fgets is so simple
that it can be used inline whenever needed:-
char s[NN + 1] = "", c;
int rc = fscanf(fp, "%NN[^\n]%1[\n]", s, &c);
if (rc == 1) fscanf("%*[^\n]%*c);
if (rc == 0) getc(fp);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,430
Messages
2,571,676
Members
48,796
Latest member
Greg L.

Latest Threads

Top