why strncpy not copy 4 character in c2 ?
-
i want to know why c2 not contain "hell" in c2?
i want to know what is technical reason behind it.
char* c= "hello world",*c2;c2= NULL;
strncpy(c2,c,4);
cout<<c2<<endl;@Qt-embedded-developer
And where doesc2point to in your code?As a separate matter, depending on whether you have any further code, you may not see anything anyway. How about
cout<<c2<<endl? -
@Qt-embedded-developer said in why strncpy not copy 4 character in c2 ?:
i want to know what is technical reason behind it.
c2 is an uninitialized pointer - you have luck it doesn't crash at all.
Maybe learning basic c(++) stuff would be a good start.
@Christian-Ehrlicher i have edited the question. can you just let me know why what i expect not come in output.
i have seen that when i use char array it show output what i expect. but why its not possible with char* ?
-
@Christian-Ehrlicher i have edited the question. can you just let me know why what i expect not come in output.
i have seen that when i use char array it show output what i expect. but why its not possible with char* ?
@Qt-embedded-developer
Goodness, how does setting toc2toNULLhelp? Do you not understandstrncpy(), please read its documentation.....i have seen that when i use char array it show output what i expect. but why its not possible with char* ?
Sorry, but you really need to read up on C basics....
Further, you will see it does not put a terminating
\0into the destination area. Ifcout<<s2prints something it would behellfollowed by an unknown stream of random characters..... -
@Qt-embedded-developer
Goodness, how does setting toc2toNULLhelp? Do you not understandstrncpy(), please read its documentation.....i have seen that when i use char array it show output what i expect. but why its not possible with char* ?
Sorry, but you really need to read up on C basics....
Further, you will see it does not put a terminating
\0into the destination area. Ifcout<<s2prints something it would behellfollowed by an unknown stream of random characters.....why this show correct output then ?
char* c= "hello world",c2[10];
memset(c2,0x00,sizeof(c2));
strncpy(c2,c,4);cout<<c2;
-
why this show correct output then ?
char* c= "hello world",c2[10];
memset(c2,0x00,sizeof(c2));
strncpy(c2,c,4);cout<<c2;
@Qt-embedded-developer
Precisely because that avoids all the faults previously mentioned in your code!char c2[10]allocates 10 bytes of storage whichc2"points to".char *c2allocates no storage forc2to point to. You would either need tomalloc()space, or setc2to point to, say, achar c3[10]which does allocate storage.You seem to think
char c2[10]andchar *c2are "the same thing", which they are not. -
@Qt-embedded-developer
Goodness, how does setting toc2toNULLhelp? Do you not understandstrncpy(), please read its documentation.....i have seen that when i use char array it show output what i expect. but why its not possible with char* ?
Sorry, but you really need to read up on C basics....
Further, you will see it does not put a terminating
\0into the destination area. Ifcout<<s2prints something it would behellfollowed by an unknown stream of random characters.....@JonB can you give example to put \0 in above example code ?
-
@JonB can you give example to put \0 in above example code ?
@Qt-embedded-developer
I really, really should not have to do this....char *c = "hello world"; char c2[10]; strncpy(c2, c, 4); c2[4] = '\0'; cout << c2 << endl;One further thing: these days, using C++ (and also Qt), you really should no longer need to use C library functions like
strncpy()(ormalloc()). There are better, safer ways to do that, e.g. using C++std::stringor QtQString(andnewformalloc()). Same with e.g.QByteArrayinstead ofmemcpy/set(). -
@Qt-embedded-developer
I really, really should not have to do this....char *c = "hello world"; char c2[10]; strncpy(c2, c, 4); c2[4] = '\0'; cout << c2 << endl;One further thing: these days, using C++ (and also Qt), you really should no longer need to use C library functions like
strncpy()(ormalloc()). There are better, safer ways to do that, e.g. using C++std::stringor QtQString(andnewformalloc()). Same with e.g.QByteArrayinstead ofmemcpy/set().@JonB said in why strncpy not copy 4 character in c2 ?:
char *c = "hello world";
char c2[10];
strncpy(c2, c, 4);
c2[4] = '\0';
cout << c2 << endl;sorry for not clearly say about code. i said about below code.
can you just let me know what change in below code i need to do to add '\0'.
char* c= "hello world",*c2;
c2= NULL;
memset(c2,0x00,sizeof(c2));
strncpy(c2,c,4); -
@JonB said in why strncpy not copy 4 character in c2 ?:
char *c = "hello world";
char c2[10];
strncpy(c2, c, 4);
c2[4] = '\0';
cout << c2 << endl;sorry for not clearly say about code. i said about below code.
can you just let me know what change in below code i need to do to add '\0'.
char* c= "hello world",*c2;
c2= NULL;
memset(c2,0x00,sizeof(c2));
strncpy(c2,c,4);@Qt-embedded-developer
No, I'm sorry, I have reached my limit as to what I consider suitable to write/explain for you on this Qt site. For your own good you need to (a) read up about basic C, (b) understand code and (c) write/adapt your own for really simple stuff instead of asking someone else to do it for you.- You have ignored what I said about
c2 = NULL, and any understanding of allocating space. - You have not made any attempt to look at what I did write for you and plainly/easily apply it to your new case.
- You have written (copied?)
memset(c2,0x00,sizeof(c2));and not understood how that would affect any need to "add '\0'.".
- You have ignored what I said about
-
@JonB said in why strncpy not copy 4 character in c2 ?:
char *c = "hello world";
char c2[10];
strncpy(c2, c, 4);
c2[4] = '\0';
cout << c2 << endl;sorry for not clearly say about code. i said about below code.
can you just let me know what change in below code i need to do to add '\0'.
char* c= "hello world",*c2;
c2= NULL;
memset(c2,0x00,sizeof(c2));
strncpy(c2,c,4);@Qt-embedded-developer said in why strncpy not copy 4 character in c2 ?:
can you just let me know what change in below code i need to do to add '\0'.
YOU NEED TO ALLOCATE MEMORY FOR c2...
You was already told so here: "You would either need to malloc() space, or set c2 to point to, say, a char c3[10]".
Please try at least to understand what others are writing.As others already pointed out: learn C/C++ basics.
-
@Qt-embedded-developer said in why strncpy not copy 4 character in c2 ?:
can you just let me know what change in below code i need to do to add '\0'.
YOU NEED TO ALLOCATE MEMORY FOR c2...
You was already told so here: "You would either need to malloc() space, or set c2 to point to, say, a char c3[10]".
Please try at least to understand what others are writing.As others already pointed out: learn C/C++ basics.
-
@Qt-embedded-developer
Yeah, but why in the world you need such code withstrncpy()&malloc()is quite beyond me....P.S.
Technically your code is still not right. You will only copyhell, 4 characters, into themalloc()ed area, and that does initialize its memory allocation. The 5th character onward could be anything. You are "lucky" thecout<<c2does not printhelljgas978623i5tagsd534...:)strncpy(c2,c,4); // copies "hell", 4 characters, nothing more, does not add any terminator c2[4] = '\0'; // need terminating '\0' here cout<<c2; // this print the hell --- FOR SURE! -
@Qt-embedded-developer
Yeah, but why in the world you need such code withstrncpy()&malloc()is quite beyond me....P.S.
Technically your code is still not right. You will only copyhell, 4 characters, into themalloc()ed area, and that does initialize its memory allocation. The 5th character onward could be anything. You are "lucky" thecout<<c2does not printhelljgas978623i5tagsd534...:)strncpy(c2,c,4); // copies "hell", 4 characters, nothing more, does not add any terminator c2[4] = '\0'; // need terminating '\0' here cout<<c2; // this print the hell --- FOR SURE!@JonB said in why strncpy not copy 4 character in c2 ?:
Technically your code is still not right.
Also: There is a memory leak because there is no matching
free()for themalloc().Just because it works now, does not mean it is valid code. You need to make sure for every C-style string that there is a terminating '\0'. There is no other way to know the length of the string. Every C algorithm expects the terminating '\0'. The reason why this might work right now could be that you are compiling a debug version and this will initialize the memory with zeros. Once you compile a release version your application might suddenly crash. At least it is not guaranteed to work. Another reason that it works might be because you are lucky that there is a 0 in memory. Just one little change in code might get you a different memory allocation/access pattern and the 0 is gone.
Since you are already using
coutthis means you are using C++. C++ is here to help you, not to punish you. Usestd::stringinstead ofchar*. Try to avoidnewandmallocbecause you always might forget the correspondingdeleteorfree.std::stringandstd::vector(and other containers) will gladly manage memory for you. If you really need a pointer, use a smart pointer (together withmake_uniqueormake_shared, so you don't have to usenewwhich might accidentally leave a memory leak).