home hardware prices news articles forums photos user reviews
Go Back   Tech Support Forums - TechIMO.com > PC Hardware and Tech > Webmastering and Programming
Join TechIMO for Free!
Register Blogs FAQ Members List Calendar Search Today's Posts Mark Forums Read
Reply Get bargains at  »  Dealighted.com
 
Thread Tools
Currently Active Users: 1705
Discussions: 188,402, Posts: 2,243,609, Members: 232,632
Old May 3rd, 2005, 02:44 AM   Digg it!   #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 = &pi;

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.

dystopia is offline   Reply With Quote
Old May 3rd, 2005, 10:05 AM     #2 (permalink)
Ultimate Member
 
Rootstonian's Avatar
 
Join Date: Mar 2005
Location: Out of my mind
Posts: 2,792
Send a message via AIM to Rootstonian
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

Rootstonian is offline   Reply With Quote
Old May 3rd, 2005, 10:12 AM     #3 (permalink)
Ultimate Member
 
Rootstonian's Avatar
 
Join Date: Mar 2005
Location: Out of my mind
Posts: 2,792
Send a message via AIM to Rootstonian
<edit> You have to set your pointer to the first element of the structure...

pip = &pi[0];

Rootstonian is offline   Reply With Quote
Old 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.
dystopia is offline   Reply With Quote
Old May 3rd, 2005, 11:47 AM     #5 (permalink)
Ultimate Member
 
Rootstonian's Avatar
 
Join Date: Mar 2005
Location: Out of my mind
Posts: 2,792
Send a message via AIM to Rootstonian
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!!
Rootstonian is offline   Reply With Quote
Old 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.
dystopia is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may post new threads
You may post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
MSCE Exam structure posty Certification and Education 0 January 17th, 2005 06:53 AM
MSCE Exam structure posty Certification and Education 0 January 17th, 2005 06:53 AM
Structure questions skybolt_1 Webmastering and Programming 1 May 8th, 2003 03:16 PM
printing directory structure in XP Gomer General Tech Discussion 4 August 15th, 2002 01:24 AM
Case structure questions? Knothead General Tech Discussion 17 March 13th, 2002 05:13 PM

Most Active Discussions
Is It Just Me? (2906)
3-days in and no threads about Gaza (161)
Misery Loves Company... (2144)
New Build ( Finally ) (7)
CPU wont boot (7)
Building a gaming computer advice (5)
I think I just killed my computer w.. (24)
RCA 52Inch HDTV wont turn on (5)
Folderchat Weekday thread (444)
Recent Discussions
Futronix has water features? (0)
Laptop proccesor to desktop mob.. (2)
Please help! multiple problems! (4)
RCA 52Inch HDTV wont turn on (5)
New Build ( Finally ) (7)
Common Spyware Solutions (97)
How do you move a hard-drive to.. (4)
What is the best external enclo.. (0)
Partition Magic 7.0 (Unallocate.. (17)
Blackberry Storm, Gears of War .. (1)
Core 2 Quad Q9550 system (3)
COWBOOM Ripoff! Used Laptop w/$.. (4)


All times are GMT -4. The time now is 04:46 AM.
TechIMO Copyright 2008 All Enthusiast, Inc.



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28