0

Hi friends in my following program of c++. I am passing array of structure to function but getting errors in it. Can anybody help please. There are following erros in the program.

1.Constant expression required. 2. Illegal structure operation. 3. Declaration syntax error.

#include<iostream.h>
#include<conio.h>
void calc_price(struct c_items);
struct c_items
{
float name[80];
int quantity;
int price;
};
int main(void)
{
int num_items=3;
c_items obj[num_items]
calc_price(obj,num_items);
getch();
return 0;
}
void calc_price(c_items obj[],int num_items)
{
int i,n,total=0;
cout<<"Enter number of items = ";
cin>>n;
for(i=1;i<=num_items;i++)
{
cout<<"Enter name of item"<<i<<" = ";
cin>>obj[i].name;
cout<<endl<<"Enter price of item"<<i<<" = ";
cin>>obj[i].price;
cout<<endl<<"Enter quantity of item"<<i<<" = ";
cin>>obj[i].quantity;
}
cout<<endl<<endl<<"Retail value of inventory ";
for(i=1;i<=5;i++)
{
cout<<endl<<obj[i].name<<i<<"   "<<obj[i].price<<"$";
total=total+obj[i].price;
}
cout<<"Total retail value = "<<total<<"$";
}
3
  • 1
    please post the errors as well as the code Commented Apr 10, 2014 at 4:47
  • please try this code by calc_price(c_items* obj,int num_items) Commented Apr 10, 2014 at 4:50
  • Hi RobP, Question is updated Commented Apr 10, 2014 at 4:54

4 Answers 4

2

In C++ and C arrays are passed by address (pointer) rather than by value. This has the side effect that the receiving function does not know how large the arrays are, even if you shape the argument to tell it.

C++ inherits something called "array-pointer equivalence" from C, which means that you can write

struct foo;
void fn(foo f[25]);

but that's just syntactic sugar. The compiler actually thinks you wrote:

void fn(foo * f);

(You will see some people write foo* f, some write foo *f and some write foo f[]; they all mean the same thing as the line above)

Here is a simple, complete, example of passing an array of structs (live demo http://ideone.com/qc4t53)

#include <cstddef>

struct Foo
{
    int m_i;  // 'm_' to distinguish a member variable.
};

int structFunc(Foo* foos, size_t numFoos);

int main()
{
    const size_t NumFoos = 10;
    Foo mainFoos[NumFoos];

    int i = structFunc(mainFoos, NumFoos);

    return 0;
}

int structFunc(Foo* foos, size_t numFoos)
{
    int retVal = 0;
    for (size_t i = 0; i < numFoos; ++i) {
        foos[i].m_i = i;
        ++retVal;
    }
    return retVal;
}

More on array-pointer equivalence, from the C FAQ

Sign up to request clarification or add additional context in comments.

4 Comments

Note that in C++ you can also pass arrays by reference, but this is only practical if you only have one size of array, or with a template function where the size is a template parameter.
+1 for explaining foo(bar*) and foo(bar[])/foo(bar[<n>] are equivalent.
I wouldn't call it "array-pointer equivalence" ; that suggests that arrays and pointers are equivalent when they are not. You are describing a syntax quirk that exists only in a function's formal parameter list.
@MattMcNabb see the link to the C faq. That's what it is called, and it exists outside of parameter lists. a[n] and *a+n are equivalent whether a is T a[...] or T * a. As the faq points out, equivalent has a specific, subtle meaning.
0

Should be:

#include <iostream>
#include <string>
using namespace std;    // if you must

struct c_items
{
    std::string name;
    int quantity;
    int price;
};

void calc_price( c_items obj[], int num_items );

int main()
{
    const int num_items = 3;
    c_items obj[num_items];
    calc_price( obj, num_items );
    return 0;
}

void calc_price( c_items obj[], int num_items )
{
// etc.

In the calc_price function you need to be more careful with your array indices. The valid indices are 0 through num_items - 1, not 1 through num_items. And 5 is outside this range.

Comments

0

Second line of main should be struct c_items obj[num_items];

Declaration of void calc_price(struct c_items); doesn't match the definition/implementation void calc_price(c_items obj[],int num_items)

2 Comments

The second line of main is fine in that aspect, this being C++, but not in that it's using a VLA.
After modifying the declaration it is still not working
0

you have five problems preventing the code from compiling:

  1. float name[ should really be char name[, otherwise the operator>> is not defined;
  2. you missed a ";" in the main;
  3. num_items in the main must be const (since you are using it for telling the compiler how long an array is);
  4. there is no (valid) forward declaration of the function calc_price (i.e. void calc_price(struct c_items); should become void calc_price(c_items obj[],int num_items);
  5. there is no forward declaration of the struct c_items when you first mention it in line 3.

Besides this, there is a number of different issues, the most important of which is: you must check, in calc_price, that the cin-ned n is no bigger than num_items

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.