指针VS数组指针 [英] Pointer VS array pointer
问题描述
//示例
#include< iostream>
使用命名空间std;
size_t bsearch(int * a){
cout<< a<< endl;
cout<< sizeof(a)/ sizeof (a [0])<< endl;
返回1;
}
int main(){
int a [] = {1,3,5,7,9,11};
cout<< a<< endl;
cout<< sizeof(a)/ sizeof(a [0])<< endl ;;
cout<< bsearch(a);
}
为什么它们不同?
a具有相同的价值,但为什么sizeof(a)/ sizeof(a [0])有区别吗?
谢谢
howa写道:
>
size_t bsearch(int * a){
a是指向int的指针。
int a [] = {1,3,5,7, 9,11};
a是一个6元素的int数组。
>
a具有相同的值,但为什么sizeof(a)/ sizeof(a [0])不同?
不,他们没有相同的价值。数组和
指针是不同的类型。
howa写道:
/ /示例
#include< iostream>
使用命名空间std;
size_t bsearch (int * a){
cout<< a<< endl;
cout<< sizeof(a)/ sizeof(a [0 ])<< endl;
返回1;
}
int main(){
int a [] = {1,3,5,7,9,11};
cout<< a<< endl;
cout<< sizeof(a)/ sizeof(a [0])<< endl ;;
cout<< bsearch(a);
}
为什么它们不同?
第一个是指针,第二个是数组。它们是不同的
东西。
在许多情况下,数组的名称首先被转换为指向
的指针元件。两个主要的例外情况是与sizeof
运算符和地址运算符(&)一起使用。
Brian
howa写道:
//示例
#include< iostream>
使用命名空间std;
size_t bsearch(int * a){
这里的a是指向int的类型。 int指向
的事实恰好是存储在数组中的一系列整数中的第一个
在调用函数中与a的类型无关这里。
cout<< a<< endl;
此语句调用运算符<<函数重载指针
因为a是一个指针。
cout<< sizeof(a)/ sizeof(a [0]) << ENDL;
a是指向int的类型
a [0]的类型为int
此声明将输出指针到int的大小除以int的
大小,无论你的实现是什么。
> ;
返回1;
}
int main(){
int a [] = {1,3,5,7,9,11};
这里a是6个整数的数组
cout<< a<< endl;
此语句调用运算符<<函数重载指针
因为在调用的上下文中从数组衰减到指针
到运算符<<功能。所以你看到与
bsearch函数内部相同的结果。
cout<< sizeof(a)/ sizeof(a [0 ])<< ENDL ;;
a有类型array-of-6-ints并且在sizeof表达式的上下文中
数组名称_not_衰减为指针就像在调用
运算符<<以上。所以sizeof是整个数组的大小,是int的大小的6 x / b $ b大小。 a [0]的类型为int,因此这个语句将输出(按int的大小分别为6 x
大小),即输出为
6.
cout<< bsearch(a);
}
为什么它们不同?
a有相同的值,但为什么sizeof(a)/ sizeof(a [0])是不同的?
因为在一种情况下a是六个整数的数组而在另一种情况下a
是指针而这两个实体不是大小相同。 (在两个
的情况下,[0]是一个int,所以在每种情况下都是相同的大小)
Gavin Deane
// example
#include <iostream>
using namespace std;
size_t bsearch(int *a) {
cout<<a<<endl;
cout<<sizeof(a) / sizeof(a[0])<<endl;
return 1;
}
int main() {
int a[] = {1,3,5,7,9,11};
cout<<a<<endl;
cout<<sizeof(a) / sizeof(a[0])<<endl;;
cout<< bsearch(a);
}
why they are difference ?
a has the same value, but why sizeof(a) / sizeof(a[0]) is difference?
thanks
howa wrote:
>
size_t bsearch(int *a) {a is a pointer to int.
int a[] = {1,3,5,7,9,11};
a is a 6 element array of int.
>
a has the same value, but why sizeof(a) / sizeof(a[0]) is difference?No they do not have the same value. Arrays and
pointers are different types.
howa wrote:
// example
#include <iostream>
using namespace std;
size_t bsearch(int *a) {
cout<<a<<endl;
cout<<sizeof(a) / sizeof(a[0])<<endl;
return 1;
}
int main() {
int a[] = {1,3,5,7,9,11};
cout<<a<<endl;
cout<<sizeof(a) / sizeof(a[0])<<endl;;
cout<< bsearch(a);
}
why they are difference ?The first is a pointer and the second is an array. They are different
things.
In many contexts, the name of an array is converted to a pointer to the
first element. The two major exceptions are when used with the sizeof
operator and the address-of operator (&).
Brian
howa wrote:// example
#include <iostream>
using namespace std;
size_t bsearch(int *a) {Here a is of type pointer-to-int. The fact that the int pointed to
happens to be the first in a series of ints that are stored in an array
in the calling function is irrelevant to the type of a here.
cout<<a<<endl;This statement calls the operator<< function overloaded for pointers
because a is a pointer.
cout<<sizeof(a) / sizeof(a[0])<<endl;a is of type pointer-to-int
a[0] is of type int
This statement will output the size of a pointer-to-int divided by the
size of an int, whatever that is in your implementation.
>
return 1;
}
int main() {
int a[] = {1,3,5,7,9,11};Here a is an array of 6 ints
cout<<a<<endl;This statement calls the operator<< function overloaded for pointers
because a decays from an array to a pointer in the context of the call
to the operator<< function. So you see the same result as inside the
bsearch function.
cout<<sizeof(a) / sizeof(a[0])<<endl;;a has type array-of-6-ints and in the context of a sizeof expression
the array name does _not_ decay to a pointer as it did in the call to
operator<< above. So sizeof a is the size of the whole array, 6 x the
size of int. a[0] is of type int so this statement will output (6 x the
size of an int) divied by the size of an int, i.e. the output will be
6.
cout<< bsearch(a);
}
why they are difference ?
a has the same value, but why sizeof(a) / sizeof(a[0]) is difference?Because in one case a is an array of six ints and in the other case a
is a pointer and those two entities are not the same size. (In both
cases a[0] is an int so is the same size in each case)
Gavin Deane
这篇关于指针VS数组指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!