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 9045953
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T11:29:58+00:00 2026-06-16T11:29:58+00:00

Hi I got confused to get the explanation for this below, can anyone explain

  • 0

Hi I got confused to get the explanation for this below, can anyone explain it to me? Thanks in advance.

#include<stdio.h>
int main(){
    char *arr,c;
    arr = &c;

    arr++;
    *arr = 'a';

    arr++;
    *arr = 'b';

    arr++;
    *arr = 'c';

    arr--;
    arr--;

    printf("\narr 1 = %c",arr);
    printf("\narr 2 = %c",arr[1]);
    printf("\narr 3 = %c",arr[2]);
    getch();
    return 1;
    }

Output is :
arr 1 = a
arr 2 = b
arr 3 = c

but if the change the line:

printf("\narr 1 = %c",arr);

with

printf("\narr 1 = %c",arr[0]);

Now Output is:
arr 1 =  
arr 2 = b
arr 3 = c

why ‘a’ is not getting printed.?

*For all those who are questioning the program as bad coding.. I know its not a good coding practice to use pointer like this, But my question is why arr[0] is not printing anything where as arr[1] & arr[2] is printing what is assigned?

  • 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-06-16T11:30:06+00:00Added an answer on June 16, 2026 at 11:30 am

    In the first version of the program, what likely happens is:

    • The compiler puts c at some address on the the stack, say 1003 (a single byte).
    • The compiler puts arr at some address on the stack, say 1004-1007 (four bytes).
    • The statement arr = &c; puts the address of c, 1003, into arr.
    • The statement arr++; adds one to arr, so it is now 1004 instead of 1003.
    • The statement *arr = 'a'; writes 'a' to the place where arr points. This is now behavior that is undefined by the C standard. When arr was set to &c, there was only one byte in the object at that location, and the C standard does not guarantee what happens when you write to other locations after incrementing that address outside the object.
    • As it happens in this instance, 1004 points into arr, so you are writing 'a' into one of the bytes of arr. This horribly alters the address that was in arr.
    • Then arr++; increments the address again, and *arr = 'b'; writes 'b' somewhere. We do not know where, because arr has a bad value in it.
    • Then arr++; increments the address again, and *arr = 'c'; writes 'c' somewhere. We do not know where.
    • Then arr--; and arr--; return the address to the value it had after 'a' was written.
    • Then the first printf call passes the value in arr to be printed with a “%c” specifier. “%c” is for printing a character, but the arr you pass it is a pointer. However, you previously wrote 'a' into the pointer, so the value of the pointer has 'a' in one of its bytes.
    • As it happens, when the “%c” specifier is used with a pointer, this implementation of printf prints one of the bytes in the pointer value, in this case the same byte where you wrote 'a'. So “a” is printed. You cannot rely on this happening in other C implementations. This happened in this case only because the compiler happened to place arr in memory just after c in memory, so the statement *arr = 'a'; wrote 'a' into arr. This will not happen in all C implementations. You got “lucky.”
    • After the write of 'a' into arr, arr is pointing somewhere, although we do not know where. Let’s call this location x.
    • The statements arr++; and *arr = 'b'; wrote 'b' to the location x+1.
    • The statements arr++; and *arr = 'c'; wrote 'c' to the location x+2.
    • Then two arr--; statements returned arr to point to x. So arr[1] is the character at x+1, which is 'b', and arr[2] is 'c'. Passing these to printf to be printed with “%c” prints “b” and “c”.

    In the second version of the program, when arr[0] is passed to printf:

    • The value of arr is the value that was formed by writing 'a' into one of its bytes. This is some “random” address, pointing to no location we have controlled. Apparently there is a 0 character there, or some other non-printing character, so, when arr[0] is passed to printf, nothing is printed.
    • Passing arr[1] and arr[2] print “b” and “c” because the code wrote 'b' and 'c' to those locations.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I got confused while I was reading the explanation below on emberjs.com. I typed
I was actually going through the question 1636644 .. and got confused with this
Got confused why this is giving me error I think my code is correct
I tried using the example mentioned in below url. but got confused how they
I got confused in C++ class definition. Can someone help me out? I have
I have tried searching for it online, but I got confused. I didn't get
I get confused by preventDefault(). I got the following working as my test $('.next').click(function(e){
I am somehow aware of returning by reference by this got me confused. I
I've been following along with this excellent asio tutorial , but have got confused
I got confused with Git !! I have some files which have added and

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.