0

I have a struct called Product in which I have a variable called prodname:

char prodname[30];

The instance of the struct is called product

I wrote a method to check if the name entered by the user, is unique or not. In this method, I pass the value entered by the user to the method called checkprodname(char n[30])

In the main method

if(checkprodname(prodName) == 0)
    {
        gotoxy(21,13);
        printf("The Product Name that you have entered already exists.");
        getch();
        addproduct();
        return 0;
    }

Then after this I have this line of code:

product.prodname = prodName;

In order to assign the value in the temp variable prodName into the actual struct. Of course I will them move on to save all this into the file. But till now I am getting an error since this is the error I'm getting:

incompatible types when assigning to type char[30] from type char**

I already used the same logic for the prodid which worked; however when using the string, I have no idea how to arrive to the actual assigning of the value into the actual struct since I'm getting that error.

Any help would be appreciated.

2 Answers 2

1

If you are in C++, consider replacing the char arrays with a std::string. Not that it makes all that much difference, but std::strings are usually easier to use. (IMHO)

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

1 Comment

An afterthought. If you store the product structures in a std::map<std::string, Product> where the std::string is prodname (string-ified), you can easily check for the existence of a mapped member.
0

You can't assign arrays in C using that notation. For character strings, use strcpy(); for other arrays, use memmove() or memcpy(). In all cases, make sure there's enough space in the target to store what is in the source.

Normally, I'd expect to write:

strcpy(product.prodname, prodName);

Given the compilation warning you're getting, it appears you need to use:

strcpy(product.prodname, *prodName);

Given the discussion in the comments below, it appears that the first alternative was correct after all, but I think that means the compilation error applies to a different line, not the assignment that was shown.

3 Comments

with the *prodname doesnt work. It seems to work without the *.
OK; then I don't understand where the compiler error was coming from. Without the * is what I first expected (it is the usual way of writing it), but I assumed that the assignment error applied to the assignment you showed. AFAICS, given your comment that *prodname does not work, the error can't apply to that line, but we don't have enough program to be sure.
btw I'm in C not in c++, Wes Miller

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.