Pointer problems

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

Pointer problems

2,716件の閲覧回数
NZ_Design
Contributor I
I have used the ICC compiler in the past and are changing my code over into CW.
 
One line I are haveing trouble with it is one I use all the time.
 
I have a string decleared
 
#define S_TABLANE      "LANE\0"
#define S_TABOUTLET    "OUTLET\0"
#define S_TABSIZE      "SIZE\0"
#define S_TABAVGSIZE   "PACKS\0"
I then have an array that points to each of these strings.
 
const char* TabName[] = {S_TABLANE,S_TABOUTLET,S_TABSIZE,S_TABAVGSIZE};
in my old code I accessed this by doing this
 
gputs(TabName[cTab]);
 
That would display the required string this on longer is the case. The screen displays rubish and it is in a never ending loop. Never finds the end of the string.
 
To get round it I did a switch case statement which works
 
   switch (cTab) {
     case 0:
       gputs(S_TABLANE);
       break;
     case 1:
       gputs(S_TABOUTLET);
       break;
     case 2:
       gputs(S_TABSIZE);
       break;
     case 3:
       gputs(S_TABAVGSIZE);
       break;
   }
why does CW not like my pointer array.
 

 
ラベル(1)
0 件の賞賛
返信
3 返答(返信)

776件の閲覧回数
CompilerGuru
NXP Employee
NXP Employee
This partial coded snipped you are showing looks ok, it should work with CW too.
Without a full compilable sample showing the problem it is impossible to say.
Some notes anyway:
>const char* TabName[]

This is a non constant array, therefore you have to have the startup code to initialize it properly. Do you have start12.c in your project? (or start08.c, if this is for the HC08).

Probably you should change this to:
const char* const TabName[]


so it would end up in flash and not use RAM.

>#define S_TABLANE      "LANE\0"


The ANSI-C standard adds a 0 byte at the end of every string literal for you. When you add one explicitly like this, you will end up with two 0 zero bytes at the end.

So if this does not help, the best would be to post a zip file with a complete project showing the problem. As the startup code note shows, the problem may be somewhere else than directly in the C file too.

Daniel
0 件の賞賛
返信

776件の閲覧回数
NZ_Design
Contributor I
Thanks for your help. That worked however the first const is not required as this brings up a warning.
 
 
0 件の賞賛
返信

776件の閲覧回数
CompilerGuru
NXP Employee
NXP Employee
The first const is not strictly necessary, but I would write it. It says that the string literals cannot be changed, and as the do by default end up in flash, that's just the truth....

Warnings about different const qualifiers are often caused by missing const qualifiers, not by having too many of them, at least this is my experience with existing code and const's.
Does the prototype of gputs contain a const?
void gputs(const char*);


Well, otherwise you would have to show the warning and the code generating it. But as it works now maybe that good enough too Smiley Happy

Daniel
0 件の賞賛
返信