1

I'm trying to swap an array of structs, and I thought following a similar fashion of storing in a temp would work like this:

int temp ,a, b;
temp = a;
a = b;
b = temp;

My array of struct definition is this:

struct storage data[10];

My attempt to swap an array of structs, I tried this:

struct storage temp[1];
temp = data[1];
data[1] = data[2];
data[2] = temp;

Unfortunately, it doesn't compile

My errors are below:

error #2168: Operands of '=' have incompatible types 'struct storage[1]' and 'struct storage'.

error #2088: Lvalue required.

error #2168: Operands of '=' have incompatible types 'struct storage' and 'struct storage*'.

2 Answers 2

5

In C arrays aren't modifiable lvalues. Drop the [1] and you're set:

struct storage temp;
Sign up to request clarification or add additional context in comments.

Comments

1

You are trying to hold a struct storage that you dereferenced when you said

temp = data[1];

You need to declare your temp variable as such to hold the dereferenced values from the array

struct storage temp;

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.