CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

addresses of a string's elements

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
rikotech8



Joined: 10 Dec 2011
Posts: 376
Location: Sofiq,Bulgariq

View user's profile Send private message

addresses of a string's elements
PostPosted: Sun Nov 18, 2012 6:27 am     Reply with quote

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: 19538

View user's profile Send private message

PostPosted: Sun Nov 18, 2012 6:47 am     Reply with quote

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

View user's profile Send private message

PostPosted: Sun Nov 18, 2012 8:52 am     Reply with quote

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: 19538

View user's profile Send private message

PostPosted: Sun Nov 18, 2012 10:21 am     Reply with quote

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

View user's profile Send private message

PostPosted: Mon Nov 19, 2012 12:50 pm     Reply with quote

Mhm.. I didn't know that. Thank you again!
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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