sizeof (array name) and sizeof (pointer)

Source: Internet
Author: User

In doing this problem:

32-bit environment, int *p=new int[10]; the value of sizeof (P) is ()
A, 4 B, ten C, D, 8

I thought the correct answer for the C,int type is 32 bits, four bytes, 10 natural is 40, the result is the correct answer is a, only the space of the pointer p.

So write a piece of code to test it:

1#include <iostream>2 using namespacestd;3 voidFunintP[])//p use as pointer here4 {5cout<<"in the function"<<sizeof(P) <<Endl;6 }7 intMain ()8 {9 inta[Ten];Ten int* b=New int[Ten]; Onecout<<"Array name"<<sizeof(A) <<Endl; Acout<<"Pointers"<<sizeof(B) <<Endl; - Fun (A); -}

Or

1#include <iostream>2 using namespacestd;3  void fun (int *P) 4 {5cout<<"in the function"<<sizeof(P) <<Endl;6 }7 intMain ()8 {9     inta[Ten];Ten     int* b=New int[Ten]; Onecout<<"Array name"<<sizeof(A) <<Endl; Acout<<"Pointers"<<sizeof(B) <<Endl; - Fun (A); -}

Result output:

Array name 40
Pointer 4
In the function 4

This shows that the array name is not exactly the same as the pointer. Although they can all access the array by pointers.

However, arrays are degenerate into pointers as they are passed as function parameters. This is why the pointer is often a length when passed as a parameter. (WSJ Note: When the pointer is a formal parameter, it is usually accompanied by a formal parameter-the length of the pointer)

Transferred from: http://blog.csdn.net/kangroger/article/details/20653255

sizeof (array name) and sizeof (pointer)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.