本文概述
- C
- C++
- C
- C
- C++
- C
- C++
- C
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语言中的指针和数组之间有什么区别()】数组参数始终作为指针传递, 即使我们使用方括号也是如此。
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语言中的指针与数组
如果发现任何不正确的地方, 或者想分享有关上述主题的更多信息, 请发表评论。
推荐阅读
- Java中HashMap和HashTable之间有什么区别()
- 算法题(求两个大数的差)
- 算法题(m个元素的两个子集之间的最大差)
- 反向索引与正向索引之间的差异
- C/C++中的int main()和int main(void)之间的区别()
- Redis Sentinel 实现高可用
- 高可用业务系统你必须知道的点
- 抓包青花瓷使用教程①
- 抓包青花瓷实战教程②