const导致不兼容的指针类型.为什么只有双指针?

前端之家收集整理的这篇文章主要介绍了const导致不兼容的指针类型.为什么只有双指针?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
这个问题已经解决here.

建议的duplicate和当前给出的答案没有解决为什么首先给出的例子没有问题.主要是为什么没有推理:

“const int **是指向const int *的指针,它与int *不同”

也适用于:

“const int *是一个指向const int的指针,它与int只是一个不同的东西”

我从不同的角度接近它,希望得到另一种解释.

带有示例的代码.

  1. #include <stdio.h>
  2.  
  3. void f_a (int const a){
  4.  
  5. /*
  6. * Can't do:
  7. * a = 3; //error: assignment of read-only parameter ‘a’
  8. *
  9. * Explanation: I can't change the value of a in the scope of the function due to the const
  10. */
  11. printf("%d\n",a);
  12. }
  13.  
  14. void f_ptr_a_type1 (int const * ptr_a){
  15. /*
  16. * Can do this:
  17. * ptr_a’ = 0x3;
  18. * which make dereferencig to a impossible.
  19. * printf("%d\n",* ptr_a’); -> segfault
  20. * But const won't forbid it.
  21. *
  22. * Can't do:
  23. * *ptr_a’ = 3; //error: assignment of read-only parameter ‘* ptr_a’
  24. *
  25. * Explanation: I can't change the value of a by pointer dereferencing and addignment due to the int const
  26. */
  27. }
  28.  
  29. void f_ptr_a_type2 (int * const ptr_a){
  30. /*
  31. * Can do this:
  32. * *a = 3;
  33. *
  34. * Can't do:
  35. * ptr_a = 3; //error: assignment of read-only parameter ‘ptr_a’
  36. *
  37. * Explanation: I can't change the value because the const is protecting the value of the pointer in the funcion scope
  38. */
  39. }
  40.  
  41. void f_ptr_ptr_a (int const ** ptr_ptr_a){
  42. /*
  43. * Can do this:
  44. * ptr_ptr_a = 3;
  45. * * ptr_ptr_a = 0x3;
  46. *
  47. * Can't do:
  48. * ** ptr_ptr_a = 0x3; //error: assignment of read-only parameter ‘**ptr_a’
  49. *
  50. * Explanation: Makes sense. Just follows the pattern from prevIoUs functions.
  51. */
  52. }
  53.  
  54. int main()
  55. {
  56. int a = 7;
  57. f_a(a);
  58.  
  59. int * ptr_a = &a;
  60. f_ptr_a_type1(&a);
  61. f_ptr_a_type2(&a);
  62.  
  63. int ** ptr_ptr_a = &ptr_a;
  64. f_ptr_ptr_a(ptr_ptr_a); //warning: passing argument 1 of ‘f_ptr_ptr_a’ from incompatible pointer type [-Wincompatible-pointer-types]
  65. }

接受的广泛接受的答案是这样的:

int ** isn’t the same as const int** and you can’t safely cast it

我的问题是为什么功能突然关注?

这里没有抱怨int不是int const:

  1. int a = 7;
  2. f_a(a);

它没有在这里抱怨因为int *既不是int const *也不是int * const:

  1. int * ptr_a = &a;
  2. f_ptr_a_type1(&a);
  3. f_ptr_a_type2(&a);

但突然间,它开始在双指针案件中抱怨.

>使用这个术语和示例寻找解释?
>为什么函数突然开始担心写入
超出范围的东西的权限?

解决方法

从例如转换char *到const char *总是安全的.通过const char *,指向的数据无法修改,就是这样.

另一方面,从char **到const char **的转换可能是不安全的,因此不允许隐式.请考虑以下代码,而不是解释它:

  1. void foo(const char **bar)
  2. {
  3. const char *str = "test string";
  4. *bar = str; // perfectly legal
  5. }
  6.  
  7. int main(void)
  8. {
  9. char *teststr[] = {0};
  10. foo((const char **)teststr);
  11. // now teststr points to a `const char *`!
  12.  
  13. *teststr[0] = 'x'; // <- attempt to modify read-only memory
  14. // ok in this line,there's no const qualifier on teststr!
  15. }

如果在调用foo()时从char **到const char **的转换是隐式的,那么你将有一种隐式转换const的方法.

猜你在找的C&C++相关文章