edited by
1,473 views
1 votes
1 votes
main()
{
    char *p1="name";
    char *p2;
    p2=(char*)malloc(20);
    memset(p2,0,20);
    while(*p2++=*p1++);
    printf("%s\n",p2);
}
edited by

2 Answers

Best answer
1 votes
1 votes
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main() {
	char *p1="name";
	char *p2;
	p2=(char*)malloc(20);
	memset(p2,0,20);
	
	char *temp = p2; // add this line of code
	                 // which stores the pointer location returned by malloc 
	while(*p2++=*p1++); // copy string content pointed by p1 to p2
	                    // in this process both p1 and p2 gets incremented
	printf("%s\n",temp);// temp still pointing to the start 
	                    // of newly created string 
}
selected by
1 votes
1 votes

Improved Code:

Compiler DOS Box. 

#include<stdio.h>

#include<stdlib.h>

#include<conio.h>

#include<string.h>

void main()

{

  char *p1="name";

char *p2;

clrscr();

p2=(char *) malloc(20);

memset(p2,0,20);

while(*++p2=*p1++)

printf("%s\n",p2);

getch();

}

edited by

Related questions

0 votes
0 votes
2 answers
1
Somdatta Sen asked Feb 10, 2019
409 views
For this statement->printf ("%s",* *++cpp+3);What will be the order of precedence according to pointer arithmetic?
0 votes
0 votes
0 answers
2
0 votes
0 votes
1 answer
3
ranarajesh495 asked Oct 9, 2018
571 views
If we are taking character as input then how we can check the character against a a range of numbers. Please explain
0 votes
0 votes
1 answer
4
Prakhar Yadav 1 asked Aug 6, 2018
497 views
What is a variable pointer? please note the question is not asking about pointer variables, but the specific term.