View previous topic :: View next topic |
Author |
Message |
mutthunaveen
Joined: 08 Apr 2009 Posts: 100 Location: Chennai, India
|
string positon of s1 in s2 [solved] |
Posted: Mon Jul 27, 2015 9:46 am |
|
|
Dear Forum
i would like to know a how can we make this
Code: |
char needle[8] = "nav";
char buffer[90] = "123456789nav etc etc";
int pointer = 0;
|
i would like to know the exact position of my needle in buffer. is it possible by strstr??
Code: |
pointer = strstr(buffer, needle);
|
actually "nav" is in 10th position of "buffer". if you see in string.
so the pointer should be (pointer = 10) determining the nav positon.
when i read the pointer the value is something not realistic. i know im making some mistake. but im not good in handling pointers.
how can i get exact position?
also. as we use MID function in VB. do we have same kind of MID function in CCS C?? to extract some exact text from the positons given??
thank you so much for your time in replying
Last edited by mutthunaveen on Tue Jul 28, 2015 9:54 pm; edited 1 time in total |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19537
|
|
Posted: Mon Jul 27, 2015 10:23 am |
|
|
The pointer is the actual location _in memory_ where the value sits. So if 'buffer' is at 1234, then 'pointer' will be 1244.
If you want to know the character number, you have to subtract 'buffer' from the value in pointer.
The memory location _won't_ fit in an int, unless you have a very small chip.
Remember though that all the string operations want a pointer, which is why this is what the function returns. |
|
|
mutthunaveen
Joined: 08 Apr 2009 Posts: 100 Location: Chennai, India
|
Thanks for the answer |
Posted: Mon Jul 27, 2015 11:26 am |
|
|
Thank you for quick and kind answer
So. I pointers has to be declared always to long
Got it. Can you plz precise why if buffer is 1234 buffer and pointer is 1244,
Why increment of 10?
Are the some good materials to read and understand this subject for dummies like me?
Thank you |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19537
|
|
Posted: Mon Jul 27, 2015 11:42 am |
|
|
No.
Pointers are declared as pointers:
This declares a _pointer_ to an char. It automatically makes it large enough to handle the memory size of the chip, and more importantly, if you increment it, it moves to the next character. Declare one instead to an int16, and if you increment it, it moves to the next int16.
if 'buffer' is at 1234.
IF. If your buffer is at 1234 in memory.
Note the 'if'.
'buffer', is the pointer to the array buffer[]. In C, the name of an array, is the pointer to it. This is why strstr works with your two arrays (since it expects pointers).
On your mid, it depends which way you want to use it?. VB allows mid to be used as a target, or to take strings from a location. Both can be done, by different methods.
Key to understand is that C doesn't actually have a 'string' type. In C, a 'string' is simply an array of characters, with a terminating null. Nothing else. You can move blocks of characters around, with memcpy. So you can just copy characters from the location, or move the rest of the string right, and copy characters into the gap created. |
|
|
mutthunaveen
Joined: 08 Apr 2009 Posts: 100 Location: Chennai, India
|
I got anoutline |
Posted: Mon Jul 27, 2015 11:55 am |
|
|
Thank you so much
Now atleast i got an outline.. i will implement 2morrow and post the code
Thanks again |
|
|
mutthunaveen
Joined: 08 Apr 2009 Posts: 100 Location: Chennai, India
|
|
Posted: Tue Jul 28, 2015 9:49 am |
|
|
Dear Ttelmah,
Even with your patience typing, I'm kind of breaking my head with this pointers.
I put my simple code here
Code: |
#include <16F88.h>
#device *=16 PASS_STRINGS=IN_RAM
#fuses INTRC, NOWDT,NOPROTECT,NOLVP,CCPB3
#use delay(clock=8000000)
char search[8] = "nav";
char in_this[15] = "mnaveen";
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
void main(){
char *ptr;
char *know_ptr_address;
long position = 0;
know_ptr_address = *in_this;
ptr = strstr(in_this, search);
position = know_ptr_address - ptr;
}
|
Will position holds the good data??
I know my pointer manupulations are not at all correct, but i don't find a clue either to be right as I'm completly dumb to pointers.
I tried this code and im not at all succeeded. Can you plz suggest me or remodify my code so that i will exactly find positon of found string.
thank you. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19537
|
|
Posted: Tue Jul 28, 2015 2:07 pm |
|
|
Don't address things directly, this is an open forum, and lots of people may have things to say.
Start with:
know_ptr_address = *in_this;
This loads the pointer, with the _contents_ of the character pointed to by 'in_this'. So 'm'. _Not_ what you want.
You don't need to fiddle around with another variable. 'in_this'. Just this, with no *, or &, _is_ the memory address of the first character.
position=ptr-in_this;
Would return '1'. The address where "nav" starts, less where 'in_this' starts. |
|
|
mutthunaveen
Joined: 08 Apr 2009 Posts: 100 Location: Chennai, India
|
that finally worked |
Posted: Tue Jul 28, 2015 9:51 pm |
|
|
dear Ttelmah
Finally it worked. Now i understood the subject on how to subtract two pointer addresses.
and for MID function. I've used MEMMOV function. This worked finally. It is more easy than MID.
Thank you for your kind support. |
|
|
|