Passing string to function
I've written this code for an exercise i found in a tutorial (without answer :( ). When i run it the szNew string doesen't have a value at all. I'm not very good with a debugger, so i wondered if you guys could help me out.
Compiler: gcc-2.95.3
Os: Debian 3.0 woody (Linux kernel 2.4.18)
Here's the code:
#include <stdio.h>
#include <stdlib.h> /* For malloc() */
#include <string.h> /* For strlen() */
char* concat_strings(char* szTarget, char* szSource, char *p);
int main()
{
char szString_1[100] = "hello "; /* Just a test value */
char szString_2[100] = "maan!";
char *szNew;
szNew = (char*) malloc(strlen(szString_1) && strlen(szString_2)); /* Allocating memory for the string (it should be the size of szString_1 and szString_2 */
szNew = concat_strings(szString_1, szString_2, szNew);
puts (szNew);
return 0;
}
char* concat_strings(char* szTarget, char* szSource, char *p)
{
puts(szTarget); /* Just to view the values */
puts(szSource);
while((*p++ = *szTarget++) != 0) /* Adding the first string to a new string */
{
}
while((*p++ = *szSource++) != 0) /* Adding the second string to the new string. */
{
}
return p;
}