指向c中结构的指针

时间:2014-10-02 14:41:34

标签: c arrays pointers struct

我的结构很简单。

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);的函数 我应该为grade结构的指针数组分配空间,其元素数等于size。当创建指针数组时,我想将数组中的每个指针设置为新创建的数据结构,该数据结构的低变量等于10,高变量等于100,然后返回指针。

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

3 个答案:

答案 0 :(得分:2)

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;
}

答案 1 :(得分:2)

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

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;
}

答案 2 :(得分:1)

尝试以下

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 );