2012-08-20 06:14 AM
Hi,
When I use strcat to append a string to a destination string. It does not work. I did it like this. char *item_list = ''''; strcat(item_list,''hello''); When I set a break point and check the item_list, it is still NUll. Any ideas?2012-08-20 09:14 AM
Thank you. It works now for your code. But if you change
char string[16]; to char *string; it will not work. Any ideas about this? Thank you.2012-08-20 10:09 AM
Well, my comment about assembly leve, was made in haste, it probably is more or less the last resort. But you should check the variable in a watch window, and in a memory window.
char m[10] = '''';for(int i=0; i<9; i++)m[i] = ‘o;
Then I look at m[i], it is still nothing in it. During my last encounter with IAR, I found it is sometimes a bit delicate in regard to optimization. Try ''volatile char m[10];'' instead. That should keep the compiler from optimizing it out. And, if you specify the length of an array, you don't need to initialize it.
2012-08-20 10:23 AM
Any ideas about this?
Because there is no memory behind it. The pointer needs to point to a memory buffer of sufficient size, and in RAM.2012-08-20 11:14 AM
Other languages (e.g., Java) don't make you think about memory allocation, but C is much more specific. You may want to spend some time understand how pointers and allocation work before breaking out your oscilloscope. :)
It's probably easier to experiment with this kind of C code on a ''real'' operating system that will give you a lot more feedback about what went wrong compared to a development board. Try it on *nix or Windows first.2012-08-20 03:05 PM
Please forgive me, I am new to this. Can you tell me how to allocate it to RAM? Using volatile? or malloc?
Thank you.2012-08-20 05:43 PM
char space[16];
void test(void)
{
char *string = space;
strcpy(string, ''Hello '');
strcat(string, ''World!'');
puts(string);
}
Learn C first, then embedded
2012-08-21 02:53 PM
Thank you.
2013-02-24 04:19 AM
Hi all,
I have a similar problem in my project.Some str*** functions, included into <string.h>, work only with debugger. In stand-alone mode, strstr, strcmp, strcpy, etc, doesn't work.this is the code:char buffer[size];char* pch;int main(void){ //... pch = strchr(buffer, '\0'); if ((pch == NULL) || (pch != NULL && pch-buffer>0)) { if(strstr(buffer, ''ok'') != NULL){ //I reach this statement only when I set a breakpoint to strstr instruction //... } }}HW: STM32F103REIAR: 6.30.1Jlink: lite v8How can I solve?Thanks2013-02-24 05:40 AM
The primary failure mode with string functions is the absence of a terminating NUL, often as a result of it not be explicitly initialized, or content moved in via memcpy() vs strcpy()
I'd be more confortable with your code if I saw a strcpy(buffer,''''); at the beginning and clearer comparison. If the debugger changes the situation look at using a serial port to provide telemetry, and dump out the content of the buffer prior to the comparison. if ((pch == NULL) || ((pch != NULL) && ((pch - buffer) > 0)))2013-02-24 06:30 AM
thanks for reply.
in my code, there is already a printf, between IFs.this way:if ((pch == NULL) || ((pch != NULL) && ((pch - buffer) > 0)))print(USART1, buffer);if(strstr...) {}the content of the buffer is correct.- OK is sent to hyperterminal- strstr statement is performed right way during debug modenote that the buffer is filled with 0x00 via memset at the end of every loop; later it will be filled via DMA transfer from stm32.it's not clear for me the strcpy(buffer,'''') you'd like to see at the beginning...thanks