Jason247
Jason247

Reputation: 1024

Assignment of struct char array to char array in C is giving an "incompatible types in assignment" error

I am trying to assign a char array of size 88 to a structure's char array of size 88 property, but when I compile the program, I get:

error: incompatible types in assignment

Here is where I define my struct:

#define BUFFERSIZE 9
/* Bounded Buffer item structure */
typedef struct item {
   int  id;       /* String index value */
   char str[88];  /* String value */
}item;

I declare the item as a global variable:

item buffer[BUFFERSIZE];
int bufferFill;

I am setting the property in another file, so I declare them in the other file as well using extern:

extern item buffer[BUFFERSIZE];
extern int bufferFill;

Inside of a function in the same file that I redeclared them as external global variables, I declare a new char array:

char outputString[88];

I originally had it as:

char * outputString; 

That gave the same error that I am still getting, so I tried changing it so that the array was declared with the same size as the one in the struct.

After I fill outputStrings with data, I try to set the buffer's str array to outputStrings.

buffer[bufferFill].str = outputString;

This gives the error:

incompatible types in assignment during compile time.

I have no idea what is wrong. I have even done things similar to this before with no problems, so I don't understand what I'm doing wrong. I have set the other property of the item struct with no problem like this:

buffer[bufferFill].id = num;  // This gives no error

In setting the str property I have also tried:

buffer[bufferFill].str = (char *)outputString;

and

buffer[bufferFill].str = &outputString;

I didn't expect those to work, but I tried them anyway. They of course did not work.

I am completely lost as to what could be causing this when as far as I can tell, they are the same type.

Upvotes: 0

Views: 3077

Answers (2)

aaa
aaa

Reputation: 499

Use strcpy() or strncpy() for copying strings (which are char arrays).

Upvotes: 1

Siddhartha Ghosh
Siddhartha Ghosh

Reputation: 3198

To copy character strings, use:-

strncpy(buffer[bufferFill].str,outputString,88); //88 is the size of char str[88] array.

Upvotes: 2

Related Questions