-
March 20th, 2008, 06:56 PM
#1
Pointing to a Structs variables.
Hi!
I am trying to write a function which manipulates the variables of a struct using pointers, but am having trouble with the syntax.
I am still at the start with is, and so am just trying to get the program to compile before adding any looping or arguments.
Code:
#include <iostream>
#include <fstream>
using namespace std;
struct items
{
char itemname[10];
double price;
bool luxury;
};
void FunctionsName();
int main()
{
items StructArrayName[10];
ifstream StreamName;
StreamName.open("myfile2.txt");
if(!StreamName)
{
cout << "phail " << endl;
}
for(int i = 0;i < 10; i++)
{
StreamName >> StructArrayName[i].itemname >> StructArrayName[i].price >> StructArrayName[i].luxury;
}
StreamName.close();
system ("pause");
return 0;
}
//////////////Compile errors section////////////////////////
void FunctionsName()
{
char *pointernameitemname;
pointernameitemname = &StructArrayName[i].itemname;
double *pointernameprice;
pointernameprice = &StructArrayName[i].price;
bool *pointernameluxury;
pointernameluxury = &StructArrayName[i].luxury;
}
/////////////////////////////////////////////////////////
If anyone could assist my with the correct syntax, it would be appreciated.
Thanks!
-
March 20th, 2008, 07:23 PM
#2
Re: Pointing to a Structs variables.
Originally Posted by Swerve
Hi!
I am trying to write a function which manipulates the variables of a struct using pointers, but am having trouble with the syntax.
Before doing that, you should learn about variable scope.
You declared a structure StructArrayName[10] within the main() function, but you're trying to access it in FunctionNames(). Variables declared within functions have local scope, and cannot be accessed by other functions unless you pass the address or reference to that variable to the function in question.
Code:
SomeFunction()
{
x = 10;
}
int main()
{
int x;
}
This is basically what you're trying to do, which is illegal.
Regards,
Paul McKenzie
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|