strncpy question

i am using strncpy to copy a string over to a field called ll->ldata.string. Below is the code snippet. The results are as follows: My question is why is it after copying final into ll->ldata.string the characters "10" became "104"????


name is :descp  size is 2
len: 0   ll->ldata.string:
len: 2   final: 10
name is :descp  size is 2
len: 3   ll->ll->ldata.string: 104

memset (ll->ldata.string, 0, ll->lfield.size);
printf("name is :%s\t size is %d\n",ll->, ll->lfield.size);
printf("len: %d\t ll->ldata.string: %s\n", strlen(ll->ldata.string), ll->ldata.string);
printf("len: %d\t final: %s\n", strlen(final), final);
strncpy (ll->ldata.string, final, ll->lfield.size);
printf("name is :%s\t size is %d\n",ll->, ll->lfield.size);
printf("len: %d\t ll->ldata.string: %s\n", strlen(ll->ldata.string), ll->ldata.string);

Open in new window

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jaime OlivaresSoftware ArchitectCommented:
you have to be aware of this:
C strings are null terminated, most C standard library that handle strings, like strcpy() and strcat() puts automatically a null character when processing a string.
But strncpy DOES NOT put a null character after string when the size of the copied string is equal or exceeds the specified size. So, if you don't handle this situation, you will see some unexpected characters after the string because it is not properly terminated by a null character.
Or in other words :

       strncpy (ll->ldata.string, final, strlen(final) + 1);

Note the + 1 for the trailing '\0' character.
Jaime OlivaresSoftware ArchitectCommented:
>>strncpy (ll->ldata.string, final, strlen(final) + 1);

this should be done with care, that depends on the capacity of the ldata.string buffer. If shorter than strlen(final)+1, you will produce a memory access violation

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

>> this should be done with care

The same "problem" existed in the original code ... So, I assume that Adrenaline already handles that correctly.
I was merely pointing out the solution in code to the problem Adrenaline experienced.
evilrixSenior Software Engineer (Avast)Commented:
If it's a case that source (final) isn't null terminated I'd suggest something like below...
#include <string.h>
#include <stdio.h>
int main()
	// Set up some test data
	char const field[] = {'h', 'e', 'l', 'l', 'o'}; // 5 chars, not NULL terminated
	size_t const field_size = sizeof(field)/sizeof(field[0]);
	char data[6] = {0}; // Make sure it's all NULL'ed out (this bit is important, use memset to do this if necessary)
	size_t const data_size = sizeof(data)/sizeof(data[0]);
	// This will ensure you definately have a NULL terminated string
	strncpy(data, field, (field_size > (data_size - 1) ? (data_size - 1) : field_size));
	return 0;

Open in new window

May I ask what the reason for the B grade is ? That usually means that something was missing in our answers. If so, you can always ask for clarification before accepting an answer ...

Also note that what evilrix suggested is slightly different than what jaime_olivares (and me) suggested. It assumes there is no NULL at the end of the source string ... Is that the case ???
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.