Code to concatenate strings
#include<stdio.h> char *concat(char *p1,char *); //function decalaration int main(void) { char a[100],b[100],*q=NULL; //declare two char arrays printf("Enter str1:"); scanf("%s",a); printf("Enter str2:"); scanf("%s",b); q=concat(a,b); //calling str concat function printf("Concatenated str:%s\n",q); return 0; } char *concat(char *p1,char *p2) //function to concatenate strings { while(*p1!='\0') p1++; while(*p2!='\0') { *p1=*p2; p1++; p2++; } *p1='\0'; printf("Concatenated str=%s\n",p1); //printing the concatenated string return p1; //returning pointer to called function } //Although the logic is correct but its not showing the output. //Why this code doesn't work?
concatfunction, when you doreturn p1, what is the value of*p1?p1still is pointing to a valid string.