Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

strcat() question 2

Status
Not open for further replies.

DoraC

Programmer
May 7, 2002
98
0
0
US
Hi,

I'm sure there is a simple explanation for this, but it is frustrating me. This code:
Code:
#include <string.h>

void main()
{
    char* pcFirst  = &quot;first&quot;;
    char* pcSecond = &quot;second&quot;;
    char* pcConcat = strcat( pcFirst
                            ,pcSecond );

    printf(&quot;concatenated: .%s.\n&quot;, pcConcat);
}
yields output:
Code:
concatenated: .firstsecond.
but THIS code:
Code:
#include <string.h>

void main()
{
    char* pcFirst  = &quot;first&quot;;
    char* pcSecond = &quot;second&quot;;
    char* pcConcat = 
        strcat( pcFirst
               ,strcat( &quot; : &quot;, pcSecond ) );

    printf(&quot;concatenated: .%s.\n&quot;, pcConcat);
}
yields output:
Code:
econd
or something similar (i.e. a tail portion of the second string) for other values of pcFirst and pcSecond.
Shouldn't they both work the same, if strcat itself just returns a char*?

thank you - I'm sure it's something silly and trivial but
i can not figure it out.
:-S
dora c
 
Both are wrong - you're modifying what C now considers to be read-only memory.
Even though your implementation allows you to modify the strings, there is no space allocated to append data

Start with these
Code:
char First[100]  = &quot;first&quot;;
char Second[100] = &quot;second&quot;;
and repeat your experiments

Oh, and main() returns an int


--
 
Hi Salem,

Thanks for the reply.

Here's the new code:
Code:
#include <string.h>

int main()
{
    char  pcFirst[100]  = &quot;first&quot;;
    char  pcSecond[100] = &quot;second&quot;;
    char* pcConcat = 
        strcat( pcFirst
               ,strcat( &quot; : &quot;, pcSecond ) );

    printf(&quot;concatenated: .%s.\n&quot;, pcConcat);
}
Yielding output:
Code:
econd
You mentioned that I'm modifying what C considers to be read-only memory. I take that to mean I'm modifying the second parameter of strcat(). My C pocket reference book defines strcat as
Code:
char *strcat( char *s1, const char *s2 );
which I just take to mean that strcat won't modify s2 in any way - not that I can't pass it a non-const pointer.

Hmmm, there must be something rather fundamental that I'm missing here...

BTW, I'm using VC++ 6.0 as my compiler, windows NT platform.

thanks!
dora c
 
> which I just take to mean that strcat won't modify s2 in any way.

strcat doesn't modify s2, it modifies s1 and the pointer that it returns is simply a pointer to s1. The &quot;read-only&quot; memory that you are now modifying is that pointed to by the constant string &quot; : &quot;.

To see what is happening, try this:
Code:
strcat(pcFirst,&quot; : &quot;);
printf(&quot;pcFirst: %s\n&quot;,pcFirst);
strcat(pcFirst,pcSecond);
printf(&quot;concatenated: .%s.\n&quot;, pcFirst);

or you could use sprintf

Good Luck!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top