Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4606680
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T00:35:57+00:00 2026-05-22T00:35:57+00:00

I cannot use the xstrcpy to copy and print and it prints a blank

  • 0

I cannot use the xstrcpy to copy and print and it prints a blank line when i try to print the whole string in main though in the while loop each character is printed…but not the string immediately below the while loop…Don’t know why this is happening:(

Code:

#include<stdio.h>
#include<stdlib.h>
int xstrlen(char *);
char * xstrcpy(char *,char *);
main()
{
    char *expptr1="Hello World";
    char *expptr2 = "Hello Again";
    char *expptr3;
    printf("%d\n",xstrlen(expptr1));
    expptr3 = xstrcpy(expptr1,expptr2);
    printf("%s\n",expptr3);
}

int xstrlen(char *ptr)
{
    //printf("I am here\n");
    int count = 0;
    while(*ptr++!='\0')
        count++;
    return count;
}

char * xstrcpy(char *ptr1,char *ptr2)
{
    int i=xstrlen(ptr2);
    printf("%s\n",ptr1);
    printf("%s\n",ptr2);
    ptr1 =(char *)malloc(i);
    //printf("i am here\n");
    while(*ptr2 != '\0')
    {
        *ptr1 = *ptr2;
        printf("%c\n",*ptr1);
        ptr1++;
        ptr2++;
    }
    printf("%s",ptr1);
    return ptr1;
}

Output:

11
Hello World
Hello Again
H
e
l
l
o

A
g
a
i
n
ׁׁ

Exited: ExitFailure 4
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T00:35:57+00:00Added an answer on May 22, 2026 at 12:35 am

    You have changed the pointer address of ptr1 as you copied the string. So by the time you print out ptr1, it is actually pointing to the end of the string which is some garbage value.

    So what you need to do is to keep the ptr1 in the beginning of xstrcpy and return that start address and that will correctly print out the ptr1, I think.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a new web app that is packaged as a WAR as part
This is beyond both making sense and my control. That being said here is
I'm doing some changes on my routes, and suddenly the following is appearing in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.