|
|
View previous topic :: View next topic |
Author |
Message |
hadeelqasaimeh
Joined: 05 Jan 2006 Posts: 105
|
content of a specific location in a string |
Posted: Sat Jun 17, 2006 2:29 am |
|
|
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
|
|
Posted: Sat Jun 17, 2006 3:09 am |
|
|
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
|
|
Posted: Sat Jun 17, 2006 11:41 pm |
|
|
please if you have a simple example code of this!!
thank you |
|
|
hadeelqasaimeh
Joined: 05 Jan 2006 Posts: 105
|
|
Posted: Sat Jun 17, 2006 11:42 pm |
|
|
thanx |
|
|
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
Re: content of a specific location in a string |
Posted: Sun Jun 18, 2006 9:00 pm |
|
|
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
|
so.... |
Posted: Mon Jun 19, 2006 12:08 am |
|
|
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?
if there is some example |
|
|
asmallri
Joined: 12 Aug 2004 Posts: 1636 Location: Perth, Australia
|
|
Posted: Mon Jun 19, 2006 12:31 am |
|
|
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
|
|
Posted: Mon Jun 19, 2006 7:53 am |
|
|
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])
|
|
|
|
|
|
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
|