Hello there,
I've just started on C and what I'm trying to do is copying one string to another (which has not been initialized before) without using any string.h functions. Here's my code.
#include <stdio.h>
#include <stdlib.h>
void copyString(char*,char**);
int main(){
char* string1="Hello there";
char** string2;
printf("string1: %s\n",string1);
printf("Copying string1 to string2....\n");
copyString(string1,string2);
printf("Now string2:%s\n",*string2);
return 0;
}
void copyString(char* string1, char** string2){
char* temp = (char*) malloc(sizeof(*string1));
do{
*temp=*string1;
string1++;
temp++;
}while(*string1 !='\0');
string2 = &temp;
}
It doesn't work.
Sorry for my naiveness. This code was written with my poor understanding of pointers. Also I'm not sure of the way that I've 'malloc'ed. I'm really unsure of the use of sizeof function there. Please help. Thanks in advance.