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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T10:02:00+00:00 2026-05-25T10:02:00+00:00

I have some code (below) where I am assigning the address of an integer

  • 0

I have some code (below) where I am assigning the address of an integer array to a pointer. I am assigning in two ways and expect two different outputs but in both cases the output is the same.

main()
{
   int a[]={11,2,3};
   int *p=&a;
   printf("%d",*(p+1));
}

It should give me some garbage value since p is pointing to next 1D array

main()
{
   int a[]={11,2,3};
   int *p=a;
   printf("%d",*(p+1));
}

This should print 2.

However BOTH of these functions print 2.

This is not what I expected. Can anyone explain what’s happening?

Can anybody tell how i make pointer pointing to whole array than to just individual element.

  • 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-25T10:02:00+00:00Added an answer on May 25, 2026 at 10:02 am

    If you compile the first program you will get a compiler warning:

     warning: initialization from incompatible pointer type
    

    Because the type of p is pointer to integer but the type of &a is pointer to array of integer. Compiling the second program is fine, because againphas typeint*andahas typeint[]` (i.e., array of integer) but in C, you can pass/assign an integer array to an integer pointer just fine. The value of the pointer is assigned the starting address of the array.

    When you define an array with square brackets in C, the array is placed immediately in the stack or data section, so the starting address of the array is the same as the address of the array variable itself! This is different from arrays created with malloc.

    What this means for you is this: Suppose the variable a is allocated at address 4e78ccba. This means that &a is 4e78ccba. But in C, the value of a when passed/assigned to a pointer is by definition the address of the beginning of the array, so this is also 4e78ccba.

    Again, if you set things up with malloc things will be different. (Try it!!) And by the way, pay attention to the warnings. 🙂 This is an interesting question because C is so weakly typed it only gave you a warning in the first case. A stronly typed language would not have liked the mixing of an integer pointer with an integer array pointer.

    ADDENDUM

    Here is some code that shows how you can actually differentiate the pointer to the first element and the pointer to the whole array:

    #include <stdio.h>
    int main() {
        /* a is an array of ints, but can be used as a pointer to int */
        int a[] = {11, 2, 3};
    
        /* Using a as an int pointer */
        int* p = a;
        printf("%d\n", *(p+1));
    
        /* Now here is how to make a pointer to the array itself! */
        int (*q)[] = &a;
        printf("%d\n", (*q)[2]);
    
        return 0;
    }
    

    This program outputs

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

Sidebar

Related Questions

I have some really funky code. As you can see from the code below
I have some problem with my cfml website. I have used the below code
I have some code which utilizes parameterized queries to prevent against injection, but I
I have some very simple code below that I can't get to validate on
I have some code that adds a flag to an email but when I
I have some code below that is accessing the registry. The application is built
I have written some code to check two dates, a start date and an
I use PHP to generate XML files. I have use some code below to
I have some code like belows. This works but I think it's not clear
I have some code below that creates a RelativeLayout and adds a button to

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.