0

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.

1
  • 1
    Arrays are not assignable Commented Oct 28, 2013 at 4:04

2 Answers 2

2

To copy character strings, use:-

strncpy(buffer[bufferFill].str,outputString,88); //88 is the size of char str[88] array.
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you so much! That did the trick. I do want to note for anyone looking at this for help with their own issues that it should be strncpy(buffer[bufferFill].str,outputString,88); because the size of both arrays is 88, BUFFERSIZE = 9.
@Jason247: You are correct. It should be 88 (i.e. the size of char str[] array), not BUFFERSIZE. Would edit the same.
1

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

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.