Jurgen Cuschieri
Jurgen Cuschieri

Reputation: 736

char array write to file

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.

Upvotes: 0

Views: 154

Answers (2)

Wes Miller
Wes Miller

Reputation: 2241

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)

Upvotes: 1

Jonathan Leffler
Jonathan Leffler

Reputation: 753595

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.

Upvotes: 0

Related Questions