I'm using a Keil C51 compiler to program a 8051 microcontroller. For some reason my code didn't run - I managed to track down the bug, but I still have difficulties understanding it. Why is the first code wrong, comparing to the other one? It's worth noting that the compiler didn't throw any error, the code just didn't even start on the microcontroller.
Wrong code:
file1.h
extern STRUCT_TYPEDEF array_var[];
file2.c
// Global variable initialization
STRUCT_TYPEDEF array_var[] = some_struct.array2_var;
After changing these to:
file1.h
extern STRUCT_TYPEDEF *array_var;
file2.c
// Global variable initialization
STRUCT_TYPEDEF *array_var = &some_struct.array2_var[0];
it started working.
Also, this portion of code was referenced only in functions like "array_var[0].property = ...", but none of these functions were ever called from the application.
some_struct variable is declared in yet another module.
Why could it behave like that? Is there some difference between [] and * I don't know about?
EDIT1: It is said that pointers and arrays are different things... but then, how does the "[]" syntax differ from "*"? I thought compiler would just convert it to a pointer in case the square brackets are empty (like it does with the function arguments). I also thought providing an array would result in giving me the address of the first element.
Now, everyone is saying pointers and arrays are different - but I can't find any information about what exactly is different in them. How does compiler see it when I give an array as rvalue instead of a pointer to its first element?
is not a valid way to initialize an array in a declaration. An array initializer must be a brace-enclosed list of initializers, such as
You cannot initialize an array with another array1, nor can you assign one array to another this way:
If you want to copy the contents of
some_struct.array2_vartoarray_var, you must use a library function likememcpy:You must also declare
array_varwith a size; you can't leave it incomplete if you want to use it. If you know ahead of time how big it needs to be, it's easy:If you don't know ahead of time how big it needs to be, then you'll either have to declare it as a variable-length array (which won't work if it needs to be at file scope or otherwise have
staticstorage duration), or you can declare the memory dynamically:This all assumes that
some_struct.array2_varis an array declared likeIf it's also just a pointer, then you'll have to keep track of the array size some other way.
EDIT
If you want
array_varto simply point to the first element ofsome_struct.array2_var, you'd do the following:Except when it is the operand of the
sizeofor unary&operators, an expression of type "N-element array ofT" will be converted ("decay") to an expression of type "pointer toT", and the value of the expression will be the address of the first element of the array. The code above is exactly equivalent tochar message[] = "Hello";; the string literal"Hello"is an array expression, but the language treats it as a special case.