473,830 Members | 2,031 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

problems with pointers and structs

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.

May 18 '06 #1
4 1368
* pc****@gmail.co m:
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<Boo k> books;

or

std::vector<Boo k> 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.
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
May 18 '06 #2
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

May 19 '06 #3
* pc****@gmail.co m:
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<Boo k> 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).tit le << 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.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
May 19 '06 #4
> 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
May 20 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2175
by: Christian F | last post by:
Hi, I'm a C-newbie and I would like to know if I am doing something wrong in the code below. It is working, but I'm afraid it might not be correct because I don't really understand everything of it. There are lots of pointers and pointers to pointers which makes me confused. First my typedef: typedef struct { double re;
8
448
by: John Hanley | last post by:
I working in C. I haven't paid much attention to void pointers in the past, but I am wondering if I can use them for my various linked lists to save work. I have two different linked lists that each use nodes of different structs. However the principle of my adding to the linked lists and removing them is the same. So will it work to have these functions accept a void pointer as an argument for the list and a void pointer as an...
42
2187
by: x-pander | last post by:
Is is guaranteed, that a pointer to any object in C points exactly at the lowest addressed byte of this object? Specifcally is it possible for any platform/os/compiler combination that: (char *)v != (void *)v where v is an int variable for example. If so, any real-life examples?
47
2672
by: sunglo | last post by:
Some time a go, in a discussion here in comp.lang.c, I learnt that it's better not to use a (sometype **) where a (void **) is expected (using a cast). Part of the discussion boiled down to the rule: if I cast a (sometype **) to a (void **) I am making a number of assumptions about the implementation's (void **) representation and length. Specifically, if I do the above cast I'm assuming that a (sometype **) and a (void **) have the same...
5
3136
by: Paminu | last post by:
Why make an array of pointers to structs, when it is possible to just make an array of structs? I have this struct: struct test { int a; int b;
17
3482
by: Johan Tibell | last post by:
Could someone outline the pros and cons of typedefing pointers to structs like this? typedef struct exp_ { int val; struct exp_ *child; } *exp; (This is straight from memory so it might not even compile.)
64
3443
by: Zytan | last post by:
I know there are no pointers in C#, but if you do: a = b; and a and b are both arrays, they now both point to the same memory (changing one changes the other). So, it makes them seem like pointers. Can someone please explain why? thanks. Zytan
5
2299
by: dev_15 | last post by:
Hi, I'm going through some code and thought that this allocates an array of structs but its supposed according to comments to allocate an array of pointer to structs. What does it actually do ptrLogArray = new structDisplayLogData *; // iCount = 50 structDisplayLogData is a strcuture of data Thanks
2
11945
by: hal | last post by:
Hi, I'm trying to make an array of pointers to 'TwoCounts' structs, where the size of the array is arraySize. Right now I'm just mallocing enough space for all the pointers to the structs, and mallocing space for the pointer 'countPtr' in each struct, but do I need to do anything else? Thanks. typedef struct TwoCounts { int *countPtr;
0
9781
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10769
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10477
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10522
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9310
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7740
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5615
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4408
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3072
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.