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?
Jaime OlivaresConnect With a Mentor Software 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
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.
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Infinity08Connect With a Mentor Commented:
>> 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.
evilrixConnect With a Mentor Senior 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 ???
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.