将元素作为函数C编程中的参数

问题描述 投票:0回答:2

我是编程新手,在学习关于在堆上分配内存并尝试同时使用结构和指针时很难。这就是我要实现的目标:

  • 创建一个名为student的结构,它带有2个指向要在堆上分配的字符串的指针元素
  • 创建一个从用户那里获取输入并将该输入保存在堆上的结构元素中的函数
  • 创建一个在堆上打印结构中所有值的函数
#include <stdio.h>

   struct student
 { 
    char * name;
    char * course;
 };

//function prototypes
void getStudentInfo(struct student *pStu, pStu->name, pStu->course);
void printInfo(struct student springClass[]);


int main(){
//create an array of 5 students
struct student springClass[5];

for(int i=0; i < 5; i++)
{
    getStudentInfo(struct student springClass[i], springClass[i].name, springClass[i].course);
}

printInfo(struct student springClass)

//free memory
free(p1);

return 0;
}


// function that gets input from the user and saves the input in the elements of the struct on the heap

void getStudentInfo(struct student *pStu, pStu->name, pStu->course)
{
    struct student *p1 {NULL}; 

       //create memory on the heap
     p1 = (struct student *) malloc( 2 * (31 * sizeof(char)));  // for 1 names strings and 1 course string

     //get input from user

         printf("Enter name: ");
         fgets(p1->name, 31, stdin);
         printf("Enter course: ");
         fgets(p1->course, 31, stdin);

}

void printInfo(struct student springClass[])
{
     for(int i=; i < 5; i++)
     {
        printf("Student #%d Name: %s\n", i+1, springClass[i].name);
        printf("Student #%d course: %s\n", i+1, springClass[i].course);
        printf("==========================================\n");
     }
}


无论如何,我都会非常感谢您的帮助,谢谢

c pointers struct structure dma
2个回答
1
投票

我修复了您代码中的一些主要错误。首先,您可以使用'typedef'声明具有指定名称的特殊对象,它使您可以一遍又一遍地记下重复的结构名。其次,正确组织代码。

在我的代码中,您可以看到我使用stdlib库将函数初始化为malloc的calloc,但它将所有分配的字节初始化为0(特别是对于字符串很有用,因为您应该在那里始终将所有内容初始化为0!)。 >

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

typedef struct student
{
    char* name;
    char* course;
} student;

//function prototypes
void getStudentInfo(student* pStu);
void printInfo(student* springClass);


int main() {
    //create an array of 5 students
    student* springClass = (student *)calloc(5, sizeof(student));

    for (int i = 0; i < 5; i++)
    {
        getStudentInfo(springClass + i);
    }

    printInfo(springClass);

    for (int i = 0; i < 5; i++)
    {
        free(springClass[i].name);
        free(springClass[i].course);
    }

    free(springClass);

    return 0;
}


// function that gets input from the user and saves the input in the elements of the struct on the heap

void getStudentInfo(student* pStu)
{
    //create memory on the heap
    //get input from user

    printf("Enter name: ");
    pStu->name = (char*)calloc(31, sizeof(char));
    fgets(pStu->name, 31, stdin);
    printf("Enter course: ");
    pStu->course = (char*)calloc(31, sizeof(char));
    fgets(pStu->course, 31, stdin);
    printf("\n");
}

void printInfo(student* springClass)
{
    for (int i = 0; i < 5; i++)
    {
        printf("Student #%d Name: %s\n", i + 1, springClass[i].name);
        printf("Student #%d course: %s\n", i + 1, springClass[i].course);
        printf("==========================================\n");
    }
}

0
投票

首先,为了使用“免费”功能,我们还需要包括stdlib.h:

© www.soinside.com 2019 - 2024. All rights reserved.