problems with pointers and structs

P

pcnate

I've been having some problems with pointers and such.This is homework,
so I don't want people writing codeand telling me to use it. I just
want some direction on what isn't working.


here is the structure prototype:
struct Books
{
string ISBN;
string title;
string author;
string publisher;
float price;
int onHand;
};

here is the call:
swapElems(book, startscan, minIndex);

book is the struct, startscan is an int for the subscript, and so is
minIndex

here is the func with lines in question:
swapElems(Books *x, int startscan, int minIndex)
{
Books temp = { "", "", "", "", 0.0f, 0 };

temp = *x[startscan];
*x[startscan] = *x[minIndex];
*x[minIndex] = temp;
}

I get the four errors that say illegal indirection and the compiler is
MS visual studio 2003, not that it even matters. I need to do this, but
more importantly, I need to learn it TOO.
 
A

Alf P. Steinbach

* (e-mail address removed):
I've been having some problems with pointers and such.This is homework,
so I don't want people writing codeand telling me to use it. I just
want some direction on what isn't working.


here is the structure prototype:
struct Books
{
string ISBN;
string title;
string author;
string publisher;
float price;
int onHand;
};

Are you sure this is a prototype of a collection of Books, and not a
single Book?

It looks like a single Book to me.


here is the call:
swapElems(book, startscan, minIndex);

If you defined 'book' etc. then perhaps it could tell others something.

book is the struct, startscan is an int for the subscript, and so is
minIndex

here is the func with lines in question:
swapElems(Books *x, int startscan, int minIndex)
{
Books temp = { "", "", "", "", 0.0f, 0 };

temp = *x[startscan];
*x[startscan] = *x[minIndex];
*x[minIndex] = temp;
}

The best advice is to NOT USE RAW POINTERS or raw arrays until you gain
much more experience.

To represent a collection of books, do something like

std::vector<Book> books;

or

std::vector<Book> books(500);

To swap e.g. the books at indices 5 and 13, do

std::swap( books.at(5), books.at(13) );
I get the four errors that say illegal indirection and the compiler is
MS visual studio 2003, not that it even matters. I need to do this, but
more importantly, I need to learn it TOO.

Use the standard library, as explained above.

Keep away from pointers and raw arrays.
 
P

pcnate

Here is my object instance declaration

Books book[100];

I know I don't want to use vectors right now, I just need to learn how
to point to this in a way to change my values
 
A

Alf P. Steinbach

* (e-mail address removed):
Here is my object instance declaration

Books book[100];

I know I don't want to use vectors right now, I just need to learn how
to point to this in a way to change my values

Well, if you don't want the easy, professional way, but rather the
difficult, unprofessional way, i.e. as many hurdles and problems and
bugs as possible, there's not much advice to give except delve into it
and try the most difficult and hard to grok you can think of first: that
should presumably give you enough problems.

Otherwise, replace that declaration with

std::vector<Book> books( 100 );

Note the placement of the "s": the type Book describes a single book
(not plural), wheras the variable is a collection of book (plural).

Now you can write e.g.

std::cout << books.at(3).title << std::endl;

And you'll discover that that magic number 100 is completely
meaningless, because the vector, as opposed to the raw array, can be
very easily resized to accomodate any number of books.
 
B

benben

here is the func with lines in question:
swapElems(Books *x, int startscan, int minIndex)
{
Books temp = { "", "", "", "", 0.0f, 0 };

temp = *x[startscan];
*x[startscan] = *x[minIndex];
*x[minIndex] = temp;
}

x[n] actually is a Book not a pointer, so you don't need to dereference
it. That is, instead of writing

temp = *x[startscan];

you should have written:

temp = x[startscan];

The rationale is x[n] is always interpreted as *(x+n), which is in
itself a dereferencing operation.

Regards,
Ben
 

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

Forum statistics

Threads
474,263
Messages
2,571,061
Members
48,769
Latest member
Clifft

Latest Threads

Top