indexing a array
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2020-09-28 6:09 AM
hi
I want to locate a string in an other string and then mark its position with an 'X'. here is the code
#include "main.h"
#include "adc.h"
#include "dma.h"
#include "i2c.h"
#include "usart.h"
#include "gpio.h"
#include "stm32f1xx.h"
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
int position;
char Rcv[500];
char *ret;
ret=strstr(Rcv,"+CMTI");
position=ret-Rcv;
Rcv[position]='X';
the code doesn't run. when I comment the third line, it works. why is that?
Solved! Go to Solution.
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2020-09-28 1:48 PM
- ret=strstr(Rcv,"+CMTI");
- if (ret) Rcv[(int)(ret-&Rcv[0])]='X';
Up vote any posts that you find helpful, it shows what's working..
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2020-09-28 7:07 AM
The POSIX function strstr() already returns the first position of the substring, I see no need for your pointer fiddling.
The returned pointer can be NULL.
That is simple C stuff, which can easily be tested on the host.
The clib implementation of you embedded project might have limited POSIX compatibility.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2020-09-28 1:42 PM
dear Ozone
- but when I monitor ret, it is '+' and not the position of '+' in Rcv. also, more importantly, when I am running the above code, ret is not evaluated but just as I comment the third line, it is evaluated and I get the position number right. I don't understand!!! isn't it supposed to work line by line? so why removing third line affects first and second line(ret=strstr(Rcv,"+CMTI"; & position=ret-Rcv;)???
- what do you mean my implementation is not OSIX compatible? can you help me with some example for this type of situation?
)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2020-09-28 1:48 PM
- ret=strstr(Rcv,"+CMTI");
- if (ret) Rcv[(int)(ret-&Rcv[0])]='X';
Up vote any posts that you find helpful, it shows what's working..
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2020-09-28 10:13 PM
dear clive1
it worked! you are amazing as always!:folded_hands:
