| Details | Message |
|---|
Read-Only Author sanat sharma Posted 19-Mar-2010 10:36 GMT Toolset C51 |  Error : non-address/-constant initializer sanat sharma
Why below line of code is not compiling successfully?
const unsigned char *a[]={
"Hello",
"Hi"
};
const unsigned char *b[]={
a[0],
a[1]
};
|
|
Read-Only Author Mike Kleshov Posted 19-Mar-2010 11:46 GMT Toolset C51 |  RE: Error : non-address/-constant initializer Mike Kleshov The initializer for the array b should consist of address constants. Here is what C90 says about them:
An address constant is a pointer to an lvalue designating an object of static storage duration, or to a function designator. Address constants must be created explicitly by using the unary & operator, or implicitly by using an expression of array or function type. The array subscript [] and member access operators . and ->, the address & and indirection * unary operators, and pointer casts can be used to create an address constant, but the value of an object cannot be accessed by use of these operators.
It seems that Keil followed the standard to the letter here. The standard explicitly disallows the use of [] to access an object in an initializer, which you are trying to do in your code. |
|
Read-Only Author Andy Neil Posted 19-Mar-2010 12:05 GMT Toolset None |  About your code Andy Neil Was that code purely for the sake of illustration? If not, what are you actually trying to achieve? If you explain what you are actually trying to achieve, people may be able to suggest better ways to get there... |
|
Read-Only Author sanat sharma Posted 19-Mar-2010 12:32 GMT Toolset C51 |  RE: Error : non-address/-constant initializer sanat sharma
Thanks Mike
But I want to ask how can i achieve this type of decleration for array of pointers?Because i am having some same strings in different array of pointers and i have to put only one in my code and want to declare rest of string in the above manner if it is a common for rest of the set.
. |
|
Read-Only Author IB Shy Posted 19-Mar-2010 12:35 GMT Toolset C51 |  Wow, what a cool use of the tags IB Shy NOT. |
|
Read-Only Author Andy Neil Posted 19-Mar-2010 12:41 GMT Toolset None |  RE: how can i achieve this type of decleration Andy Neil That is the wrong question to ask. It would be far better to describe what you're actually trying to achieve - and then ask for suggestions as to how to do that! BTW: What on earth made you think that formatting your response as source code would be a good idea?? |
|
Read-Only Author Mike Kleshov Posted 19-Mar-2010 14:00 GMT Toolset None |  RE: how can i achieve this type of decleration Mike Kleshov Because i am having some same strings in different array of pointers and i have to put only one in my code and want to declare rest of string in the above manner if it is a common for rest of the set. This can be done, but each string must be put in a separate variable. By the way, since we are talking about C51 here, I noticed that you are not using the code keyword for your constant variables. This could be a mistake. |
|
Read-Only Author Andy Neil Posted 19-Mar-2010 14:23 GMT Toolset None |  Common Strings Andy Neil How about something like
const unsigned char str_hello[] = "Hello";
const unsigned char str_hi[] = "Hi";
const unsigned char *a[]={
str_hello,
str_hi"
};
const unsigned char *b[]={
str_hello,
str_hi"
};
And, as Kike said, consider putting them into CODE space... |
|