cancel
Showing results for 
Search instead for 
Did you mean: 

strcat method does not work

chai
Associate II
Posted on August 20, 2012 at 15:14

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?

24 REPLIES 24
chai
Associate II
Posted on August 20, 2012 at 18:14

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.

frankmeyer9
Associate II
Posted on August 20, 2012 at 19:09

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.

Posted on August 20, 2012 at 19:23

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.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
andyturk
Associate II
Posted on August 20, 2012 at 20:14

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.

chai
Associate II
Posted on August 21, 2012 at 00:05

Please forgive me, I am new to this. Can you tell me how to allocate it to RAM? Using volatile? or malloc?

Thank you.

Posted on August 21, 2012 at 02:43

char space[16];
void test(void)
{
char *string = space;
strcpy(string, ''Hello '');
strcat(string, ''World!'');
puts(string);
}
 Learn C first, then embedded

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
chai
Associate II
Posted on August 21, 2012 at 23:53

Thank you.

Posted on February 24, 2013 at 13:19

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

IAR: 6.30.1

Jlink: lite v8

How can I solve?

Thanks

Posted on February 24, 2013 at 14:40

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)))
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on February 24, 2013 at 15:30

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 mode

note 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