C使用带有结构的2D数组的指针以及如何使用指针访问它们

时间:2018-02-25 07:12:11

标签: c arrays pointers 2d access

#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 的2D数组,然后通过将默认值设置为名字和姓氏来填充数组。我要求的是,如果有人可以提供一个简单的解释或提示如何连接参数, struct classroom_seating * a struct student ** seat ,以及二维数组。除此之外,我如何使用 struct classroom_seating * a 来访问2D数组?我知道指针的基础知识,但我已经研究了几个小时,并且没有找到工作的联系。

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

0 个答案:

没有答案