ADVERTISEMENT
ADVERTISEMENT

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!

ADVERTISEMENT




ADVERTISEMENT



Comments and Discussions!

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.