指针和数组
来源:互联网 发布:淘宝中学科学实验室 编辑:程序博客网 时间:2024/06/07 13:18
指针和数组
一、 概述
一种常见的错误观念是数组和指针是完全可以互换的,尽管数组名字有时候可以当做指针来用,但是数组的名字不是指针,数组表示法也可以和指针一起使用,但是连着明显不同,也不一定能互换。
1、一维数组
一维数组是线性结构,用一个索引访问成员,下面的代码声明了一个5个元素的整数数组:
Int vector[5];
数组名字只是引用了一块内存,对数组做sizeof操作会得到为该数组分配的字节数。
2、二维数组
3、多维数组
二、指针表示法和数组
1、指针指向数组
指针在处理数组的时候很有用,我们可以用指针指向已有的数组,也可以从堆上分配内存然后把这块内存当做一个数组使用。数组表示法和指针表示法在某种意义上可以互换。
单独使用数组名字时会返回数组地址,我们可以把地址赋给指针,如下所示:
intvector[5]={1,2,3,4,5};
int *pv=vector;
pv变量是指向数组第一个元素而不是指向数组本身的指针,给pv赋值是把数组的第一个元素的地址赋给pv。
Vector与&vector[0]等价,都是数组的第一个元素的地址;
&vector与vector、&vector[0]的值相同,但是第一个数组的地址,后两个表示数组第一个元素的地址。
2、指针和数组的差别
(1)Vector[i]和*(vector+1)的值是一样的,但是生成的机器码不一样,vector[i]表示从数组vector的0位置开始移动i个位置,去除内容;而*(vector+1)表示从vector的地址开始,地址上加上i,然后取出这个地址的内容。
(2)sizeof操作符对vector返回20;对pv使用返回4;
(3)pv是左值,能够修改;而vector不是左值,不能修改。
三、使用malloc创建一维数组
如果从堆上分配内存并把地址赋给一个指针,就可以对指针使用数组下标并把这块内存当成一个数组,如下代码所示:
int main(intargc, char* argv[])
{
int *pv = (int*)malloc(5*sizeof(int));
for(inti=0;i<5;i++)
{
*(pv+i)=i;//等价于pv[i]=i
}
for(inti=0;i<5;i++)
{
printf("%d\n",pv[i]);
}
return 0;
}
四、使用realloc调整数组长度
用malloc创建的已有数组的长度可以通过realloc函数来调整。
char* getline(void)
{
const intsizeincrement=3;
char*buffer=(char*)malloc(sizeincrement);
char*currentposition=buffer;
intmaximumlength=sizeincrement;
intlength=0;
intcharacter;
if(currentposition==NULL)
return NULL;
while(1)
{
character=fgetc(stdin);
if(character=='\n'){break;}
if(++length>maximumlength)
{
char*newBuffer = (char*)realloc(buffer,maximumlength+=sizeincrement);//重新分配内存
if(newBuffer==NULL)
{
free(buffer);
return NULL;
}
currentposition=newBuffer+(currentposition-buffer);//newBuffer是新分配内存的首地址,currentposition-buffer是原来内存中数据个数
buffer=newBuffer;
}
*currentposition++=character;
}
*currentposition='\n';
return buffer;
}
五、二维数组与数组指针、指针的关系(见下面代码)
#include"stdafx.h"
#include<iostream>
usingnamespace std;
intarray[4][4] = {{1,2,8,9},{2,4,9,12},{4,7,10,13},{6,8,11,15}};
//将数组作为形参,遍历数组
voidprint1(int arr[],int size)
{
for(int i = 0;i<size;i++)
cout<<arr[i]<<" ";
cout<<endl;
}
//将一维数组的指针作为形参,指针遍历数组
voidprint2(int * arr,int size)
{
for(int i = 0;i<size;i++)
cout<<*(arr+i)<<"";
cout<<endl;
}
int_tmain(int argc, _TCHAR* argv[])
{
printf("hello world!\n");
/******************一维数组和指针**************************/
cout<<"***********一维数组和指针**************"<<endl;
int arr[] ={1,2, 3, 4 ,5 ,6 ,7, 8,9};
//1.1、一维数组:采用数组下标的方式遍历
cout<<"采用数组下标的方式遍历数组!"<<endl;
for(int i = 0;i<9;i++)
cout<<arr[i]<<" ";
cout<<endl;
//1.2、指针方式遍历
cout<<"采用指针方式遍历数组!"<<endl;
int * it =arr;//将指针的值为arr的首地址
for(int i = 0;i<9;i++)
cout<<*(it+i)<<" ";
cout<<endl;
//1.3、将数组作为形参,传递给函数
cout<<"将数组作为形参,数组下标方式遍历数组!"<<endl;
print1(arr,9);
//1.4、将数组作为形参,传递给函数
cout<<"将一维数组的指针作为形参,指针遍历数组"<<endl;
print2(it,9);
/******************二维数组和指针**************************/
cout<<endl<<endl;
cout<<"***********二维数组和指针**************"<<endl;
//二维数组:采用数组下标的方式遍历
cout<<"采用数组下标的方式遍历数组!"<<endl;
for(int i=0;i<4;i++)
{
for(intj=0;j<4;j++)
cout<<array[i][j]<<"";
cout <<endl;
}
//二维数组:采用指针的方式遍历
cout<<"采用指针的方式1遍历数组!"<<endl;
int* iter1 =(int*)(array);
for(int i=0;i<4;i++)
{
for(int j=0;j<4;j++)
cout<<*(iter1+4*i+j)<<"";
cout <<endl;
}
//二维数组:采用指针的方式遍历
cout<<"采用指针的方式2遍历数组!"<<endl;
int** iter2 =(int**)(array);
for(int i=0;i<4;i++)
{
for(int j=0;j<4;j++)
cout<<int(*(iter1+4*i+j))<<"";
cout <<endl;
}
//二维数组:采用指针的方式遍历
cout<<"采用指针数组的方式2遍历数组!"<<endl;
int(*iter3)[4] =array;
for(int i=0;i<4;i++)
{
for(int j=0;j<4;j++)
cout<<*(*(iter3+i)+j)<<"";
cout <<endl;
}
return 0;
}
- 指针和指针数组
- 指针数组和数组指针
- 指针数组和数组指针
- 指针数组和数组指针
- 指针数组和数组指针
- 数组指针和指针数组
- 指针数组和数组指针
- 数组指针和指针数组
- 指针数组和数组指针
- 指针数组和数组指针
- 数组指针和指针数组
- 指针数组和数组指针
- 指针数组和数组指针
- 数组指针和指针数组
- 数组指针和指针数组
- 指针数组和数组指针
- 指针数组和数组指针
- 数组指针和指针数组
- cocos code IDE 没有调试信息解决办法
- Hadoop集群服务器的时间同步
- codeforces 670A 水(最多最少周末)
- Java中方法参数传值问题
- 堆排序
- 指针和数组
- poj 1080 Human Gene Functions
- 二级指针的三种内存模型的整理与分析
- notepad++使用
- UVA 11762 Race to 1
- ios 拨打电话
- OVMF基础
- 数据结构实验之二叉树一:树的同构
- not acceptable according to the request "accept" headers 问题解决