i cant understand why i get such an output??

This is a discussion on i cant understand why i get such an output?? within the C Programming forums, part of the General Programming Boards category; Code: int array[] = { 45, 67, 89 }; //1st ine int *array_ptr = &array[1]; //2nd line printf("%i\n", array_ptr[1]); //3rd ...

  1. #1
    Banned
    Join Date
    Oct 2008
    Posts
    1,535

    i cant understand why i get such an output??

    Code:
    int array[] = { 45, 67, 89 };    //1st ine
     int *array_ptr = &array[1];   //2nd line
    printf("%i\n", array_ptr[1]);   //3rd line
    the second line links the pointer *array_ptr with the value of cell "1"
    but in the third line it should have print the address of cell "1"
    not its value 89

    i think that if i want to print cell one we need to change this line into

    Code:
    printf("%i\n",*array_ptr[1]);

    why i get the value of cell "1"
    when by my logic i should get the adress of cell "1"

    ??

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Your logic needs quite a bit of work.

    Remember that [] not only adds the offset to the pointer (where the array name points to the first element of the array), but also dereferences. So array points to the first element, array[1] is the value of the second cell, and & gets its address. So array_ptr holds the address of the second cell.

    Now the third line adds an offset of 1 to array_ptr, to point to the third cell of the array, and then dereferences to get the value. If you want the address of that value, you'll have to use & -- which should surprise no one since that's exactly the same scenario is on line 2.

  3. #3
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    When you put [] at an array you get the value. If you want the address you just use the pointer. If you want the address of the next element you would do:
    Code:
    printf("%i",array_ptr+1);

  4. #4
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,832
    Following his example it should probably be in this form:
    Code:
    printf("%i\n", &array_ptr[1]);   //3rd line

  5. #5
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i know that &array_ptr[1] is the address of cell "1"

    if we want to print the value of cell one
    we need to use *array_ptr[1] (this one points to the value)

    but when we just want to print array_ptr[1] we will get the address to which
    array_ptr[1] is pointed to

    so i cant understand why when we execute this line:
    Code:
    printf("%i\n", array_ptr[1]);
    we get the value to which the pointer is pointed to
    instead of the address of the cell to which the ponter is pointed too
    ??

  6. #6
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,806
    Quote Originally Posted by transgalactic2 View Post
    i know that &array_ptr[1] is the address of cell "1"
    Correct (sort of, it's actually the second cell).

    Quote Originally Posted by transgalactic2 View Post
    if we want to print the value of cell one
    we need to use *array_ptr[1] (this one points to the value)
    Incorrect... array_ptr[1] is the value stored in cell two (array's are zero based so array_ptr[0] is actually the value stored in the "first" cell). array_ptr is a pointer to int, array_ptr[1] is the value in cell one, *array_ptr[1] does not make any sense in this context (maybe if array_ptr was declared as int **array_ptr or int *array_ptr[10] or somesuch it would).

    Quote Originally Posted by transgalactic2 View Post
    but when we just want to print array_ptr[1] we will get the address to which
    array_ptr[1] is pointed to
    No, as stated before, array_ptr[1] does not represent an address but rather a value at some address. array_ptr+1 on the other hand (or alternately, &array_ptr[1]) represents the address of the second cell.

    Quote Originally Posted by transgalactic2 View Post
    so i cant understand why when we execute this line:
    Code:
    printf("%i\n", array_ptr[1]);
    we get the value to which the pointer is pointed to
    instead of the address of the cell to which the ponter is pointed too
    ??
    Again, as stated, array_ptr[1] is the value located at an address which is why you get said value as output.

    [edit]
    Code:
    int array[4] = { 24, 36, 48, 64 };
    int * array_ptr = &array[1];
    
    printf("array_ptr[1] is  : %d\n",array_ptr[1]);
    printf("*(array_ptr+1) is: %d\n",*(array_ptr+1));
    printf("array_ptr+1 is   : %p\n",array_ptr+1);
    printf("&array_ptr[1] is : %p\n\n",&array_ptr[1]);
    
    printf("array[2] is      : %d\n",array[2]);
    printf("*(array+2) is    : %d\n",*(array+2));
    printf("array+2 is       : %p\n",array+2);
    printf("&array[2] is     : %p\n",&array[2]);
    Sample output on my machine:
    Code:
    array_ptr[1] is  : 48
    *(array_ptr+1) is: 48
    array_ptr+1 is   : 0012FF5C
    &array_ptr[1] is : 0012FF5C
    
    array[2] is      : 48
    *(array+2) is    : 48
    array+2 is       : 0012FF5C
    &array[2] is     : 0012FF5C
    [/edit]
    Last edited by hk_mp5kpdw; 10-16-2008 at 08:02 AM. Reason: Modified code sample a bit
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. code output...
    By roaan in forum C Programming
    Replies: 6
    Last Post: 07-03-2009, 03:22 AM
  2. Help for my output array
    By qwertysingh in forum C Programming
    Replies: 1
    Last Post: 02-17-2009, 03:08 PM
  3. Replies: 4
    Last Post: 11-30-2005, 04:44 PM
  4. Formatting output into even columns?
    By Uncle Rico in forum C Programming
    Replies: 2
    Last Post: 08-16-2005, 06:10 PM
  5. Output problems with structures
    By Gkitty in forum C Programming
    Replies: 1
    Last Post: 12-16-2002, 05:27 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21