array of ptrs vs constant ptr to an first element of array

This is a discussion on array of ptrs vs constant ptr to an first element of array within the C Programming forums, part of the General Programming Boards category; I'm unsure why both the array of ptrs and the 'regular' arr both work, shouldn't the former array, it should ...

  1. #1
    Registered User
    Join Date
    Jul 2012
    Posts
    87

    array of ptrs vs constant ptr to an first element of array

    I'm unsure why both the array of ptrs and the 'regular' arr both work, shouldn't the former array, it should be 'dereferenced' twice, once to pt the actual first address of first element and the second to access the actual content @ the first index?

    Code:
    int main()
    {
        int* arr_1[] = {1,22,3,55,6};
            
        printf("%d\n", *(arr_1 + 1));//output: 22 (shouldn't it be dereferenced twice?)
    
       /**VS*/
      
      int arr_2[] = {1,22,3,55,6};
      printf("%d\n", *(arr_2 + 1) );//same as arr_2[1]
            
       return 0;
    }
    EDIT: I think I see it now. arr_1 holds ptr elems (so addresses of the literals) versus arr_2 stores the literals in the array and we have just ONE ptr (as oppose to multiple ptrs in arr_1) that pts to first elem, so it's like glass half full, half empty. We have individual ptrs to pt to where each literal is stored in arr_1 w/ a ptr that defaults to first elem that we can move to traverse arr_2. Of course this is trivial use of arr of ptrs, it be more useful to store an array of ptrs to objects (so structs or even arrays).

    EDIT EDIT: I didn't notice that C is error prone unlike C++, I tried:
    Code:
    int* arr[] = {1,2,3}
    in C++ and it didn't compile. Srry for all the trouble.
    Last edited by monkey_c_monkey; 08-30-2012 at 03:45 PM.

  2. #2
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,261
    O_o

    This is like the tenth question you've asked lately about pointers and array.

    Have you considered, you know, getting a book and reading about how C works as opposed to guessing and then asking for clarification for a self-inflicted misunderstanding?

    This is exactly like some of your last posts. You are seeing a behavior you don't expect precisely because you've lied to the compiler and environment.

    The thing at `arr_1[1]' is an `int *' with the value 22; you've lied to the environment and told `printf' that what you've passed it is an `int'; the environment, `printf', treats the value as an `int' because you told it to treat the value as an `int'.

    Seriously now, what did you really expect to happen under those circumstances?

    Soma

  3. #3
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,706
    Code:
        printf("%d\n", *(arr_1 + 1));//output: 22 (shouldn't it be dereferenced twice?)
    I don't know, why are you asking me? It's pretty much like phantomotap said. In addition though, if you did dereference twice you would have gotten a nice segmentation fault, which is what normally happens when you treat a number you pulled out of your butt like a pointer.

  4. #4
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,261
    I didn't notice that C is error prone unlike C++, I tried:
    C isn't error prone. C++ isn't error prone.

    C++ does have a safer type system, but those are your errors; the compiler just didn't complain that you made them.

    Soma

  5. #5
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,384
    Correct me if I'm wrong here

    But I'd read this as:
    Code:
    int* arr_1[] = {1,22,3,55,6};
    //Declare arr_1 as an array of pointers to integers at addresses 1, 22, 3, 55 and 6 in each element
    Therefore,
    Code:
    *(arr_1 + 1)
    Returns the value you forced arr_1 to be - i.e. 22

  6. #6
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,384
    Is what you were trying to achieve?

    Code:
        int arr1[] = {1,2,3,4};
    
    
        int *arr2[sizeof(arr1)];
    
    
        arr2[0] = &arr1[0];
        arr2[1] = &arr1[1];
        arr2[2] = &arr1[2];
        arr2[3] = &arr1[3];
    
    
        printf("%d, %d\n", *(arr1+1), **(arr2+1));
    Last edited by Click_here; 08-30-2012 at 11:47 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointer to an element of array as array?
    By dan_paul in forum C Programming
    Replies: 4
    Last Post: 11-22-2011, 11:00 PM
  2. size of an array poited by array element
    By mitofik in forum C Programming
    Replies: 7
    Last Post: 12-23-2010, 11:09 PM
  3. Syntax for constant array of array pointers
    By BMintern in forum C Programming
    Replies: 4
    Last Post: 05-14-2008, 08:21 AM
  4. a few questions on array of ptrs
    By AngKar in forum C Programming
    Replies: 13
    Last Post: 01-09-2006, 05:48 AM
  5. passing 2D array of ptrs as parameter
    By Psycho in forum C++ Programming
    Replies: 3
    Last Post: 06-13-2003, 10:13 AM

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