Passing a char pointer array to a function

前端 未结 2 1089
野性不改
野性不改 2021-01-21 02:24

I have written following sample code to demonstrate my problem

#include 
#include 

using namespace std;

void f (char*** a)
{
           


        
相关标签:
2条回答
  • 2021-01-21 02:58

    It's because of the operator precedence, where the array-indexing operator [] have higher precedence than the dereference operator *.

    So the expression *a[0] is really, from the compilers point of view, the same as *(a[0]), which is not what you want.

    You have to explicitly add parentheses to change the precedence:

    (*a)[0] = ...
    
    0 讨论(0)
  • 2021-01-21 03:07

    a[0], a[1] are char, and they have a value in them, dereferencing that value will obviously cause a segmentation fault. What you may want to do is:

    (*a)[...]
    

    dereference 'a' which is a pointer, this will give u an array.

    0 讨论(0)
提交回复
热议问题