How to read the last 3 characters of a char with no set size

Asked

Viewed 147 times

1

I have a C application where I need to read only the last 3 characters of a char. Like the size of this char may vary, I did as follows:

memcpy(valor_final, string_total[strlen(string_total - 3)], 3);

where valor_final group the last 3 characters and string_total is my char whole.

But this is returning me the first three. Can anyone point out to me the mistake?

1 answer

1

You have miscalculation

memcpy(valor_final,  string_total[strlen(string_total - 3)], 3);
//                  v                                v   ^   |
memcpy(valor_final, @string_total[strlen(string_total) - 3], 4);

Don’t forget to copy the terminator ('\0'). Make sure you have room in valor_final.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.