Understanding Pointers to Arrays in C++ [duplicate]

In C++, there is a concept of a pointer to an array, where it is said to point to the “whole array” rather than just the first element. This idea confuses me. How can a pointer point to an entire array? Shouldn’t it only point to the first element?

10

Lets draw it out for you…

First we have out array:

+--------+--------+-----+--------+
| arr[0] | arr[1] | ... | arr[9] |
+--------+--------+-----+--------+

Now lets add some pointers:

         +--------+--------+-----+--------+
&arr --> | arr[0] | arr[1] | ... | arr[9] |
         +--------+--------+-----+--------+
         ^        ^              ^
         |        |              |
         &arr[0]  &arr[1]        &arr[9]

Here we can see that &arr will point to the array, but it’s a pointer to the whole array, not a pointer to a single element like &arr[0].


To perhaps show it in another way: If you don’t know, then arr[i] is exactly the same as *(arr + i). That is, you take a pointer to the first element and add the index to get a pointer to that element, then dereference it.

The important part here is that arr + 1 is a pointer to an element.

Now lets say we store the pointer to the array in a variable:

int (*arr_ptr)[10] = &arr;

With this we know that arr_ptr[0] is the same as *(arr_ptr + 0) which is the same as *arr_ptr.

What we also know that arr_ptr[1] will be the same as *(arr_ptr + 1).

Lets take the drawing above, and add the pointers arr_ptr and arr_ptr + 1:

            +--------+--------+-----+--------+
arr_ptr --> | arr[0] | arr[1] | ... | arr[9] | ... unknown memory
            +--------+--------+-----+--------+
            ^        ^              ^        ^
            |        |              |        |
            &arr[0]  &arr[1]        &arr[9]  arr_ptr + 1

This drawing shows that arr_ptr (and thus &arr) is a pointer to the whole array.

6

What really changes is the behavior of pointer arithmetic, incrementing/decrementing or adding/subtracting an offset to the pointer, because they are two different types of pointers.

#include <stdio.h>

int main()
{
    int arr[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
    void *arrPlusOne = (void *)(&arr+1);
    void *arrItemPlusOne = (void *)(&arr[0]+1);
    printf("%p %p %p %pn", (void *)&arr, (void *)&(arr[0]), arrPlusOne, arrItemPlusOne);
    
    printf("array pointer step: %ldn", ((char *)arrPlusOne - (char *)&arr));
    printf("array item pointer step: %ldn", ((char *)arrItemPlusOne - (char *)&arr));
    
    return 0;
}

The output might be something like this:

0x7fff4b58ce80 0x7fff4b58ce80 0x7fff4b58cea8 0x7fff4b58ce84
array pointer step: 40
array item pointer step: 4

The cast to char pointer is to allow arithmetic subtraction which is not allowed on void pointers.

Cast to void pointers in printf() is required because is a variadic function.

If you run this code you will see that the third and fourth pointers point to different memory locations even though the code always adds 1, depending on the processor architecture and type sizes.

0

Much like a class/struct can be thought of as a container of data members, an array can be thought of as a container of elements (and in fact, std::array is part of the standard Containers library).

You can have a pointer to a single member/element, and you can have a pointer to the entire container as a whole. Both pointers may point at the same address in memory, but the things they are pointing at are logically and semantically different things.

A pointer tells you not only WHERE it is pointing at, but WHAT it is pointing at.

It will become evident from the following test:

#include <stdio.h>

int main() {
    int a[10];
    printf("%lu, %lu", sizeof(a), sizeof(a[0]));
    return 0;
}

Output is:

40, 4

Therefore, an int array of 10 elements have a size of 40, whereas a single int has a size of 4. Therefore the size of your array is:

size_of_unit * number_of_units

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa

Understanding Pointers to Arrays in C++ [duplicate]

In C++, there is a concept of a pointer to an array, where it is said to point to the “whole array” rather than just the first element. This idea confuses me. How can a pointer point to an entire array? Shouldn’t it only point to the first element?

10

Lets draw it out for you…

First we have out array:

+--------+--------+-----+--------+
| arr[0] | arr[1] | ... | arr[9] |
+--------+--------+-----+--------+

Now lets add some pointers:

         +--------+--------+-----+--------+
&arr --> | arr[0] | arr[1] | ... | arr[9] |
         +--------+--------+-----+--------+
         ^        ^              ^
         |        |              |
         &arr[0]  &arr[1]        &arr[9]

Here we can see that &arr will point to the array, but it’s a pointer to the whole array, not a pointer to a single element like &arr[0].


To perhaps show it in another way: If you don’t know, then arr[i] is exactly the same as *(arr + i). That is, you take a pointer to the first element and add the index to get a pointer to that element, then dereference it.

The important part here is that arr + 1 is a pointer to an element.

Now lets say we store the pointer to the array in a variable:

int (*arr_ptr)[10] = &arr;

With this we know that arr_ptr[0] is the same as *(arr_ptr + 0) which is the same as *arr_ptr.

What we also know that arr_ptr[1] will be the same as *(arr_ptr + 1).

Lets take the drawing above, and add the pointers arr_ptr and arr_ptr + 1:

            +--------+--------+-----+--------+
arr_ptr --> | arr[0] | arr[1] | ... | arr[9] | ... unknown memory
            +--------+--------+-----+--------+
            ^        ^              ^        ^
            |        |              |        |
            &arr[0]  &arr[1]        &arr[9]  arr_ptr + 1

This drawing shows that arr_ptr (and thus &arr) is a pointer to the whole array.

6

What really changes is the behavior of pointer arithmetic, incrementing/decrementing or adding/subtracting an offset to the pointer, because they are two different types of pointers.

#include <stdio.h>

int main()
{
    int arr[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
    void *arrPlusOne = (void *)(&arr+1);
    void *arrItemPlusOne = (void *)(&arr[0]+1);
    printf("%p %p %p %pn", (void *)&arr, (void *)&(arr[0]), arrPlusOne, arrItemPlusOne);
    
    printf("array pointer step: %ldn", ((char *)arrPlusOne - (char *)&arr));
    printf("array item pointer step: %ldn", ((char *)arrItemPlusOne - (char *)&arr));
    
    return 0;
}

The output might be something like this:

0x7fff4b58ce80 0x7fff4b58ce80 0x7fff4b58cea8 0x7fff4b58ce84
array pointer step: 40
array item pointer step: 4

The cast to char pointer is to allow arithmetic subtraction which is not allowed on void pointers.

Cast to void pointers in printf() is required because is a variadic function.

If you run this code you will see that the third and fourth pointers point to different memory locations even though the code always adds 1, depending on the processor architecture and type sizes.

0

Much like a class/struct can be thought of as a container of data members, an array can be thought of as a container of elements (and in fact, std::array is part of the standard Containers library).

You can have a pointer to a single member/element, and you can have a pointer to the entire container as a whole. Both pointers may point at the same address in memory, but the things they are pointing at are logically and semantically different things.

A pointer tells you not only WHERE it is pointing at, but WHAT it is pointing at.

It will become evident from the following test:

#include <stdio.h>

int main() {
    int a[10];
    printf("%lu, %lu", sizeof(a), sizeof(a[0]));
    return 0;
}

Output is:

40, 4

Therefore, an int array of 10 elements have a size of 40, whereas a single int has a size of 4. Therefore the size of your array is:

size_of_unit * number_of_units

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật