www.digitalmars.com         C & C++   DMDScript  

c++.dos - stpcpy() function

reply Noobi <Noobi_member pathlink.com> writes:
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
parent reply "Gisle Vanem" <giva users.sourceforge.net> writes:
"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
parent reply Noobi <Noobi_member pathlink.com> writes:
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
parent reply "Gisle Vanem" <giva users.sourceforge.net> writes:
"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:
Because 'strtmp' points to an undefined area. You must initiate it.
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 found
Oct 04 2003
parent reply noobi <noobi_member pathlink.com> writes:
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:
Because 'strtmp' points to an undefined area. You must initiate it.
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 found
Oct 04 2003
parent Jan Knepper <jan smartsoft.us> writes:
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