Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Unsigned Char Error
06-09-2013, 06:56 AM,
#1
Unsigned Char Error
I can't figure out what is wrong with this... maybe somebody can help me.
Here are two global variables:
unsigned char serial_buffer[80], currNW[2], homeNW[2];

Later:
currNW[0] = serial_buffer[28]; currNW[1] = serial_buffer[41];
homeNW = currNW;

However the last line always gives the following error:
error: incompatible types when assigning to type 'unsigned char[2]' from type 'unsigned char *'

Have I done something wrong? I didn't find a report of a bug when I did a search of the forum.

Thanks,
- David.
Reply
06-09-2013, 08:06 AM, (This post was last modified: 06-09-2013, 08:08 AM by pingotg.)
#2
RE: Unsigned Char Error
It's bad (as in: not valid) C and should fail with every C compiler on every platform.

What are you wanting to do with that line? If you're trying to copy the contents of currNW into homeNW then that's not what it attempts. It's attempting to rename homeNW or something equally not allowed.

You access the contents using [ and ] as in the statements that work.

At a guess you want:
homeNW[0] = currNW[0];
homeNW[1] = currNW[1];

John
Reply
06-09-2013, 09:21 AM,
#3
RE: Unsigned Char Error
David, I strongly encourage you to consult a good (ANSI) C book. Understand arrays, pointers and how they are interconnected. I ensure you: your time won't be wasted. In general, you may think of an array NAME as pointer to the first element of that array. (NOT: a pointer VARIABLE -as there is none- but rather the mere memory address.) Given that, your assignment "homeNW = currNW;" would be transscripted into something like "&(homeNW[0]) = &(currNW[0]);" - which explains the error message. In C, you cannot copy arrays with a simple assignment.

The tricky thing is: with structs (rather than arrays), your assignment would have been valid and functional C code (implicitly using sort of memcpy). Therefore it is even possible to pass structs "by value": as function arguments or return values (see for instance div/ldiv, which return a struct). This different treatment of arrays and structs (since both are compound types) is quite arbitrary and sometimes confusing. I assume the creators of C wanted to encourage the use of lightweight structs, like complex numbers. But on tiny embedded systems with little RAM and very limitted stack resources, reconsider the use of pointers (unless the struct is VERY small; say, about the footprint of a pointer variable)

Frank
Reply
06-09-2013, 10:15 PM,
#4
RE: Unsigned Char Error
Many thanks to both of you. I am not a programmer, but I have a C book somewhere in the house... sorry for being a pain in the neck and grateful for your patience.
- David.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)