在另一个指向结构的指针内访问结构的指针内的元素 [英] Accessing elements within a pointer of a struct inside another pointer to a struct

查看:62
本文介绍了在另一个指向结构的指针内访问结构的指针内的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只需尝试了解结构的嵌套指针是如何工作的.如何在另一个指向结构的指针中访问一个结构的指针中的元素?谢谢.

Just trying to learn how nested pointers to structs work. How would I access an element within a pointer of a struct within another pointer to a struct? Thanks.

#include <iostream>

typedef struct
{
  unsigned int            a;
  unsigned int            b;
  unsigned int            c;
} inner;

typedef struct
{
  unsigned int            d;
  unsigned int            e;
  inner * in;
} outer;

/* Function to use struct */
void test(outer * out)
{
  //Update the value of out->in->a.
  out->in->a = 5;
}

int main() {
  outer * out;
  test(out);

  std::cout << "My updated value: " << out->in->a << std::endl;
}

我收到分段错误(核心已转储)".

I get a "Segmentation fault (core dumped)".

根据建议进行更改.

#include <iostream>

typedef struct
{
    unsigned int            a;
    unsigned int            b;
    unsigned int            c;
} inner;

typedef struct
{
    unsigned int            d;
    unsigned int            e;
    inner * in = new inner();
} outer;

/* Function to use struct */
void test(outer * out)
{
    //Update the value of out->d.
    out->d = 3; 
    //Update the value of out->in->a.
    out->in->a = 5;
}

int main() {

    outer out; 
    outer* p = &out; 

    test(p);

    std::cout << "My updated value for d: " << p->d << std::endl;
    std::cout << "My updated value for a: " << p->in->a << std::endl;
}

我现在获得了正确的值更新.但是我现在有警告

I now get the correct values updated. However I have a warning now

警告:非静态数据成员初始化器仅与-std = c ++ 11或-std = gnu ++ 11一起提供"

"warning: non-static data member initializers only available with -std=c++11 or -std=gnu++11"

inner * in = new inner();

如何解决此警告?

推荐答案

对于初学者来说,C和C ++是不同的语言.

For starters C and C++ are different languages.

无论如何,您必须在访问结构的元素之前将其分配抛出指针.否则,使用未初始化的指针或未指向有效结构对象的指针会导致程序的行为未定义.

In any case you have to allocate the structures before accessing their elements throw pointers. Otherwise using uninitialized pointers or pointers that do not point to valid structures' objects results in undefined behavior of the program.

这是一个演示性C程序,它显示了如何使用指针访问结构的元素

Here is a demonstrative C program that shows how to access elements of a structure using pointers

#include <stdlib.h>
#include <stdio.h>

typedef struct
{
  unsigned int            a;
  unsigned int            b;
  unsigned int            c;
} inner;

typedef struct
{
  unsigned int            d;
  unsigned int            e;
  inner * in;
} outer;


int main( void ) 
{
    outer *out = malloc( sizeof( outer ) );

    if ( ( out != NULL ) && ( out->in = malloc( sizeof( inner ) ) ) )
    {
        out->in->a = 1;
        out->in->b = 2;
        out->in->c = 3;

        printf( "out->in = { a = %d, b = %d, c = %d }\n", out->in->a, out->in->b, out->in->c );

        free( out->in );
        free( out );
    }

    return 0;
}

其输出为

out->in = { a = 1, b = 2, c = 3 }

在C ++中,程序看起来像

In C++ the program can look like

#include <iostream>

typedef struct
{
  unsigned int            a;
  unsigned int            b;
  unsigned int            c;
} inner;

typedef struct
{
  unsigned int            d;
  unsigned int            e;
  inner * in;
} outer;

int main() 
{
    outer *out = new outer;

    out->in = new inner;

    out->in->a = 1;
    out->in->b = 2;
    out->in->c = 3;

    std::cout << "out->in = { a = " << out->in->a
              << ", b = " << out->in->b
              << ", c = " << out->in->c
              << " }" << std::endl;

    delete out->in;
    delete out;

    return 0;
}

其输出将与上面显示的相同.

Its output will be the same as shown above.

关于此功能

void test(outer *out)
{
  //Update the value of out->in->a.
  out->in->a = 5;
}

然后两个指针outin必须指向类型为outerinner的有效对象.

then the both pointers out and in must point to valid objects of types outer and inner.

这篇关于在另一个指向结构的指针内访问结构的指针内的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆