如何使用的结构? [英] How do I use a structure?

查看:144
本文介绍了如何使用的结构?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧首先我会解释我的任务。对于这个任务,我不得不使用它我有没有问题,动态内存分配。什么我有是搞清楚工作我分配正确的方式有问题。对于我的任务,我需要建立一个程序,提示用户输入他们有多少学生然后问以下信息;学号,生日和电话号码。我需要使用一个循环来提示用户输入所有学生的信息。我需要创建一个循环,就能把所有的学生ID扫描和使用他们的生日发现的最古老的学生(循环必须通过3个以上的学生能够扫描)。

下面是我的code,我已经得到了一些建议,并从你们code连位,但他们实施后,我甚至对我应该做的更多的困惑。请看看它,并批评了我。

编辑:我还添加了在哪里我收到和错误的code

感谢您。

 的#include<&stdio.h中GT;
#包括LT&;&stdlib.h中GT;INT主要(无效)
{
    为int * studentData = NULL;
    为int * studentDataType = NULL;
    INT学生;
    INT studentID;
    INT年;
    INT月;
    日整型;
    长长的电话;    的printf(有多少学生进入你的记录:\\ n);
    scanf函数(%d个,&安培;生);    studentData =(INT *)malloc的((的sizeof(int)的*学生));    结构studentDataType
    {
        INT studentID;
        INT年;
        INT月;
        日整型;
        长长的电话;
    };
    //无效操作数为二进制*(有'廉政'和'为int *)
    studentDataType * studentData =(studentDataType *)malloc的(numberOfStudents *的sizeof(studentData));    的for(int i = 0; I<学生; ++ I)
    {
        的printf(%D,%D,%D,%D,%d个\\ N,studentID,年,月,日,电话);
    }
}


解决方案

第一个问题是,你有变量名相同类型的名称。虽然可以有,在C在一定程度上,如:

 的typedef INT X;
点¯x富(X X)
{
  返回X;
}

这可能是一个好主意,不为便于阅读,做到这一点。所以你的情况,你有:

 为int * studentData = NULL;
为int * studentDataType = NULL;

这是一个变量名,那么你有:

 结构studentDataType ...

这是该类型的名称(如你应该用作结构studentDataType,不无结构);最后

  studentDataType * studentData = ...

是由编译器作为操作上的两个变量处理,而不是一个类型声明如你所愿。所以,你的内存分配必须是:

 结构studentDataType * studentData =的malloc(numberOfStudents *的sizeof(结构studentData));

这让你重新定义studentData,你在程序的开头声明的,而numberOfStudents问题是没有定义,也许你想写生来代替。

至于与scanf函数读取数据,请参阅previous评论。

Ok firstly I'll explain my assignment. For this assignment I have to use dynamic memory allocation which I am having no problems with. What I am having a problem with is figuring out the correct way to work my assignment. For my assignment I need to create a program that prompt the user to enter how many students they have then ask for the following information; Student ID, Birthdate, and Phone number. I need to use a loop to prompt the user to enter all the students information. I need to create a loop that will scan through all the student IDs and find the oldest student using their birthdate (The loop must be able scan through more then 3 students).

Here is my code, I've gotten some suggestions and even bits of code from you guys, but after implementing them I'm even more confused on what I should do. Please take a look at it and critique me.

EDIT: I also added in on the code where I'm receiving and error

Thank you.

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

int main (void)
{
    int * studentData= NULL;
    int * studentDataType=NULL;
    int students;
    int studentID;
    int year;
    int month;
    int day;
    long long phone;

    printf("How many students are you entering records for:\n");
    scanf("%d", &students);

    studentData=(int*)malloc((sizeof(int)*students));

    struct studentDataType
    {
        int studentID; 
        int year;
        int month;
        int day;
        long long phone;
    };
    //invalid operands to binary * (have 'int' and 'int *')
    studentDataType *studentData = (studentDataType*)malloc(numberOfStudents *sizeof(studentData));

    for (int i = 0 ; i < students ; ++i) 
    {
        printf("%d, %d, %d, %d, %d\n", studentID, year, month, day, phone);
    }
}

解决方案

The first problem is that you have variable names the same as the name of the type. Although you can have that in C to a certain extent, like:

typedef int x;
x foo (x x)
{
  return x;
}

It might be a good idea not to do this for the readability purposes. So in your case you have:

int * studentData= NULL;
int * studentDataType= NULL;

which is a variable name, then you have:

struct studentDataType ...

which is a name of the type (should be used as struct studentDataType, not without struct as you do); finally

studentDataType *studentData = ...

is treated by the compiler as an operation on two variables, not a type declaration as you would expect. So your memory allocation needs to be:

struct studentDataType *studentData = malloc(numberOfStudents *sizeof(struct studentData));

Which brings a problem that you redefine studentData, which you declared in the beginning of the program, and "numberOfStudents" is not defined, probably you wanted to write "students" instead.

As for the reading data with scanf, see the previous comment.

这篇关于如何使用的结构?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆