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 support@ccsinfo.com

string positon of s1 in s2 [solved]

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



Joined: 08 Apr 2009
Posts: 100
Location: Chennai, India

View user's profile Send private message

string positon of s1 in s2 [solved]
PostPosted: Mon Jul 27, 2015 9:46 am     Reply with quote

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

View user's profile Send private message

PostPosted: Mon Jul 27, 2015 10:23 am     Reply with quote

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

View user's profile Send private message

Thanks for the answer
PostPosted: Mon Jul 27, 2015 11:26 am     Reply with quote

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

View user's profile Send private message

PostPosted: Mon Jul 27, 2015 11:42 am     Reply with quote

No.

Pointers are declared as pointers:

Code:

char * pointer;


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

View user's profile Send private message

I got anoutline
PostPosted: Mon Jul 27, 2015 11:55 am     Reply with quote

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

View user's profile Send private message

PostPosted: Tue Jul 28, 2015 9:49 am     Reply with quote

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

View user's profile Send private message

PostPosted: Tue Jul 28, 2015 2:07 pm     Reply with quote

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

View user's profile Send private message

that finally worked
PostPosted: Tue Jul 28, 2015 9:51 pm     Reply with quote

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.
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