May 3rd, 2005, 02:44 AM
|
#1 (permalink)
| | Member
Join Date: Aug 2004
Posts: 52
| Pointers to structure won't work in C
So, I'm trying to make a pointer to this structure "ProdInfo", however it gives me something about not being able to convert from "ProdInfo(*)[5] to ProdInfo*" and I'm wondering if it is the compiler included with knoppix 3.7, or if copying examples is simply futile. Code: // Soda machine sim
#ifdef HAVE_CONFIG_H
#include <config.h>
#include <coke.h>
#endif
#include <iostream>
#include <cstdlib>
#include <strings.h>
#include <iomanip.h>
using namespace std;
void display(char &);
int main(int argc, char *argv[])
{
struct ProdInfo
{
char name[20];
float cost;
int inv;
float profit;
};
ProdInfo pi[5] = {
{"Cola", .75, 20, 0},
{"Root Beer", .75, 20, 0},
{"Lemon Lime", .75, 20, 0},
{"Grape Soda", .80, 20, 0},
{"Cream Soda", .80, 20, 0}
};
int option;
ProdInfo *pip;
pip = π
cout << pi[0].name;
} It's my understanding that I must use a pointer to pass the entire structure array to another function, is this true? I've tried referencing it as a variable, array, and pointer and all have failed despite the pointer initialization.
This is the last time I take advise for "good" course in college -_-
Thanks in Advance. |
| |
May 3rd, 2005, 10:05 AM
|
#2 (permalink)
| | Ultimate Member
Join Date: Mar 2005 Location: Out of my mind
Posts: 2,792
|
Not sure what you want to do here...
If you want to display your data using the pointer, then you have to do this:
cout << pip->name << pip->cost; // etc, etc.
the "->" notation is used when you have a pointer to a structrue and want to dereference an item in it. To get to the next item in your list, add 1 to the pointer:
++pip;
If you want to use a function "display" in your program, you have to put that function into the structure and show your structure elements. I'll leave that up to you to try  |
| |
May 3rd, 2005, 10:12 AM
|
#3 (permalink)
| | Ultimate Member
Join Date: Mar 2005 Location: Out of my mind
Posts: 2,792
|
<edit> You have to set your pointer to the first element of the structure...
pip = &pi[0]; |
| |
May 3rd, 2005, 11:32 AM
|
#4 (permalink)
| | Member
Join Date: Aug 2004
Posts: 52
|
Yay for reading the book...it shows it as
pip = &pi[];
Thanks, I'd been googling like mad and just couldn't seem to find an answer. In the end I just made the stupid program one function, but I guess having a running program is worth a 5 point deduction as opposed to 30 for it not running heh.
edit -- Fixed 'up all night' typing.
Last edited by dystopia : May 3rd, 2005 at 11:39 AM.
|
| |
May 3rd, 2005, 11:47 AM
|
#5 (permalink)
| | Ultimate Member
Join Date: Mar 2005 Location: Out of my mind
Posts: 2,792
|
Glad you got it working.
What is the name of the class? In college?
Getting into structures and then onto classes (same as a structure) is the first step into Object Oriented Programming. Way fun stuff!!  |
| |
May 3rd, 2005, 11:53 AM
|
#6 (permalink)
| | Member
Join Date: Aug 2004
Posts: 52
|
Programming Fundamentals 1 - C++
It's been whipping me all semester because I haven't had time to practice enough. Between intense studies of the Japanese language (in my free time no less) and two paper crazy professors, time has just been too sparse.
I'll probably continue into object oriented stuff during the summer since the book covers it, and I'll be able to practice at my own rate, actually compiling something of even moderate use to myself would be neat. At least with linux I can use it unlike the educational version of visual studio we have heh. |
| | |
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | | |
Posting Rules
| You may post new threads You may post replies You may not post attachments You may not edit your posts HTML code is Off | | | | Most Active Discussions | | | | | Recent Discussions  | | | | | |