EmbDev.net

Forum: ARM programming with GCC/GNU tools Easy (I hope) C const pointer question


von jrmymllr j. (jrmymllr)


Rate this post
useful
not useful
I have this array:
1
const unsigned char *param_tag[]=        
2
{
3
  "SMSH=",
4
  "SMIN=",
5
  "SMUS=",
6
  "SMPA=",
7
  "SMDO=",
8
  "SYIP=",
9
  "SYNE=",
10
  "SYGA=",
11
  "SYDN=",
12
  "SYIM="
13
};

I had thought that the const would put everything into Flash, however 
according to the .map file it appears that the strings are in Flash, but 
the pointers to each of the 10 strings are being placed into RAM.  It's 
using 40 bytes of RAM: 32 bit micro, 4bytes * 10 strings = 40.

How do I put everything into Flash?  I'm not even sure what to search 
for to answer this myself.  My guess is the answer is easy if you know!

von (prx) A. K. (prx)


Rate this post
useful
not useful
A pointer to a constant ist not a constant pointer.

const unsigned char *const param_tag[] = ...

von jrmymllr j. (jrmymllr)


Rate this post
useful
not useful
A. K. wrote:
> A pointer to a constant ist not a constant pointer.
>
> const unsigned char *const param_tag[] = ...

Great, that fixed it!

von Rolf Magnus (Guest)


Rate this post
useful
not useful
> I have this array:
1
const unsigned char *param_tag[]=        
2
{
3
  "SMSH=",
4
  "SMIN=",
5
  "SMUS=",
6
  "SMPA=",
7
  "SMDO=",
8
  "SYIP=",
9
  "SYNE=",
10
  "SYGA=",
11
  "SYDN=",
12
  "SYIM="
13
};

Do you actually need the pointers? If all arrays are the same size, you 
can also make it an array of arrays and save the pointers.
1
const unsigned char param_tag[][6]=
2
{
3
  "SMSH=",
4
  "SMIN=",
5
  "SMUS=",
6
  "SMPA=",
7
  "SMDO=",
8
  "SYIP=",
9
  "SYNE=",
10
  "SYGA=",
11
  "SYDN=",
12
  "SYIM="
13
};

Btw: Why unsigned char? There is no such thing as signedness for 
characters, so if those are strings, the element type should be char.

von jrmymllr j. (jrmymllr)


Rate this post
useful
not useful
Rolf Magnus wrote:

>
> Do you actually need the pointers? If all arrays are the same size, you
> can also make it an array of arrays and save the pointers.

That's true.  I have another array similar to this one, but the strings 
are different sizes, so I sort of copied and pasted and modified.  But 
good point.

>
> Btw: Why unsigned char? There is no such thing as signedness for
> characters, so if those are strings, the element type should be char.

The only answer I can give is because I'm a novice :)

I've occasionally wondered about this but just kept going as long as it 
worked and I had no warnings.  After initially shunning C, I decided to 
teach it to myself and have learned the hard way.  I was one of those in 
college who said "I don't want to do any programming".  Now I use all of 
it for hobbies and none for my real job.

Please log in before posting. Registration is free and takes only a minute.
Existing account
Do you have a Google/GoogleMail account? No registration required!
Log in with Google account
No account? Register here.