C - Trick to Copy One String to another String without copying it in C Language.


IncludeHelp 16 August 2016

In this code snippet we will learn how to copy string using pointer without copying? In this trick we will neither use any library function not any logic to copy the string.

Actually we will use pointers, first string's pointer will be assigned to second string, and hence both strings will point same string. Partially strings will be copied.

So we can say it's a trick to copy one string to another without using copying, let's learn the trick.

C Code Snippet - Trick to Copy One String to another String without Copying

/*C - Trick to Copy One String to another 
String without copying it in C Language.*/

#include <stdio.h>

int main(){
	char *str1="Hello World!";
	char *str2;
	
	//assign address of str1 to str2
	str2=str1;
	
	printf("str1: %s\n",str1);
	printf("str2: %s\n",str2);
	
	return 0;
}

    str1: Hello World!
    str2: Hello World!



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.