繁体   English   中英

指向C中结构的指针

[英]Pointer to Pointer to a structure in c

我有一个简单的结构。

struct grades
{
 int lowest;
 int highest;
};

然后,我要创建一个函数,该函数返回一个初始化结构的指针。

struct *grades setGrades(int low, int high)
{
  struct *ptr = malloc(sizeof(struct grades));
  ptr->lowest = low;
  ptr->highest = high;
  return ptr;
}

现在我应该用定义struct **grades random(int size);创建一个函数struct **grades random(int size); 我应该为元素的数量等于size grade结构的指针数组分配空间。 创建指针数组时,我想将数组中的每个指针设置为一个新创建的数据结构,该结构的低变量等于10,高变量等于100,然后返回指针。

在这一点上,我真的迷失了,因为我在网上查找了指向结构的双重指针,但是没有找到任何可以帮助我理解的示例。 我想知道是否有人可以向我解释结构的双重指针如何使我在正确的方向上取得一个好的开端。

struct grades { int lowest; int highest; };

struct grades * createGradeSet(int low, int high) //careful: return type is struct grades *
{
  // variable name: ptr, type: struct grades *
  struct grades * ptr = malloc(sizeof(struct grades)); 
  ptr->lowest = low;
  ptr->highest = high;
  return ptr;
}

struct grades ** random(int size)
{
  // Create a pointer to an array of struct grades pointers
  // the size of the array is `size` x the size of a struct grades pointer
  struct grades ** ptr_arr = malloc(sizeof(struct grades *) * size); 
  for (unsigned int i = 0; i < size; i++)
     ptr_arr[i] = createGradeSet(10, 100);  // assign a newly created Gradeset to every cell
  return ptr_arr;
}

所谓的“双指针”只是指向指针的指针。 也就是说, struct grade example是一个变量,其中包含您所定义的lowesthighest 指向struct grade *example指针是一个变量,用于存储相同结构的内存地址。 指向struct grade **example的指针的指针是一个变量,用于存储变量的内存地址,该变量用于存储结构的内存地址。 这里可以找到更详细的解释。 无论如何,要回答您的特定问题,函数将是:

struct grades** random(int size) {
    struct grades** result = malloc(sizeof(struct grades*) * size); //here you are
                                                    //allocating enough space for an
                                                    //array of pointers
    int i;
    for(i = 0; i < size; i++) {
        result[i] = setGrades(10, 100); //here you are setting each pointer to one
                                        //grade through the function you've already
                                        //defined
    }

    return result;
}

尝试以下

struct grades ** random( size_t size )
{
    if ( size == 0 ) return NULL:

    struct grades **p = malloc( size * sizeof( struct grades * ) );

    if ( p != NULL )
    {
        size_t i = 0;
        do
        {
            p[i] = malloc( sizeof( struct grades ) );
            if ( p[i] != NULL )
            {
                p[i]->lowest = 10;
                p[i]->highest = 100;
            }
        } while ( p[i] != NULL && ++i < size );

        if ( i != size )
        {
            for ( size_t j = 0; j < i; j++ ) free( p[i] );
        }
        free( p );
        p = NULL;    
    }

    return p;
}      

函数setGrades应该写为

struct *grades setGrades( int low, int high )
{
    struct *p = malloc( sizeof( struct grades ) );

    if ( p != NULL )
    {
        p->lowest = low;
        p->highest = high;
    }

    return p;
}

在这种情况下,上述函数中的do while循环可以写成

        do
        {
            p[i] = setGrades( 10, 100 );
        } while ( p[i] != NULL && ++i < size );

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM