首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

数组名是什么解决思路

2012-05-20 
数组名是什么#includestdio.h#includestring.hvoid main(){char a[]hello worldprintf(%d\n,siz

数组名是什么
#include<stdio.h>
#include<string.h>
void main()
{

char a[]="hello world";
  printf("%d\n",sizeof(a));
printf("%d\n",a);
printf("%d\n",&a);//数组名应该是指针常量(只读指针变量),存储在栈中,为什么&a和a的值是一样的呢
}

[解决办法]
因为数组的地址就是数组首元素的地址。
[解决办法]
1. a,&a[0]和&a的值都是一样的,但数据类型是不一样的
2. a和&a[0]的含义类似,都是数组中第一个元素的地址
3. &a的含义是数组的地址

参考下面的代码:

C/C++ code
#include <iostream>using namespace std;int main(int argc, char** argv){    int a[] = {1, 2, 3};        // 考察数据类型    cout << typeid(a).name() << endl;        // int [3]    cout << typeid(&a[0]).name() << endl;    // int *    cout << typeid(&a).name() << endl;        // int (*)[3]    // 考察指针加1运算    cout << a << endl;    cout << a + 1 << endl;        // = a的第一个元素地址 + 1 * sizeof(int)    cout << &a[0] + 1 << endl;    // = a的第一个元素地址 + 1 * sizeof(int)    cout << &a + 1 << endl;        // = a的第一个元素地址 + 1 * sizeof(int * a中元素的个数)    return 0;} 

热点排行