Re: strcat not working



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.
.



Relevant Pages

  • Re: Roll your own std::vector ???
    ... For the sake of your comprehension, forget about pointers ... to always provide all of the overhead of a deep copy just in case that is what ... but those of reference types are simply "pointed to" by the ... | type of array element may be a value type or a reference type ??? ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Array Copy Concept Questions
    ... Here is what can happen when using arrays to hold objects of Junk. ... At this point what we have done is executed a deep copy of the references to the Junk objects. ... The arrays are different objects but their elements reference the same Junk objects. ... Bottomline is when you did your deep copy of the pins array you essentially did a deep copy of the references to the objects. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Why Serialization
    ... As I wrote in my previous reply you cannot deep copy an arraylist object. ... (You can copy an array, but if that contains reference types not more than ... in the state it is pver the line without XML serialization. ...
    (microsoft.public.dotnet.general)
  • Shalow copy vs Deep copy
    ... I wanted to perform a deep copy of an array. ... A shallow copy of an Array copies only the elements of the ... copy the objects that the references refer to. ...
    (microsoft.public.dotnet.languages.csharp)