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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T14:59:45+00:00 2026-06-01T14:59:45+00:00

I have these two statements : printf(%u,a+1); and printf(%u,(int *)a+1); Actually I was working

  • 0

I have these two statements :

printf("%u",a+1);

and

printf("%u",(int *)a+1);

Actually I was working on this code when I came across this confusion.

#include<stdio.h>

int main()
{
  int a[2][2]={1,2,3,4};
  int i,j;
  int *p[] = { (int*)a, (int*)a+1, (int*)a+2 };
  for(i=0; i<2; i++){
    for(j=0; j<2; j++){
      printf("%d %d %d %d",* (*(p+i)+j), *(*(j+p)+i), *(*(i+p)+j), *(*(p+j)+i));
    }
  }
  return 0; 
}

Output:

1 1 1 1
2 2 2 2
2 2 2 2
3 3 3 3

In order to understand the output of the above program I came to know that the difference that’s making this output can be solved if I know the difference between above two statements.

My current understanding:
(a+1) will give me the address of 2nd element of array. In this case a 2-d array can be visualized as 2 1-d arrays, each with 2 elements. So (a+1) will give me the address of a[1][0], but then why is (int *)a+1 giving me the address of a[0][1]?

Please explain the difference and the output of the program.

Thanks.

  • 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-01T14:59:46+00:00Added an answer on June 1, 2026 at 2:59 pm

    The idiom (int*)a+1 is interpreted as ((int*)a) + 1). That is, the cast takes precedence over the addition. So this evaluates to (int*) a), which is the address of the array as ptr-to-int, offset by 1, which returns the second element in the array (2).

    Two critical rules of programming:

    Rule 1: When you write code, make the layout reflect the functionality.
    Rule 2: When you read code, read the functionality, not the layout. (Corollary: debug the code, not the comments.)

    Conceptually, when you declare

    int a[2][2]={1,2,3,4};
    

    you envision a 2 dimensional array like this:

      1 2
      3 4
    

    But C actually stores the data in a contiguous block of memory, like this:

      1 2 3 4
    

    It “remembers” that the data represents a 2×2 array when it calculates the indices. But when you cast a from its original type to int *, you’re telling the compiler to forget about its original declaration, effectively losing its 2-dimensionality and becoming a simple vector of ints.

    Here’s how to understand the declaration of p:

    int *p[] = { (int*) a,  (int*) a+1,     (int*) a+2 };     // As written
    int *p[] = { (int*) a,  ((int*) a) + 1, ((int*) a) + 2 }; // As interpreted
    int *p[] = { &a[0][0],  &a[0][1],       &a[1][0] };       // Resulting values
    

    From this, you can see that p is a one-dimensional array of vectors:

    p[0] = { 1, 2, 3 }
    p[1] = { 2, 3 }
    p[2] = { 3 }
    

    If you recognize that (p+i) == (i+p), then the last two items are the same as the first two in the line

    printf("%d %d %d %d\n",* (*(p+i)+j), *(*(j+p)+i), *(*(i+p)+j), *(*(p+j)+i));
    

    which is equivalent to this:

    printf("%d %d %d %d\n", p[i+j], p[j+i], p[i+j], p[j+i]);
    

    It’s interesting to note that, since the following are all equivalent:

    a[i]
    *(a+i)
    *(i+a)
    

    then it’s perfectly legal to write i[a] to represent the same value. In other words, the compiler allows you to write

    printf("%d %d %d %d\n", p[i], i[p], p[1], 1[p]);
    

    Of course, your tech lead had better not allow you to write that. If you write that in my group, you’re fired. 😉

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

Sidebar

Related Questions

I have these two pieces of code, wich one is more readable? foreach decimal
I have a class named A . What the difference between these two statements?
I have these two modules : package G1; sub new { my $class =
I have these two classes public class Person { } public class Company {
I have these two situations: String s = aa; s = s + aa;
I have these two methods on a class that differ only in one method
I have these two functions (with Point2D & LineVector (has 2 Point2D member variables)
I have these two images. And... I made them into graysclae images and then
I have these two URL's I want to make available to my users: /Account/Orders
I have these two data structures that I continually find myself choosing between when

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.