hello,
I am supposed to concatenate 2 strings (words). For this the function should call malloc or calloc to get memory for the newstring(after concatenated) .I am not allowed to use the library string.h. I figured out to find the length of the 2 strings. .. but i don't get how to concatenate the two words.
P.S This is the first time i am doing program with dynamic memory allocation.
so far this is my code:
warning C4013: 'my_strcat' undefined; assuming extern returning int
: warning C4047: 'return' : 'int ' differs in levels of indirection from 'int *'
: warning C4101: 'i' : unreferenced local variable
: error C2040: 'my_strcat' : 'char *(char *,char *)' differs in levels of indirection from 'int ()'
Error executing cl.exe.
I think i am making the problem more complicated than it is... i would like to know the concept behind dynamic memory allocation..
any help would be great!! thanks
I am supposed to concatenate 2 strings (words). For this the function should call malloc or calloc to get memory for the newstring(after concatenated) .I am not allowed to use the library string.h. I figured out to find the length of the 2 strings. .. but i don't get how to concatenate the two words.
P.S This is the first time i am doing program with dynamic memory allocation.
so far this is my code:
Code:
#include<stdio.h>
#include<stdlib.h>
int my_strlen(char *string1, char *string2);
//char*my_strcat(char *string1,char *string2);
main(){
char *string1 = "Hello";
char *string2 = "world!";
my_strlen(string1,string2);
// printf("\n %d %d",n[0],n[1]);//
printf("%s\n",my_strcat("Hello","world!"));
}
int my_strlen(char *string1,char *string2){
int i;
int *newstring;
int *newstring1;
newstring = malloc(sizeof(string1 +1));
newstring1= malloc(sizeof(string2 +1));
if(newstring == 0|| newstring1==0)
printf("no memory");
printf("%d %d", newstring,newstring1);
/* for(i=0;*newstring!='\0',*newstring1!='\0';newstring++,newstring1++)
i++;*/
return (newstring,newstring);
}
/*char *my_strcat(char*string1, char *string2){
while(*string1) string1++;
while(*string1++ = *string2++);
return(string1,string2);
}*/
// warnings and error//
: warning C4047: 'return' : 'int ' differs in levels of indirection from 'int *'
: warning C4101: 'i' : unreferenced local variable
: error C2040: 'my_strcat' : 'char *(char *,char *)' differs in levels of indirection from 'int ()'
Error executing cl.exe.
I think i am making the problem more complicated than it is... i would like to know the concept behind dynamic memory allocation..
any help would be great!! thanks
Comment