Thread: Pointer output explanation

  1. #1
    Registered User
    Join Date
    Jun 2016
    Posts
    3

    Pointer output explanation

    I have assignment to find output of the source:

    Code:
        int a[7]={81,12,35,97,40,52,17}, *pa, x, y;
        pa=a+3;
        x=*(pa-2)+1;
        y=(*pa-2)+1;
    So I got in CodeBlocks and did this program:
    Code:
    #include <stdio.h>
    int main()
    {
        int a[7]={81,12,35,97,40,52,17}, *pa, x, y;
        pa=a+3;
        x=*(pa-2)+1;
        y=(*pa-2)+1;
    
        printf("\nValues are:\nx=%d\ny=%d\npa=%d",x,y,*pa);
    
        return 0;
    }
    The output was: x=13, y=96, pa=a+3 (its required to present it with a+ number). Problem is, I don't know how are those values calculated and if someone can explain it, it would be gr8.

    Edit 1: PS: Wondering if someone could tell me how can i determine size of pointer to int. I was using printf("%d",sizeof(int)); to get size of integer but how to do it if I want to see size of int *a ?
    Last edited by freakZ; 06-04-2016 at 10:49 AM. Reason: Adding more info

  2. #2
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    The output was: x=13, y=96, pa=a+3 (its required to present it with a+ number). Problem is, I don't know how are those values calculated and if someone can explain it, it would be gr8.
    So, what they're using is "dereferencing". C is awesome because it has pointers. If you work in languages that don't have pointers, you sometimes miss them. Or rather, I sometimes do.

    'a' is an allocation of memory on the stack. pa is a pointer to an integer type. So this means that pa stores an address in memory. At this address is an integer. It's kind of similar to houses, you know? Houses have addresses and while each one may have an address, each one can be radically different.

    We initialize pa with an offset of 12 bytes from the base location of a. I say 12 because when we write 'a + 3', we're saying that we want the 4th integral value using 'a' as a base. Remember that indexing has this weird off-by-one thing so that's why I say 4th value. In actuality, that's really 12 bytes away because sizeof(int) is 4 and 4 * 3 = 12. But we don't have to every worry about this. There's some auto-magic stuff that happens so you you're free to just write 'a + 3' and it'll mean the 4th value, no matter what sizeof(a) is.

    Dereferencing syntax is putting the * in front of the pointer (or arithemtic pointer operation [x's calculation is an example of this]). It gives you read and write access to a location in memory. In our case, we're simply storing a copy of the value. You can also write to dereferences which mutate the data at that address.

    So, x is asking for the (a + 3 - 2)'th value of the array which happens to be (a + 1) which is the second element which is 12. Great. 12 + 1 == 13.

    y wants the value at pa which is (a + 3) which is the 4th value which is 97. 97 - 2 + 1 = 97 - 1 == 96.

    Hopefully that explains it.

    Edit:

    And as for getting the size of a pointer, just sizeof(pa).

    Getting the sizeof(a) might give you something different because stack-based arrays like that are treated differently. Other posters could explain this aspect much better than I because they actually know what's going on.
    Last edited by MutantJohn; 06-04-2016 at 11:12 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 08-18-2015, 03:13 PM
  2. Explanation for the Output
    By soton1986 in forum C Programming
    Replies: 4
    Last Post: 04-09-2015, 12:29 AM
  3. Fork and wait. need output explanation
    By ollie88r in forum C Programming
    Replies: 18
    Last Post: 11-03-2009, 09:26 AM
  4. Output Explanation
    By anirban in forum C Programming
    Replies: 8
    Last Post: 01-20-2009, 07:10 AM
  5. Just some pointer explanation I need
    By blackswan in forum C Programming
    Replies: 6
    Last Post: 05-08-2005, 12:04 PM

Tags for this Thread