【问题标题】:C Using pointers with a 2D array of a struct and how to access them using pointersC 使用带有二维结构数组的指针以及如何使用指针访问它们
【发布时间】:2018-08-04 21:14:48
【问题描述】:
#include <string.h>
#include <stdio.h>

struct student{
    char last[25] ;
    char first[25];
};

struct seating {
    struct student **seat;
};

//Set the first and last name to default values
void student_init_default(struct student *s ) {
    strcpy(s->last_name,"***");
    strcpy(s->first_name,"***");
}

void seating(int rowNum, int columnNum, struct seating *a ){
    //Instantiate a 2D array specfied by the parameters
    struct student students[rowNum][columnNum];

    a->seat = students;

    //Initialize each element to the default
    for(int rows = 0; rows < rowNum; rows++){
        for(int columns = 0; columns < columnNum; columns++){
            student_init_default(&students[rows][columns]);
        }
    }

}

void main() { 
    struct seating room; 
    struct student student;

    int row, col, rowNum, columnNum; 

    char student_info[30]; 

    // Ask a user to enter a number of rows for an classroom seating    
    printf ("Please enter a number of rows for an classroom seating."); 
    scanf ("%d", &rowNum); 

    // Ask a user to enter a number of columns for an classroom seating   
    printf ("Please enter a number of columns for an classroom seating.");   
    scanf ("%d", &columnNum); 

    // seating
    seating(rowNum, columnNum, &room); 

}

首先,我是 C 的初学者。我的问题是使用指针。这段代码的重点是制作一个 struct student 的二维数组,然后通过将默认值设置为名字和姓氏来填充数组。我要问的是,是否有人可以简单地解释或提示如何将参数 struct chinese_seating *astruct student **seating 以及二维数组。除此之外,我将如何使用 struct chinese_seating *a 来访问 2D 数组?我确实知道指针的基础知识,但我已经研究了几个小时并没有找到与工作的联系。

我知道这一行 printf("%s", listOfStudents[0][2].firstName); 打印 ***(假设用户输入 3,3 作为行/列)。只是我不知道如何在以后的方法中访问它。

【问题讨论】:

  • 我猜你从提供的答案中解决了问题。
  • 正如 coderredoc 在下面的回答中指出的那样,您已经尝试将不适合的内容分配给 a-&gt;seat。但即使它适合代码也不会让你高兴,因为struct student students[rowNum][columnNum];本地 定义到函数seating() 的,所以从这个函数返回的代码的那一刻所有内存a-&gt;seat 都指向会变得无效。

标签: arrays c pointers 2d


【解决方案1】:
a->seat = students;

编译器明确指出错误

error: cannot convert 'student [rowNum][columnNum]' to 'student**' in assignment
     a->seat = students;
             ^

是错误的,因为在这里您将二维数组分配给双指针。二维数组衰减为struct student (*)[],然后您尝试将其分配给struct student **

相反,您应该自己创建锯齿状数组并使用它。

a->seat = malloc(sizeof(struct student*)* rowNum);
if(!a->seat){
   perror("malloc");
   exit(EXIT_FAILURE);
}
for(int i = 0; i< rowNum; i++){
   a->seat[i] = malloc(sizeof(struct student)* columnNum);
   if(!a->seat[i]){
      perror("malloc");
      exit(EXIT_FAILURE);
   }
}

之后,您可以像以前一样传递每个struct student 实例的地址并对其进行更改。

进一步说明您可能关注的组织

  • struct seating room; 这里room 是一个包含双指针的结构体实例。而已。该指针指向不重要的地方。它包含不确定的值(垃圾值)。

  • 现在您要分配一些内存并使用它。这是在函数seating 中完成的。因为它只是一个指针,你分配了rowNumstruct student *的内存,为什么struct student*?因为那又会指向一系列struct student变量实例。

  • 分配成功后,现在你有了内存,它不是自动存储持续时间 - 当函数seating结束时它也会保留。它的存储时间超出了函数的范围。

  • 注意一件事,strcpy(s-&gt;last_name,"***");,但结构定义中没有 last_name 变量。应该是last。其他成员变量也是如此。

  • 进行更改后,您将传递struct student 的实例,如下所示:-

    for(int rows = 0; rows < rowNum; rows++){
        for(int columns = 0; columns < columnNum; columns++){
            student_init_default(&a->seat[rows][columns]);
        }
    }
    
  • 您正在尝试分配给a-&gt;seat 一些不适合(不可能)的东西。但即使它适合它也是错误的,因为struct student students[rowNum][columnNum]; 是在函数seating() 本地定义的,所以从这个函数返回的代码的那一刻,所有内存a-&gt;seat 指向将变得无效。访问它将是未定义的行为。 (alk 指出了这一点)。

【讨论】:

  • @alk.:已编辑..谢谢。
猜你喜欢
  • 1970-01-01
  • 2013-06-29
  • 2016-06-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多