Re: strcat not working
- From: Robert W Hand <rwhand@xxxxxxxxxxxxxxxxxxx>
- Date: Sun, 03 Jul 2005 07:37:17 -0000
On Sun, 26 Jun 2005 09:57:50 -0000, "vasanth" <vasanthkr@xxxxxxxxx>
wrote:
>#include <stdio.h>
>#include <conio.h>
Non-standard header
>main()
int main(void)
> {
> char *str[] = {
> "vasanth",
> "rajit",
> "rakesh",
> "sanjay",
> "arvind",
> "mukul",
> "deep",
> };
str is an array of character pointers. The array is composed of
string literals that cannot be written to. So your program crashes.
There is another problem in that you concatenate between overlapping
arrays. strcat() does not allow it. The behavior is not defined. So
on my system, your program would give outcome like:
Hello kul
Hello deep
kuldeep
eep
Note the d is missing from deep. The system does not crash, it just
does something crazy.
Perhaps you meant something like this:
char str[][21] = {
"vasanth",
"rajit",
"rakesh",
"sanjay",
"arvind",
"mukul",
"deep",
};
> char *ptr_str, *ptr_str1;
> char *conc;
> ptr_str =*(str + 5) + 2;
> printf("\n Hello %s", ptr_str);
> ptr_str1 = *(str+6);
> printf("\n Hello %s", ptr_str1);
> conc = strcat(*(str + 5) + 2,*(str+6));
> /*
> printf("\n %s", conc);
> conc = *(str+6);
> printf("\n %s", conc);*/
printf("\n %s\n", conc);
to make sure that the last line can be read on output.
> getch();
getch() is non-standard.
> }
>This program crashes when it reaches the strcat comand can anyone tell
>me why.
Taking out the non-standard function, uncommenting the code, fixing
the last line, and changing the definition of str, the program
compiles and produces this output:
Hello kul
Hello deep
kuldeep
deep
--
Best wishes,
Bob
--
comp.lang.c.moderated - moderation address: clcm@xxxxxxxxxxxx -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
.
- References:
- strcat not working
- From: vasanth
- strcat not working
- Prev by Date: Re: Proper Switch & Case usage
- Next by Date: Re: pointers and strings
- Previous by thread: strcat not working
- Next by thread: Re: strcat not working
- Index(es):
Relevant Pages
|