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.

char* temp = (char*) malloc(sizeof(*string1)); Consider string1, what type is it? char * from the function prototype.
In that case what type is *string1? If string1 is char * then dereferencing it gives char.
What does sizeof(char) return? 1 (by definition in the C standard)

I do not believe you wanted to allocate an array of 1 byte, I assume you want to allocate enough memory to hold the string what string1 points to. You need to count the number of characters that string1 points to (and then add 1 for the terminator), equivalent to strlen(string1) + 1 but presumably you are not allowed to use strlen either so you may have to write your own version of this.

Your loop looks OK to me but then consider string2 = &temp; What does this do? Alters the value of string2 which is a local variable. Since the variable is local that change is lost when the function exits so the function alters nothing outside itself. You want to return the string that temp now points to, that means that you need to alter something outside the function and that normally means dereferencing a pointer passed into the function to alter what it points to (assuming you are not returning the value). You need something that looks like *string2 = ???; , I leave the ??? for you to fill in.

Finally consider how you call your function copyString(string1,string2); From above we now that string2 will be dereferenced inside copyString and the location it points to altered. Where does string2 point? Nowhere, this code doesn't initialise it so this would cause an error. string2 needs to point at something or more conventionally string2 needs to have type char * and then you pass a pointer to it into the function copyString(string1,&string2); .

Ok, I've changed my code. All clear except the last advice. What should string2 point at? I don't understand this. Please explain. This is my code so far.

#include <stdio.h>
#include <stdlib.h>

void copyString(char*,char**);
int length(char*);

int main(){

	char* string1="Hello";
	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(char)*length(string1));
	
	do{
		*temp=*string1;
		string1++;
		temp++;

	}while(*string1 !='\0');
			
	*string2 = temp;		
}

int length(char* string){
	
	int i=1;
	
	do{
		string++;
		i++;
	}while(*string != '\0');

	return i;
}

About which string2 are u talking at in the main function or inside the copystring function
1. Inside main function, string2 do nothing accept holding the string after copystring fuction call
2.

void copyString(char* string1, char** string2

at this line string hold the address of string2 of main function
3.

*string2 = temp;

if you know abt pointer then i hope you understand this line

Ok, I got few errors there. Line 15 should be

copyString(string1,&string2);

and line 17 should be

printf("Now string2:%s\n",string2);

But that doesn't mean I didn't compile. That was because I tried something else on it before I posted the code. It doesn't work still. Nothing gets copied.

It seems there's some problem in the while loop. It seems not to copy anything. I cannot figure out why. Please help.

well.. i could not understand the need of copystring function in your program.
you are using two pointer to char type of variables. when we deal with pointers then this could be done by = operator..

try this one.

#include <stdio.h>

int main(){

	char* string1="Hello World!!";
	char* string2;
	
	printf("string1: %s\n",string1);
	printf("Copying string1 to string2....\n");
	
	string2 = string1;
	
	printf("Now string2:%s\n",string2);
	
	return 0;
}

or if you just want your program to work then try this code

#include <stdio.h>
#include <stdlib.h>

void copyString(char*,char**);
int length(char*);

int main(){

	char* string1="Hello world!!";
	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(char) * length(string1));

	do{
		*temp = *string1;
		string1++;
		temp ++;

	}while( *string1 != '\0');
    *temp = '\0';
    *string2 = temp;
}

int length(char* string){

	int i;

	for(i=0;string[i]!='\0';i++);

	return i;
}

Edited 5 Years Ago by vinitmittal2008: n/a

But that doesn't mean I didn't compile. That was because I tried something else on it before I posted the code. It doesn't work still. Nothing gets copied.

I hope I don't really need to say this but it really is best to post the correct code version when you are asking a question.

It seems there's some problem in the while loop. It seems not to copy anything. I cannot figure out why. Please help.

You mean the program produces no output? Or rather

string1: Hello
Copying string1 to string2....
Now string2:

Be precise when describing your problems, the program output and the expected program output it will help us to help you.

Your length function is wrong, as has been highlighted already.

Also you code does not allocate space for the NUL ('\0') terminator that C uses at the end of its strings and you don't put a NUL terminator into the copied string.

you can check this code :

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>

void copyString(char*,char*);

int main(){

    char* string1="Hello there";
    char* string2;

    printf("string1: %s\n",string1);
    printf("Copying string1 to string2....\n");

   //string2=string1;     // you can replace this line with your function.

    copyString(string1,string2);

    printf("Now string2:\n%s\n",string2);
    getch();
    return 0;
}

void copyString(char* x, char* y){

    int i=0;
    while(x[i]!=0)
   {
    y[i]=x[i];i++;
   }

   y[i]=0;

}

Edited 3 Years Ago by mike_2000_17: Fixed formatting

First of all, sorry for taking so long to respond. I had problems with my internet connection. Really sorry for that.

well.. i could not understand the need of copystring function in your program.

I think (correct me if I'm wrong) that,if your code is used then when later string1 is changed, string2 is also changed because they really point at the same char array. If you use this function that does not happen. That's the difference.

I hope I don't really need to say this but it really is best to post the correct code version when you are asking a question.

point taken. Yes, it's my mistake. Very sorry about that. I'll make sure it does not happen again.

You mean the program produces no output? Or rather

This point taken, again. I thought that my intention is obvious by the name, code etc.

Anyway, I found the problem. Now it looks so obvious and silly. But it consumed few of my days. Anyway this is my final code:

#include <stdio.h>
#include <stdlib.h>
  
void copyString(char*,char**);
int length(char*);
   
int main(){
   
     char* string1="Hello";
  	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(char)*length(string1));
  
     char* start = temp;
  
      do{ 
      	*temp=*string1;
 		 string1++; 
     	 temp++;
      
      }while(*string1 !='\0');
  
      *string2 = start;
  
      }
  
      int length(char* string){
   
      int i=1;
   
      do{
     	 string++;
     	 i++;
      }while(*string != '\0');
  
      return i;
  
      }

Thanks everybody. I think the problem is solved.

This question has already been answered. Start a new discussion instead.