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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T05:29:33+00:00 2026-05-16T05:29:33+00:00

We have int a[5]={10, 20, 30, 40, 50}; I would like to know how

  • 0

We have

 int a[5]={10, 20, 30, 40, 50};

I would like to know how does the following two code segment do?

 int *ptr = (int *)(&a+1);
 int *t = (int *)(&a -1);

If we have

 printf("%d  %d  %d \n", *(a+1), *(ptr-1), *(t+1));

What should be the result?

  • 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-16T05:29:33+00:00Added an answer on May 16, 2026 at 5:29 am

    All the problems come from the use of &a, which is a pointer to “an array of five integers”, so that pointer arithmetic (when you think in terms of addresses) gets “scaled” by sizeof(a) (which might e.g. be 20 if int are 4 bytes and the compiler needs no padding for alignment purposes — reasonable hypotheses, though far from certain of course.

    So, after

    int *ptr = (int *)(&a+1);
    int *t = (int *)(&a -1);
    

    ptr is a pointer to int at the memory address “sizeof(a) more than the address a”, and t similarly for “sizeof(a) less than the address of a”. Therefore…:

     printf("%d  %d  %d \n", *(a+1), *(ptr-1), *(t+1));
    

    What should be the result?

    Quite possibly a segmentation violation, otherwise 20 followed by two completely arbitrary integer values. Since ptr and t are pointers to int, the address arithmetic scaling for their -1 and +1 does not compensate that done on &a (the scaling in terms of memory addresses is by sizeof(int), not sizeof(a)!), so ptr-1 and t+1 are pointing to (alleged;-) ints that are respectively “a few ints after the end of a” and “a few ints before the start of a“.

    There’s no way to know whether at those arbitrary addresses there is any memory which the process is allowed to address (whence the possibility for segmentation violatons), and, if any accessible memory is there, what its contents “seen as an int” might possibly be.

    Edit: @caf points out that ptr - 1 is not invalid — it correctly points to the last element of a; so the output (unless there’s a segmentation fault, which @NullUserException thinks is very unlikely but on this point we disagree;-) would start with 20 50 before the third, “arbitrary” junk. Point is, per the C standard, it is valid to compute (though not to use) the pointer “just one past the end” of an array, and the sizeof an array must be exactly that array’s length time the sizeof its elements (padding is allowed for an element’s type, if needed, and if so it shows in the element’s own sizeof, but not for the array as a whole). Subtle, but important;-).

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

Sidebar

Related Questions

I would like to know the best way to determine if the following code
I know what the following function does I would just like an explanation of
I would like to know if the following code are valid. The original intension
I have an int* of rgb data,width, height, and scanlinestride where i would like
How exactly does fork() work? The following code #include <stdio.h> int main (int argc,
I have the following piece of code: public void AddHash( int val ) {
Why does the following code compile and run. I have created an integer array
I have the following classes. For testing purpose, I would like to get all
Question: I receive the following error for the code below, does anyone know why?
I have following simple program: import std.stdio; int main(string[] argv) { writeln(Hello, world!); return

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.