Accessing a Structure Using a Pointer

S

sakitah

Here is what I have:

struct SubList
{
int BookId;
int WFreq;
};

struct Listing
{
string Word;
int BookCnt;
int TWFreq; //total frequency in all books
SubList * ptr[10];

};


A Listing structure will be used as an array like so:
Listing a[100];

Now, the Listing pointer variable s points to an array of SubList
structure, so 1 Listing can point to more than one SubList. If a word
is found in more than one book, it will have one entry in Listing which
then points to SubList with all the book id's it is found in. Either
way, if a word is found in Listing then it is bound to have a book id
and so it's s pointer will have to point to SubList with at least one
entry.
I am a bit confused as to how I'll be accessing this SubList
structure. So far, this is what I have been able to come up with, but
needless to say, it's wrong:

a.Word = FullBase[FBI].Word;
a.BookCnt = 1;
a.TWFreq = FullBase[FBI].WordFreq;

//now make entry for this word into SubList
a.ptr = new Posting;
a.(ptr[0]->BookId) = BookId_tmp;

In the last line, I get an error that the '(' is illegal (followed by a
gazillion other errors, but I'm sure that it is the parenthesis
notation that is wrong).

Gratefull for any help
SA
 
A

Alf P. Steinbach

* sakitah:
Here is what I have:

struct SubList
{
int BookId;
int WFreq;
};

struct Listing
{
string Word;
int BookCnt;
int TWFreq; //total frequency in all books
SubList * ptr[10];

};


A Listing structure will be used as an array like so:
Listing a[100];

...
Gratefull for any help

Use standard library collection classes such as std::vector (instead of
raw arrays and pointers), and use meaningful names.
 
N

Neelesh

sakitah said:
Here is what I have:

struct SubList
{
int BookId;
int WFreq;
};

struct Listing
{
string Word;
int BookCnt;
int TWFreq; //total frequency in all books
SubList * ptr[10];

};

Not that its not 'ptr' that is the array of SubList pointers, it is
Listing::ptr.
A Listing structure will be used as an array like so:
Listing a[100];

Now, the Listing pointer variable s points to an array of SubList
structure, so 1 Listing can point to more than one SubList. If a word
is found in more than one book, it will have one entry in Listing which
then points to SubList with all the book id's it is found in. Either
way, if a word is found in Listing then it is bound to have a book id
and so it's s pointer will have to point to SubList with at least one
entry.
I am a bit confused as to how I'll be accessing this SubList
structure. So far, this is what I have been able to come up with, but
needless to say, it's wrong:

a.Word = FullBase[FBI].Word;
a.BookCnt = 1;
a.TWFreq = FullBase[FBI].WordFreq;

//now make entry for this word into SubList
a.ptr = new Posting;
a.(ptr[0]->BookId) = BookId_tmp;


Hence it is incorrect to index ptr, you must index a.ptr
The correct code would be

a.ptr[0]->BookId = BookId_tmp;
In the last line, I get an error that the '(' is illegal (followed by a
gazillion other errors, but I'm sure that it is the parenthesis
notation that is wrong).

As an aside, it is recommended that one should prefer vector or other
containers of the standard library over arrays.
 
M

Mike Wahler

sakitah said:
Here is what I have:

struct SubList
{
int BookId;
int WFreq;
};

struct Listing
{
string Word;
int BookCnt;
int TWFreq; //total frequency in all books
SubList * ptr[10];

};


A Listing structure will be used as an array like so:
Listing a[100];

Now, the Listing pointer variable s points to an array of SubList
structure, so 1 Listing can point to more than one SubList. If a word
is found in more than one book, it will have one entry in Listing which
then points to SubList with all the book id's it is found in. Either
way, if a word is found in Listing then it is bound to have a book id
and so it's s pointer will have to point to SubList with at least one
entry.
I am a bit confused as to how I'll be accessing this SubList
structure. So far, this is what I have been able to come up with, but
needless to say, it's wrong:

a.Word = FullBase[FBI].Word;
a.BookCnt = 1;
a.TWFreq = FullBase[FBI].WordFreq;

//now make entry for this word into SubList
a.ptr = new Posting;
a.(ptr[0]->BookId) = BookId_tmp;

In the last line, I get an error that the '(' is illegal (followed by a
gazillion other errors, but I'm sure that it is the parenthesis
notation that is wrong).


Um,

a.ptr[0]->BookId = BookId_tmp;

The compiler told you the problem.

-Mike
 
E

ecky-l

The procedure to access the ptr[] structures was already mentioned
(what does "ptr" mean - do you know this when you look at your hundret
lines of code 6 month later?), as well as the recommendation to use
std::vector and friends.

You surely have a good reason to use structures rather than classes?


Eckhard
 
N

Neil Cerutti

Here is what I have:

struct SubList
{
int BookId;
int WFreq;
};

struct Listing
{
string Word;
int BookCnt;
int TWFreq; //total frequency in all books
SubList * ptr[10];

};


A Listing structure will be used as an array like so:
Listing a[100];

Now, the Listing pointer variable s

There is no variable s.
points to an array of SubList structure, so 1 Listing can point
to more than one SubList. If a word is found in more than one
book, it will have one entry in Listing which then points to
SubList with all the book id's it is found in. Either way, if
a word is found in Listing then it is bound to have a book id
and so it's s pointer will have to point to SubList with at
least one entry.

Where are you going to find books whose vocabulary is limited to
100 words?

Why are there 10 SubLists for each word?
 
S

sakitah

I tried it like this (without the '()')

a.ptr = new SubList;
a.ptr[0]->BookId = BookId_tmp;

and sadly it refused to compile....I get an error at line:
a.ptr = new SubList;

with the following error:
C:\Documents and Settings\B only B\My
Documents\Master's\FaLl05\Cs681\Inverted
Files\InvertedFile\InvertedFile.cpp(125) : error C2440: '=' : cannot
convert from 'struct SubList*' to 'struct SubList*[10]'
There are no conversions to array types, although there are
conversions to references or pointers to arrays

I tried commenting out that line and it did compile but when run, the
program stops at that line, which does make sense.....I wasnt expecting
it to work anyways.

Obviuosly I need that line in there, just dont know how to write it
correctly.
Help
 
K

Krishanu Debnath

sakitah said:
I tried it like this (without the '()')

a.ptr = new SubList;
a.ptr[0]->BookId = BookId_tmp;

and sadly it refused to compile....I get an error at line:
a.ptr = new SubList;

with the following error:
C:\Documents and Settings\B only B\My
Documents\Master's\FaLl05\Cs681\Inverted
Files\InvertedFile\InvertedFile.cpp(125) : error C2440: '=' : cannot
convert from 'struct SubList*' to 'struct SubList*[10]'
There are no conversions to array types, although there are
conversions to references or pointers to arrays


You are not paying attention to compiler's errors. ptr is an array 10 of
pointer to int. Array name is a not modifiable lvalue, and you are
assigning struct SubList* to it. you want ..

a.ptr[0] = new SubList;

Krishanu
 
K

Krishanu Debnath

Krishanu said:
sakitah said:
I tried it like this (without the '()')

a.ptr = new SubList;
a.ptr[0]->BookId = BookId_tmp;

and sadly it refused to compile....I get an error at line:
a.ptr = new SubList;

with the following error:
C:\Documents and Settings\B only B\My
Documents\Master's\FaLl05\Cs681\Inverted
Files\InvertedFile\InvertedFile.cpp(125) : error C2440: '=' : cannot
convert from 'struct SubList*' to 'struct SubList*[10]'
There are no conversions to array types, although there are
conversions to references or pointers to arrays


You are not paying attention to compiler's errors. ptr is an array 10 of


ptr is an array 10 of pointer to SubList, darn.
pointer to int. Array name is a not modifiable lvalue, and you are
assigning struct SubList* to it. you want ..

a.ptr[0] = new SubList;

Krishanu


Krishanu
 

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
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top