C中指针和数组的区别?

指针用于存储动态分配数组的地址,以及作为参数传递给函数的数组。在其他上下文中,数组和指针是两个不同的东西,请参阅以下程序来证明此语句的正确性。 sizeof算子的行为

null

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) << "" ;
// sizeof a pointer is printed which is same for all
// type of pointers (char *, void *, etc)
cout << "Size of ptr " << sizeof (ptr);
return 0;
}


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" , 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;
}


输出

Size of arr[] 24Size of ptr 8

不允许将任何地址分配给数组变量。

C++

// IInd program to show that array
// and pointers are different
#include <iostream>
using namespace std;
int main()
{
int arr[] = {10, 20}, x = 10;
int *ptr = &x; // This is fine
arr = &x; // Compiler Error
return 0;
}
// This code is contributed by Shubhamsingh10


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 *' 

看到了吗 以前的职位 在这个话题上有更多的分歧。 虽然数组和指针是不同的东西,但数组的以下属性使它们看起来很相似。

  1. 数组名给出数组第一个元素的地址。

例如,考虑下面的程序。

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;
}


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;
}


输出

Value of first element is 10

使用指针算法访问数组成员。 编译器使用指针算法来访问数组元素。例如,像“arr[i]”这样的表达式被编译器视为*(arr+i)。这就是为什么像*(arr+i)这样的表达式适用于数组arr,像ptr[i]这样的表达式也适用于指针ptr。

C++

#include <iostream>
using namespace std;
int main()
{
int arr[] = { 10, 20, 30, 40, 50, 60 };
int * ptr = arr;
cout << "arr[2] = " << arr[2] << "" ;
cout << "*(arr + 2) = " << *(arr + 2) << "" ;
cout << "ptr[2] = " << ptr[2] << "" ;
cout << "*(ptr + 2) = " << *(ptr + 2) << "" ;
return 0;
}


C

#include <stdio.h>
int main()
{
int arr[] = {10, 20, 30, 40, 50, 60};
int *ptr = arr;
printf ( "arr[2] = %d" , arr[2]);
printf ( "*(arr + 2) = %d" , *(arr + 2));
printf ( "ptr[2] = %d" , ptr[2]);
printf ( "*(ptr + 2) = %d" , *(ptr + 2));
return 0;
}


输出

arr[2] = 30*(arr + 2) = 30ptr[2] = 30*(ptr + 2) = 30

数组参数始终作为指针传递,即使使用方括号也是如此。

C++

#include <bits/stdc++.h>
using namespace std;
int fun( int ptr[])
{
int x = 10;
// Size of a pointer is printed
cout << "sizeof(ptr) = "
<< ( int ) sizeof (*ptr)
<< endl;
// This allowed because ptr is a
// pointer, not array
ptr = &x;
cout << "*ptr =  " << *ptr;
return 0;
}
// Driver code
int main()
{
int arr[] = { 10, 20, 30, 40, 50, 60 };
// Size of a array is printed
cout << "sizeof(arr) = "
<< ( int ) sizeof (arr)
<< endl;
fun(arr);
return 0;
}
// This code is contributed by shivanisinghss2110


C

#include <stdio.h>
int fun( int ptr[])
{
int x = 10;
// size of a pointer is printed
printf ( "sizeof(ptr) = %d" , ( 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" , ( int ) sizeof (arr));
fun(arr);
return 0;
}


输出

sizeof(arr) = 24sizeof(ptr) = 4*ptr = 10 

请参考 C语言中的指针与数组 更多细节。

本文由 阿比拉蒂 。如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请发表评论

© 版权声明
THE END
喜欢就支持一下吧
点赞7 分享