|
|
View previous topic :: View next topic |
Author |
Message |
rikotech8
Joined: 10 Dec 2011 Posts: 376 Location: Sofiq,Bulgariq
|
addresses of a string's elements |
Posted: Sun Nov 18, 2012 6:27 am |
|
|
Hi! How can I know what is the address of each element of string.
For example:
Code: |
const char str[]={1,2,3,4};
|
So, what is the address of str[0],str[1]....ect?
I tried with &str but I get garbage. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19499
|
|
Posted: Sun Nov 18, 2012 6:47 am |
|
|
With a normal string.
str, _is_ the address of the first element.
str+1 the address of the second, etc. etc.....
However this will not work with const strings. You have to get your head round the fact that in the PIC, there are two separate memory spaces. So there are two sddress 0's, etc... One in the ROM, and one in RAM.
If you change your declaration to:
Code: |
rom char str[]={1,2,3,4};
|
This tells the compiler to use a (more bulky) way of storing the const strings, and construct 'virtual' pointers to these.
In the original 'const' form:
addressof(str);
will return the ROM address where str is stored, _but_ you then need to access this with the 'read_program_memory' function, not using *. (* handles accessing RAM, not ROM).
&str, would always be garbage in any version, since it will give the address of the variable 'str', not it's contents.....
Best Wishes |
|
|
rikotech8
Joined: 10 Dec 2011 Posts: 376 Location: Sofiq,Bulgariq
|
|
Posted: Sun Nov 18, 2012 8:52 am |
|
|
You are saying that declaring constant variable doesn't take RAM but ROM. I tried with new declaration
Code: | char str[]={1,12,2}; |
and then I succeed to read the address of str by using '&', so thank you, I see now. I don't know if you understand me. Excuse my bad englisnh! |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19499
|
|
Posted: Sun Nov 18, 2012 10:21 am |
|
|
Yes, you have it.
Remember also, you don't need '&'. In C, the name of an array, is it's address. So:
str, is exactly the same as &str[0]
This is a standard C 'shortcut'.
Best Wishes |
|
|
rikotech8
Joined: 10 Dec 2011 Posts: 376 Location: Sofiq,Bulgariq
|
|
Posted: Mon Nov 19, 2012 12:50 pm |
|
|
Mhm.. I didn't know that. Thank you again! |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|