为什么我们需要指向结构指针的指针来改变成员值?

时间:2017-03-01 16:00:58

标签: c pointers structure

对于简单类型(simple types: int, char..),我们直接使用指针(their address)作为函数的参数来永久地更改它们在主程序中的值,有人可以向我解释为什么我们需要指向的结构一个Structure pointer,而不只是一个指针?

struct someStruct
{
  int field1;
  int field2;
}

void initializeFields(struct someStruct** foo)
{
  //changing fields
}

//while 
void initializeFields(struct someStruct* foo) //does not work ?

3 个答案:

答案 0 :(得分:3)

struct someStruct* foo没问题,您不需要struct someStruct** foo。不确定你从哪里得到这个想法,但这不正确。

答案 1 :(得分:2)

要仅更改您不需要的结构成员。但是要将实际指针更改为,需要通过使用指向结构的指针来模拟通过引用传递的结构。

例如,要仅更改成员,这就足够了

void initializeFields(struct someStruct* foo)
{
    foo->field1 = 1;
    foo->field2 = 2;
}

int main(void)
{
    struct someStruct bar;
    initializeFields(&bar);
    printf("field1 = %d\n", bar.field1);
    printf("field2 = %d\n", bar.field2);

    return 0;
}

但是,如果您想要在函数中动态分配结构,则需要模拟通过引用传递:

void initializeFields(struct someStruct** foo)
{
    // The difference is this assignment
    *foo = malloc(sizeof(struct someStruct));

    (*foo)->field1 = 1;
    (*foo)->field2 = 2;
}

int main(void)
{
    struct someStruct* bar;
    initializeFields(&bar);
    printf("field1 = %d\n", bar->field1);
    printf("field2 = %d\n", bar->field2);

    return 0;
}

另外两个示例显示传递值与模拟传递引用之间的区别。

考虑以下计划:

#include <stdio.h>

void foo(int y)
{
    y = 5;
}

int main(void)
{
    int x = 1;
    printf("1: x = %d\n", x);
    foo(x);
    printf("2: x = %d\n", x);

    return 0;
}

上述程序将打印

1: x = 1
2: x = 1

这是因为x中值的副本被传递给函数foo。修改foo中的本地副本不会修改x内的原始变量main

现在模仿传递参考:

#include <stdio.h>

void foo(int *y)
{
    *y = 5;
}

int main(void)
{
    int x = 1;
    printf("1: x = %d\n", x);
    foo(&x);
    printf("2: x = %d\n", x);

    return 0;
}

此程序将打印

1: x = 1
2: x = 5

这是因为我们不会更改函数y中的指针变量foo,但我们会更改它指向的位置的值,这恰好是变量{{1}在x函数中。

答案 2 :(得分:1)

在非常特殊的情况下,您需要指向struct指针的指针(大多数是struct指针数组)。

否则,指向结构的指针就足够了。

03-01 21:53:32.634 17772-17772/? I/art: Late-enabling -Xcheck:jni
03-01 21:53:32.710 17772-17772/bk.acs W/System: ClassLoader referenced unknown path: /data/app/bk.acs-2/lib/arm
03-01 21:53:32.725 17772-17772/bk.acs I/InstantRun: Instant Run Runtime started. Android package is bk.acs, real application class is null.
03-01 21:53:32.809 17772-17772/bk.acs W/System: ClassLoader referenced unknown path: /data/app/bk.acs-2/lib/arm
03-01 21:53:33.024 17772-17772/bk.acs W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
03-01 21:53:33.519 17772-17830/bk.acs I/Adreno-EGL: <qeglDrvAPI_eglInitialize:379>: QUALCOMM Build: 06/15/16, 8520914, Ic0d3aa8c86
03-01 21:53:33.521 17772-17830/bk.acs I/OpenGLRenderer: Initialized EGL, version 1.4
03-01 21:53:33.521 17772-17830/bk.acs D/OpenGLRenderer: Swap behavior 1
03-01 21:53:33.537 17772-17772/bk.acs W/ResourceType: No package identifier when getting value for resource number 0x00000001
03-01 21:53:33.538 17772-17772/bk.acs D/AndroidRuntime: Shutting down VM
03-01 21:53:33.540 17772-17772/bk.acs E/AndroidRuntime: FATAL EXCEPTION: main
                                                        Process: bk.acs, PID: 17772
                                                        android.content.res.Resources$NotFoundException: String resource ID #0x1
                                                            at android.content.res.Resources.getText(Resources.java:335)
                                                            at android.support.v7.widget.ResourcesWrapper.getText(ResourcesWrapper.java:52)
                                                            at android.widget.TextView.setText(TextView.java:4551)
                                                            at bk.acs.RecyclerView.MyAdapter.onBindViewHolder(MyAdapter.java:38)
                                                            at bk.acs.RecyclerView.MyAdapter.onBindViewHolder(MyAdapter.java:18)
                                                            at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:5217)
                                                            at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:5250)
                                                            at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4487)
                                                            at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4363)
                                                            at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:1961)
                                                            at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1370)
                                                            at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1333)
                                                            at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:562)
                                                            at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:2900)
                                                            at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3071)
                                                            at android.view.View.layout(View.java:17643)
                                                            at android.view.ViewGroup.layout(ViewGroup.java:5612)
                                                            at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1741)
                                                            at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1585)
                                                            at android.widget.LinearLayout.onLayout(LinearLayout.java:1494)
                                                            at android.view.View.layout(View.java:17643)
                                                            at android.view.ViewGroup.layout(ViewGroup.java:5612)
                                                            at android.support.design.widget.CoordinatorLayout.layoutChild(CoordinatorLayout.java:1037)
                                                            at android.support.design.widget.CoordinatorLayout.onLayoutChild(CoordinatorLayout.java:747)
                                                            at android.support.design.widget.CoordinatorLayout.onLayout(CoordinatorLayout.java:761)
                                                            at android.view.View.layout(View.java:17643)
                                                            at android.view.ViewGroup.layout(ViewGroup.java:5612)
                                                            at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
                                                            at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
                                                            at android.view.View.layout(View.java:17643)
                                                            at android.view.ViewGroup.layout(ViewGroup.java:5612)
                                                            at android.support.v7.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:433)
                                                            at android.view.View.layout(View.java:17643)
                                                            at android.view.ViewGroup.layout(ViewGroup.java:5612)
                                                            at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
                                                            at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
                                                            at android.view.View.layout(View.java:17643)
                                                            at android.view.ViewGroup.layout(ViewGroup.java:5612)
                                                            at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1741)
                                                            at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1585)
                                                            at android.widget.LinearLayout.onLayout(LinearLayout.java:1494)
                                                            at android.view.View.layout(View.java:17643)
                                                            at android.view.ViewGroup.layout(ViewGroup.java:5612)
                                                            at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
                                                            at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
                                                            at com.android.internal.policy.DecorView.onLayout(DecorView.java:726)
                                                            at android.view.View.layout(View.java:17643)
                                                            at android.view.ViewGroup.layout(ViewGroup.java:5612)
                                                            at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2341)
                                                            at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2068)
                                                            at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1254)
                                                            at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6306)
                                                            at android.view.Choreographer$CallbackRecord.run(Choreographer.java:874)
                                                            at android.view.Choreographer.doCallbacks(Choreographer.java:686)
                                                            at android.view.Choreographer.doFrame(Choreographer.java:621)
                                                            at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:860)
03-01 21:53:33.540 17772-17772/? E/AndroidRuntime:     at android.os.Handler.handleCallback(Handler.java:751)
                                                       at android.os.Handler.dispatchMessage(Handler.java:95)
                                                       at android.os.Looper.loop(Looper.java:154)
                                                       at android.app.ActivityThread.main(ActivityThread.java:6088)
                                                       at java.lang.reflect.Method.invoke(Native Method)
                                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)