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

  • SEARCH
  • Home
  • 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 7001105
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T20:45:38+00:00 2026-05-27T20:45:38+00:00

I found a question somewhere … here it is and its answer with explanation.

  • 0

I found a question somewhere … here it is and its answer with explanation.

main()
{
char *p="hai friends",*p1;
p1=p;
while(*p!='\0') ++*p++;
printf("%s   %s",p,p1);
}

 Answer:
 ibj!gsjfoet

 Explanation:
++*p++ will be parse in the given order

*p that is value at the location currently pointed by p will be taken

++*p the retrieved value will be incremented

when ; is encountered the location will be incremented that is p++ will be executed

Hence, in the while loop initial value pointed by p is ‘h’, which is changed to ‘i’ by executing ++*p and pointer moves to point, ‘a’ which is similarly changed to ‘b’ and so on. Similarly blank space is converted to ‘!’. Thus, we obtain value in p becomes “ibj!gsjfoet” and since p reaches ‘\0’ and p1 points to p thus p1 doesnot print anything.

I found something wrong with explanation on p1.I think p1 should print “hai friends” and output of p is fine as given.

but when I tried to run the same code on gcc compiler,its giving segmentatiion fault

here is the exact code which I tried to run ..

 #include<stdio.h>
 int main()
 {
 char *p="hai friends",*p1;
 p1=p;
 while(*p !='\0') ++*p++;
 printf("%s   %s",p,p1);
 return 0;
 }

If possible edit the title ,I could not find a suitable title which would explain the situation more clearly.

EDIT :

I tried to run the modified code as suggested by Mysticial ,But what I think output should be –

ibj!gsjfoet    hai friends

because I am incrementing only p0 but p1 should be as its initial place i.e. at the starting address of string.Please if anyone could explain it where I am getting it wrong ???

  • 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-27T20:45:38+00:00Added an answer on May 27, 2026 at 8:45 pm

    Well for one, code like this: ++*p++ should be avoided because it’s generally hard to read.

    Secondly, the problem is that you are modifying a string literal. That’s undefined behavior. Don’t do it.

    Instead, change your declaration to this:

    char p[] = "hai friends";
    char *p1;
    

    and modify you code as such:

    int main()
    {
    
        char p[] = "hai friends";
        char *p0,*p1;
        p0 = p;
        p1 = p;
        while(*p0 !='\0') ++*p0++;
        printf("%s   %s",p0,p1);
        return 0;
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I found a question here about blogs on software development, but I would like
I searched and found this question but did not like the answer. Is there
I haven't found this question, feel free to close if it's already up here.
I've been trying to find the answer to this question here. Several people seem
Question: I use the bidirectional dicionary class I found here: Bidirectional 1 to 1
This should be a fairly common question, but I haven't found a straightforward answer
My question is almost identical to an earlier entry I found here but not
My question I believe is pretty simple, yet I haven't found a good answer.
I found this question in an old question in your website so i thought
I found this question that is discussing what I would like to do, but

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.