Thread: array == pointer ?

  1. #1
    flashing vampire black's Avatar
    Join Date
    May 2002

    array == pointer ?


    i heard that array are actually a pointer, so i test it and all seems that remarks told the truth like this:
    int arr[100];
    std::cout << arr << std::endl;
    std::cout << &arr << std::endl;
    std::cout << &arr[0] << std::endl;
    all three of them have the same output, which make me believe arr is absolutely a pointer itself. but if we declare a dynamic array this time, just like this:
    int n = 100;
    int* arr = new int(n);
    std::cout << arr << std::endl;
    std::cout << &arr << std::endl;
    std::cout << &arr[0] << std::endl;
    to my surprise this time output is different from the former, here arr and &arr[0] are still be equivalent, but &arr show me another result, if the assumption which regards array as pointer didnt go wrong then the output for &arr and arr may be the also the same because they are all points to same area, what's wrong here ??? very confused...
    Never end on learning~

  2. #2
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    When you declare array like this:

    int array[100];

    name array is actually pointer to the first element of that array.

    When you say:
    int *array=new int (n);
    that is same like

    int *array;
    array=new int(n);
    that means array is a pointer (variable just like other has it's value and adress).
    int *array will create variable (pointer) on some address say it is 0012FD44. And it' s value will be address of some other variable because it is pointer.
    To simplify our vaariable (pointer array) will be placed on adress
    0012FD44 and after array=new int(n);
    it value will be adress of first byte of allocated memory for example 003708F8
    0012FD44|003708F8|--------------->003708F8 ....etc
    So in first case compiler reserve 100*sizeof(int) bytes of memory where name array is actually pointer on the first element of first byte,
    in the second case you have two things:
    -first compiler create variable which is pointer variable it has it's own adress
    -second you dinamically allocate memory and new returns adress that will be stored in your variable array.

    I'm not sure if I make this clear to you, because English is not my native language, but you must understand that int array[], and allocating with new or malloc are not equivalent things.

  3. #3
    Registered User
    Join Date
    Jan 2003
    int array[7];

    to the compiler array is an immedeate value of type address of int. That is
    int array[7];
    int a = 7;
    int *p = array;
    in this case 7 is an int, but it is not a variable of type int. The variable of type int is assigned the value 7, in exactly the same way as the pointer to int is assigned the value array. There is no spoon.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    >i heard that array are actually a pointer
    You heard wrong. Arrays are often converted to a pointer to the first element, but in an object context, the two are different.

    >what's wrong here ???
    Your understanding of context. The Rule states that in value context an array name is converted to a pointer to the first element of the array. This is why subscripting is often compared to dereferencing a pointer offset. It's an expression with value context, so a[i] really does become *(a + i). Likewise, a function call is also a value context, so you can pass an array to a function that expects a pointer to the appropriate type:
      int *p
      cout<< *p <<endl;
      int a[] = {1,2,3,4,5};
    So an array becomes a pointer to the first element of the array when in value context. Value context is everything except for two cases:

    1) As an operand to the sizeof operator.
    2) As an operand to the address-of operator.

    For arrays, a third case where a string literal is used as an initializer for an array of char also exists. Arrays are not pointers or constant pointers or anything like that, they're arrays. However, in practical use arrays are closely related to pointers simply because they become pointers much of the time.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. non-lvalue error
    By zlb12 in forum C Programming
    Replies: 1
    Last Post: 04-17-2009, 10:43 AM
  2. process killer on a loop
    By Anddos in forum Windows Programming
    Replies: 8
    Last Post: 01-11-2006, 01:50 AM
  3. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  4. Massive Function Problem
    By Marc Sharp in forum C Programming
    Replies: 10
    Last Post: 11-19-2003, 08:49 PM
  5. swapping elements in a 2D array
    By axon in forum C++ Programming
    Replies: 9
    Last Post: 03-10-2003, 02:18 PM