View previous topic :: View next topic |
Author |
Message |
cvargcal
Joined: 17 Feb 2015 Posts: 134
|
how moving spaces in array |
Posted: Thu May 07, 2020 1:19 am |
|
|
Hi,
I have this array:
Code: | char buffer[]=0d0a53454e44204f4b0d0a2002000000;
|
I need move or clear the part 0d0a53454e44204f4b0d0a that is the response of AT CMD and after get only data since same buffer:
Code: | buffer[]=2002000000; |
I know its possible copy since 0x20.... to other array, but i want know the way to delete that data since same buffer.
Thanks |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19520
|
|
Posted: Thu May 07, 2020 2:13 am |
|
|
The way to move parts of a buffer into itself, is memmove. You need to pointer
where you want it to go, and where you want it to come 'from'.
This performs a safe copy, so data is not destroyed till 'after' the move
has taken place.
You can 'shorten' a buffer by simply putting '\0' where you want the new
'end' to be. |
|
|
cvargcal
Joined: 17 Feb 2015 Posts: 134
|
|
Posted: Thu May 07, 2020 3:05 am |
|
|
Ttelmah wrote: | The way to move parts of a buffer into itself, is memmove. You need to pointer
where you want it to go, and where you want it to come 'from'.
This performs a safe copy, so data is not destroyed till 'after' the move
has taken place.
You can 'shorten' a buffer by simply putting '\0' where you want the new
'end' to be. |
Thanks
Code: | int main(){
char *res;
char txt[] = "\r\nSEND OK\r\n1234";
char txt_sub[] = "0000000000099";
res = memmove(txt+1, txt_sub, 10);
printf(res);
} |
output
00000000001234
but i want the result is
1234 |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19520
|
|
Posted: Thu May 07, 2020 3:29 am |
|
|
res = memmove((txt+11), txt, 5);
Then txt, will be "1234".
You want to start at the '1', not the beginning of the string, and only move
what is wanted. You asked about doing it to the same buffer (though
you can equally use a different one). |
|
|
cvargcal
Joined: 17 Feb 2015 Posts: 134
|
|
Posted: Thu May 07, 2020 8:19 am |
|
|
Ttelmah wrote: | res = memmove((txt+11), txt, 5);
Then txt, will be "1234".
You want to start at the '1', not the beginning of the string, and only move
what is wanted. You asked about doing it to the same buffer (though
you can equally use a different one). |
Yes, I want "delete" the first 11th bytes.
I tested this in the compiler online and i dont understand why i have put "0",
https://www.onlinegdb.com/online_c_compiler
Code: | #include <stdio.h>
#include <string.h>
int main() {
char *res;
int i;
char txt[] = "\r\nSEND OK\r\n1234";
res = memmove(txt+11, txt, 0);
i= strlen(res);
printf(res);
strncpy((char *)txt, res, i);
printf(txt);
return 0;
}
|
Well, I tested in CCS and worked fine |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19520
|
|
Posted: Thu May 07, 2020 11:56 am |
|
|
The reason is because I typed the destination and source the wrong
way round. Should be:
res = memmove(txt, (txt+11), 5);
Destination is the first number in memmove.
Also, I hope you are not trying to use a number returned by memmove.
It does not return a value. The location where the dat is, is where
you have said for it to go. So txt.
The returned value is 'undefined'. |
|
|
cvargcal
Joined: 17 Feb 2015 Posts: 134
|
|
Posted: Thu May 07, 2020 4:28 pm |
|
|
Ttelmah wrote: | The reason is because I typed the destination and source the wrong
way round. Should be:
res = memmove(txt, (txt+11), 5);
Destination is the first number in memmove.
Also, I hope you are not trying to use a number returned by memmove.
It does not return a value. The location where the dat is, is where
you have said for it to go. So txt.
The returned value is 'undefined'. |
The pic work fine with res = memmove(txt+11, txt, 0);
I don't have problem, maybe its better because is dont know hows much data is after 11bytes. right?
res = memmove(receive_string_uart2,receive_string_uart2+11, SIZE_BUFFER_UART2-11);
Thanks you, this was of many help. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19520
|
|
Posted: Thu May 07, 2020 11:34 pm |
|
|
Using the return value is wrong.
You have to understand that with all such operations, it is up to you
to ensure there is storage at the location being used, and that what you do
does not overflow the available space allocated for the string. The return
value is an 'accidental' return, that may well not be addressing what it
should. The manual is explicit, that the return is 'undefined'.
The address you should be using is receive_string_uart2.
Get rid of using res. It is an 'accident waiting to happen', that could
result in you trying to address memory that is nothing to do with what
is required..... |
|
|
|