本文概述
指针用于存储动态分配的数组的地址, 以及用于作为参数传递给函数的数组。在其他情况下, 数组和指针是两个不同的东西, 请参见以下程序以证明此语句的合理性。
sizeof运算符的行为
C
//1st program to show that array and pointers are different
#include <stdio.h>
int main()
{
int arr[] = { 10, 20, 30, 40, 50, 60 };
int * ptr = arr;
//sizof(int) * (number of element in arr[]) is printed
printf ( "Size of arr[] %ld\n" , sizeof (arr));
//sizeof a pointer is printed which is same for all
//type of pointers (char *, void *, etc)
printf ( "Size of ptr %ld" , sizeof (ptr));
return 0;
}
C++
//1st program to show that array and pointers are different
#include <iostream>
using namespace std;
int main()
{
int arr[] = { 10, 20, 30, 40, 50, 60 };
int * ptr = arr;
//sizof(int) * (number of element in arr[]) is printed
cout <<"Size of arr[] " <<sizeof (arr) <<"\n" ;
//sizeof a pointer is printed which is same for all
//type of pointers (char *, void *, etc)
cout <<"Size of ptr " <<sizeof (ptr);
return 0;
}
输出如下
Size of arr[] 24
Size of ptr 8
不允许将任何地址分配给数组变量。
C
//IInd program to show that array and pointers are different
#include <stdio.h>
int main()
{
int arr[] = {10, 20}, x = 10;
int *ptr = &x; //This is fine
arr = &x; //Compiler Error
return 0;
}
输出如下:
Compiler Error: incompatible types when assigning to
type 'int[2]' from type 'int *'
尽管数组和指针是不同的东西, 但是数组的以下属性使它们看起来相似。
- 数组名称给出数组第一个元素的地址。
例如, 考虑以下程序。
C
#include <stdio.h>
int main()
{
int arr[] = { 10, 20, 30, 40, 50, 60 };
//Assigns address of array to ptr
int * ptr = arr;
printf ( "Value of first element is %d" , *ptr);
return 0;
}
C++
//1st program to show that array and pointers are different
#include <iostream>
using namespace std;
int main()
{
int arr[] = { 10, 20, 30, 40, 50, 60 };
//Assigns address of array to ptr
int * ptr = arr;
cout <<"Value of first element is " <<*ptr;
return 0;
}
输出如下
Value of first element is 10
使用指针算法访问数组成员。
编译器使用指针算法访问数组元素。例如, 像” arr [i]”之类的表达式被编译器视为*(arr + i)。这就是为什么像*(arr + i)这样的表达式适用于数组arr, 而像ptr [i]这样的表达式也适用于指针ptr的原因。
C
#include <stdio.h>
int main()
{
int arr[] = {10, 20, 30, 40, 50, 60};
int *ptr = arr;
printf ( "arr[2] = %d\n" , arr[2]);
printf ( "*(arr + 2) = %d\n" , *(arr + 2));
printf ( "ptr[2] = %d\n" , ptr[2]);
printf ( "*(ptr + 2) = %d\n" , *(ptr + 2));
return 0;
}
C++
#include <iostream>
using namespace std;
int main()
{
int arr[] = { 10, 20, 30, 40, 50, 60 };
int * ptr = arr;
cout <<"arr[2] = " <<arr[2] <<"\n" ;
cout <<"*(arr + 2) = " <<*(arr + 2) <<"\n" ;
cout <<"ptr[2] = " <<ptr[2] <<"\n" ;
cout <<"*(ptr + 2) = " <<*(ptr + 2) <<"\n" ;
return 0;
}
输出如下
arr[2] = 30
*(arr + 2) = 30
ptr[2] = 30
*(ptr + 2) = 30
数组参数始终作为指针传递, 即使我们使用方括号也是如此。
C
#include <stdio.h>
int fun( int ptr[])
{
int x = 10;
//size of a pointer is printed
printf ( "sizeof(ptr) = %d\n" , ( int ) sizeof (*ptr));
//This allowed because ptr is a pointer, not array
ptr = &x;
printf ( "*ptr = %d " , *ptr);
return 0;
}
//Driver code
int main()
{
int arr[] = { 10, 20, 30, 40, 50, 60 };
//size of a array is printed
printf ( "sizeof(arr) = %d\n" , ( int ) sizeof (arr));
fun(arr);
return 0;
}
输出如下
sizeof(arr) = 24
sizeof(ptr) = 4
*ptr = 10
请参考C语言中的指针与数组
如果发现任何不正确的地方, 或者想分享有关上述主题的更多信息, 请发表评论。
来源:
https://www.srcmini02.com/69411.html