c++.dos - stpcpy() function
- Noobi (15/15) Oct 04 2003 Hi everybody,
- Gisle Vanem (19/28) Oct 04 2003 Because 'strtmp' points to an undefined area. You must initiate it.
- Noobi (24/55) Oct 04 2003 Ok thanks Gisle V.
- Gisle Vanem (11/21) Oct 04 2003 Why do you need strtmp? Simply:
- noobi (23/45) Oct 04 2003 Ok, i rewriten the code :
- Jan Knepper (9/38) Oct 04 2003 // char *strtmp=""; //initiate strtmp
Hi everybody, #include <stdio.h> #include <string.h> char *a[4] ={"one", "two", "three", "four"}; main() { char *strtmp; stpcpy(strtmp, a[0]); printf("%s", strtmp); getch(); } Is correct to write stpcpy(strtmp, a[0]); ? When i declare strtmp in a global buffer, in output i got (null), why ?. Thanks for your help. Noobi.
Oct 04 2003
"Noobi" <Noobi_member pathlink.com> wrote:main() { char *strtmp; stpcpy(strtmp, a[0]); printf("%s", strtmp); getch(); } Is correct to write stpcpy(strtmp, a[0]); ?Yes, but..When i declare strtmp in a global buffer, in output i got (null), why ?.Because 'strtmp' points to an undefined area. You must initiate it. But why use that archaic and non-standard function? Use strlcpy() or better yet strlcpy() wherever possible. Use strchr(strtmp,'\0') to find the end-of-string. man stpcpy: ... DESCRIPTION The stpcpy() function copies the string pointed to by src (including the terminating `\0' character) to the array pointed to by dest. The strings may not overlap, and the destination string dest must be large enough to receive the copy. strcpy, strnpy and strlcpy do allows overlapping strings AFAIK. Gisle V. -- There are only 10 types of people in this world... those who understand binary, and those who don't.
Oct 04 2003
Ok thanks Gisle V. #include <stdio.h> #include <string.h> char *a[24] ={"one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen", "twenty", "twenty 1", "twenty 2", "twenty 3", "twenty 4"}; main() { short int x; char *strtmp; for(x=0; x<=23; x++) { strcpy (strtmp, a[x]); printf("%s ", strtmp); } getch(); } I want to display all 'a''s items but in output this program failed. Whats wrong ? Thanks for your help Noobi. In article <blmblh$2gi$1 digitaldaemon.com>, Gisle Vanem says..."Noobi" <Noobi_member pathlink.com> wrote:main() { char *strtmp; stpcpy(strtmp, a[0]); printf("%s", strtmp); getch(); } Is correct to write stpcpy(strtmp, a[0]); ?Yes, but..When i declare strtmp in a global buffer, in output i got (null), why ?.Because 'strtmp' points to an undefined area. You must initiate it. But why use that archaic and non-standard function? Use strlcpy() or better yet strlcpy() wherever possible. Use strchr(strtmp,'\0') to find the end-of-string. man stpcpy: ... DESCRIPTION The stpcpy() function copies the string pointed to by src (including the terminating `\0' character) to the array pointed to by dest. The strings may not overlap, and the destination string dest must be large enough to receive the copy. strcpy, strnpy and strlcpy do allows overlapping strings AFAIK. Gisle V. -- There are only 10 types of people in this world... those who understand binary, and those who don't.
Oct 04 2003
"Noobi" <Noobi_member pathlink.com> wrote:main() { short int x; char *strtmp; for(x=0; x<=23; x++) { strcpy (strtmp, a[x]); printf("%s ", strtmp); }Same error as 1st time:Why do you need strtmp? Simply: for(x=0; x < sizeof(a) / sizeof(a[0]); x++) printf("%s ", a[x]); NEVER use hardcoded values (23) like that (incase a[] changes and you forget to change the foor-loop). -- Gisle V. /bin/laden: Not foundBecause 'strtmp' points to an undefined area. You must initiate it.
Oct 04 2003
Ok, i rewriten the code : #include <stdio.h> #include <string.h> char *a[24] ={"one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen", "twenty", "twenty 1", "twenty 2", "twenty 3", "twenty 4"}; main() { char *strtmp=""; //initiate strtmp short int x; for(x=0; x < sizeof(a) / sizeof(a[0]); x++) { strcpy (strtmp, a[x]); //i use strtmp to change the a[]'s value without to be modify it printf("%s ", strtmp); //printf("%s ", a[x]); } getch(); } the program failed again. In article <blmr71$os2$1 digitaldaemon.com>, Gisle Vanem says..."Noobi" <Noobi_member pathlink.com> wrote:main() { short int x; char *strtmp; for(x=0; x<=23; x++) { strcpy (strtmp, a[x]); printf("%s ", strtmp); }Same error as 1st time:Why do you need strtmp? Simply: for(x=0; x < sizeof(a) / sizeof(a[0]); x++) printf("%s ", a[x]); NEVER use hardcoded values (23) like that (incase a[] changes and you forget to change the foor-loop). -- Gisle V. /bin/laden: Not foundBecause 'strtmp' points to an undefined area. You must initiate it.
Oct 04 2003
noobi wrote:Ok, i rewriten the code : #include <stdio.h> #include <string.h> char *a[24] ={"one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen", "twenty", "twenty 1", "twenty 2", "twenty 3", "twenty 4"}; main() {// char *strtmp=""; //initiate strtmp char strtmp [ 64 ];short int x; for(x=0; x < sizeof(a) / sizeof(a[0]); x++) { strcpy (strtmp, a[x]); //i use strtmp to change the a[]'s value without to be modify it printf("%s ", strtmp); //printf("%s ", a[x]); } getch(); } the program failed again.Yes! You really should get a read on C (or C++). You're not quit up to par with the basic C/C++ principles which is OK, but there are LOTS of sources out there that will explain you what is wrong with the code. -- ManiaC++ Jan Knepper
Oct 04 2003