Click here to Skip to main content
15,893,904 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

i try simple test function string in c,

C++
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

int main(int argc, char **argv)
{ 

	const char *str ="G06111412/031/PL05"; 
	unsigned short maxLen = strlen( str );
	unsigned short lengcut =13;
	char c_substring[lengcut + 1];
	strncpy(c_substring, str, lengcut);
	printf("string \"%s\"- has lenght %d char, cut -%d-char substring is \"%s\"- lenght %d\n", str, maxLen, lengcut, c_substring, strlen(c_substring));
	
	return EXIT_SUCCESS;
}


but return value invalid:
C++
string "G06111412/031/PL05"- has lenght 18 char, cut -13-char substring is "G06111412/031■a"- lenght 15


Please explain why this is wrong ?

Thanks advices

What I have tried:

runtime we get substring is not valid
C++
string "G06111412/031/PL05"- has lenght 18 char, cut -13-char substring is "G06111412/031■a"- lenght 15
Posted
Updated 27-Apr-24 6:42am
v3
Comments
Dave Kreskowiak 27-Apr-24 0:50am    
You forgot to explain what you expect to happen. What is wrong depends on what you need the code to do and you didn't explain that.

1 solution

Nothing there is saying that the substring is invalid: you get what I would expect.
If I run your code at onlinegdb.com, I get a different result (if I ignore the copious compiler warnings):
string "G06111412/031/PL05"- has lenght 18 char, cut -13-char substring is "G06111412/031V"- lenght 14
And that's because it treats memory slightly differently to your compiler: by default it zeros memory when assigned - yours doesn't.

In C, a string is terminated by a null character - so when you extract 13 characters from your 18 character input strncpy copies the data up to the character limit (13) or the first null character whichever comes first. In your case, that means that 13 characters are copied into your 14 character buffer and the last character position is never touched - it remains unassigned just as it was when your compiler allocated the memory. If by luck it is a null (which has odds of 256:1) then you will get a C string what works fine. If it doesn't, then the string continues utill; a null is located - and you get a length of 15, 16, 1024, whatever and random garbage printed after teh actual string data.

Add this line, and your problem will go away:
C
char c_substring[lengcut + 1];
	strncpy(c_substring, str, lengcut);
	c_substring[lengcut] = '\0';
But ... do yourself a favour and sort out the compiler warnings and notes before you run your app! They could be hiding errors and that means the compiler doesn't produce an EXE file so when you run your app it's the last version that did compiler you execute and that gets very confusing to test ... :D
 
Share this answer
 
Comments
Member 16250016 27-Apr-24 5:48am    
Thanks,
OriginalGriff 27-Apr-24 6:31am    
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900