通过引用传递空数组并在函数

时间:2017-09-21 10:09:52

标签: c pointers initialization parameter-passing pass-by-reference

运行此时出现分段错误,但没有错误。 我试图将指针传递给一个指针数组,将其初始化为一个指针数组,填充数组和调用函数可用的数据。

static void test4(int ***arr)
{
    int *values = (int *) MEM_mallocN(sizeof(int) * 3, "*values");
    values[0] = 0;
    values[1] = 1;
    values[2] = 2;
    *arr = (int **) MEM_mallocN(sizeof(int *) * 3, "*arr");
    *arr[0] = &values[0];
    *arr[1] = &values[1];
    *arr[2] = &values[2];   /* this line causes crash */
}

static void test3(void)
{
    int **arr = NULL;
    test4(&arr);
    for (int i = 0; i < 3; i++) {
        printf("arr[%d] = %p \n", i, arr[i]);
    }
    for (int i = 0; i < 3; i++) {
        printf("arr[%d] = %d \n", i, *arr[i]);
    }
}

1 个答案:

答案 0 :(得分:2)

看来你的意思是

public class SplashScreen extends AppCompatActivity {

// Splash screen timer
private static int SPLASH_TIME_OUT = 2000;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_splash);

    //showing splash screen for desired time
    new Handler().postDelayed(new Runnable() {

        /*
         * Showing splash screen with a timer. This will be useful when you
         * want to show case your app logo / company
         */

        @Override
        public void run() {
            // This method will be executed once the timer is over
            // Start your app main activity
            Intent i = new Intent(SplashScreen.this, Login.class);
            startActivity(i);

            // close this activity
            finish();
        }
    }, SPLASH_TIME_OUT);

}}

后缀下标operator []的优先级高于一元运算符*。

相关问题