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

content of a specific location in a string

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



Joined: 05 Jan 2006
Posts: 105

View user's profile Send private message

content of a specific location in a string
PostPosted: Sat Jun 17, 2006 2:29 am     Reply with quote

hi all
i need to know a content of a specific location in some string i have recieved from rs-232 port.
for example i need to know whats in S[10] in location 10,,so how to do?

thanx
Ttelmah
Guest







PostPosted: Sat Jun 17, 2006 3:09 am     Reply with quote

In C, there is a 'cross correlation', between what are called 'pointers', and 'arrays'. If (for instance), you have:

char str[20];

//and then call:

gets(str);

//which sends the 'pointer' to 'str', to the function, then after the data is retrieved:

str[0] will contain the first character received.

So, 'strings', are just arrays of characters (with a '0' in the last location used), and can be accessed either way at will. Using the 'pointer' notation:

*(str+10)

will be the 'contents' of the _eleventh_ character in the string. Remember that throughout C, things arrays, pointers etc., are 'zero referenced', so a ten element string (str[10]), has elements str[0] to str[9]. Also that for a string, there must be a terminating '0', so only nine useable characters can actually be held in such a ten element array.
Forgetting that you need this 'extra' character, and the use of the zero location, are the two commonest problems with strings in C.

Best Wishes
garage
Guest







PostPosted: Sat Jun 17, 2006 11:41 pm     Reply with quote

please if you have a simple example code of this!!
thank you
hadeelqasaimeh



Joined: 05 Jan 2006
Posts: 105

View user's profile Send private message

PostPosted: Sat Jun 17, 2006 11:42 pm     Reply with quote

thanx
Mark



Joined: 07 Sep 2003
Posts: 2838
Location: Atlanta, GA

View user's profile Send private message Send e-mail

Re: content of a specific location in a string
PostPosted: Sun Jun 18, 2006 9:00 pm     Reply with quote

hadeelqasaimeh wrote:
hi all
i need to know a content of a specific location in some string i have recieved from rs-232 port.
for example i need to know whats in S[10] in location 10,,so how to do?

thanx


The 10th character in a string is s[9].
hadeelqasaimeh



Joined: 05 Jan 2006
Posts: 105

View user's profile Send private message

so....
PostPosted: Mon Jun 19, 2006 12:08 am     Reply with quote

hi
i understand that
Quote:
so a ten element string (str[10]), has elements str[0] to str[9].

from first reply

now mark said
Quote:
The 10th character in a string is s[9].


i think this different!!!!so what to do guyes? Shocked
if there is some example
asmallri



Joined: 12 Aug 2004
Posts: 1636
Location: Perth, Australia

View user's profile Send private message Send e-mail Visit poster's website

PostPosted: Mon Jun 19, 2006 12:31 am     Reply with quote

assuming the following:
Code:

        char Mesg[20];
   sprintf(Mesg, "Hello, world");



the line
Code:

      printf("%c", Mesg[5])

will print the , character

the line
Code:

       printf("%s", &Mesg[3]);

will print the message
lo, world

Corrected - thanks Mark for spotting that deliberate error to test who was awake :-)
_________________
Regards, Andrew

http://www.brushelectronics.com/software
Home of Ethernet, SD card and Encrypted Serial Bootloaders for PICs!!


Last edited by asmallri on Mon Jun 19, 2006 6:43 pm; edited 1 time in total
Mark



Joined: 07 Sep 2003
Posts: 2838
Location: Atlanta, GA

View user's profile Send private message Send e-mail

PostPosted: Mon Jun 19, 2006 7:53 am     Reply with quote

asmallri wrote:
assuming the following:
Code:

        char Mesg[20];
   sprintf(Mesg, "Hello, world");



the line
Code:

      printf("%c", *Mesg[5])

will print the , character

the line
Code:

       printf("%s", &Mesg[3]);

will print the message
lo, world


I think you meant to write

Code:

      printf("%c", Mesg[5])
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