Problem with sizeof() when using external arrays.

キャンセル
次の結果を表示 
表示  限定  | 次の代わりに検索 
もしかして: 

Problem with sizeof() when using external arrays.

ソリューションへジャンプ
1,223件の閲覧回数
perhojfeldt
Contributor III

Is it something I totally misunderstand. To figure out the size of a const char array I use sizeof(array)-1. Works fine when array is defined in the file where sizeof is used but when used as a external in a header file I get an error. Ide/compiler is Codewarrior version 11.0. I attached the project with the error.

Thanks

Per Højfeldt

Without error.

pastedImage_2.png

With error.

pastedImage_1.png

0 件の賞賛
返信
1 解決策
1,111件の閲覧回数
perhojfeldt
Contributor III

I think I figured out the problem. Use of sizeof() can not be used with an external because the compiler at compile time do not know the size of an external array. Correct me if I'm wrong. Make sense. The solution to the problem is to make a function with sizeof() where the array is defined and then make the function an external. That should work.

 Example that can be declared external.

signed char const caArray[] = "1234";

unsigned char arrsize(void)

{
    return sizeof(caArray)-1;
}

Per Højfeldt

元の投稿で解決策を見る

0 件の賞賛
返信
1 返信
1,112件の閲覧回数
perhojfeldt
Contributor III

I think I figured out the problem. Use of sizeof() can not be used with an external because the compiler at compile time do not know the size of an external array. Correct me if I'm wrong. Make sense. The solution to the problem is to make a function with sizeof() where the array is defined and then make the function an external. That should work.

 Example that can be declared external.

signed char const caArray[] = "1234";

unsigned char arrsize(void)

{
    return sizeof(caArray)-1;
}

Per Højfeldt

0 件の賞賛
返信