为什么我的数组不能正确存储结构?

发布于 2024-12-18 23:46:05 字数 1537 浏览 2 评论 0原文

我在一些本来应该很简单的事情上遇到了很大的麻烦。在我的程序中,我有一个名为“Run”的结构:

typedef struct{
    char name[MAXNAMELENGTH], day[MAXDAYLENGTH];
    int distance, intDay;
    Date startDate;
    Time startTime;
    Time runTime;
} Run;

我通过使用 fgets() 将文本文件中的数据解析到该结构,将单行解析为名为 line[] 的数组,然后调用此函数:

void parseTable(char line[NUMBEROFLINES], Run run, Run runs[NUMBEROFLINES], int *j){
    sscanf(line,"%s %s %s %d, %s %d:%d %d %d:%d:%d",run.name, run.day, run.startDate.month, &run.startDate.date, run.startDate.year,&run.startTime.hours, &run.startTime.minutes, &run.distance, &run.runTime.hours, &run.runTime.minutes, &run.runTime.seconds);
    runs[*j] = run;
    *j+=1;    
}

现在此函数正确分配结构体 run 的所有数据并将该结构体存储在数组 running[] 中,但在此之后我希望为该结构体分配一个新值:intDay。 为此,我调用了以下函数:

void dayToInt(Run run, Run runs[NUMBEROFLINES], int *i, int *a, int *b){
    if (strcmp(run.day,"Mon") == 0)
        run.intDay = 1;
    else if (strcmp(run.day,"Tue") == 0)
        run.intDay = 2;
    else if (strcmp(run.day,"Wed") == 0)
        run.intDay = 3;
    else if (strcmp(run.day,"Thu") == 0)
        run.intDay = 4;
    else if (strcmp(run.day,"Fri") == 0)
        run.intDay = 5;
    else if (strcmp(run.day,"Sat") == 0)
        run.intDay = 6;
    else if (strcmp(run.day,"Sun") == 0)
        run.intDay = 7;
    runs[*i] = run;
    *i += 1;
}

但这不会将 intDay 值存储在我的数组 running[] 中,而且我真的不明白为什么它不存储。我在这里和其他论坛上查看了如何执行此操作的示例,但一定有一些东西我一直缺少,所以如果有人能告诉我它是什么,那么我将不胜感激:)

I'm having quite a bit of trouble with something that should be really simple. In my program I have a struct called "Run":

typedef struct{
    char name[MAXNAMELENGTH], day[MAXDAYLENGTH];
    int distance, intDay;
    Date startDate;
    Time startTime;
    Time runTime;
} Run;

I parse data from a text file to this struct by using fgets() to parse a single line into an array called line[] and then call this function:

void parseTable(char line[NUMBEROFLINES], Run run, Run runs[NUMBEROFLINES], int *j){
    sscanf(line,"%s %s %s %d, %s %d:%d %d %d:%d:%d",run.name, run.day, run.startDate.month, &run.startDate.date, run.startDate.year,&run.startTime.hours, &run.startTime.minutes, &run.distance, &run.runTime.hours, &run.runTime.minutes, &run.runTime.seconds);
    runs[*j] = run;
    *j+=1;    
}

Now this function correctly assigns all the data to the struct run and stores that struct in the array runs[], but after this I wish to assign the struct a new value: intDay.
For that I call upon the following function:

void dayToInt(Run run, Run runs[NUMBEROFLINES], int *i, int *a, int *b){
    if (strcmp(run.day,"Mon") == 0)
        run.intDay = 1;
    else if (strcmp(run.day,"Tue") == 0)
        run.intDay = 2;
    else if (strcmp(run.day,"Wed") == 0)
        run.intDay = 3;
    else if (strcmp(run.day,"Thu") == 0)
        run.intDay = 4;
    else if (strcmp(run.day,"Fri") == 0)
        run.intDay = 5;
    else if (strcmp(run.day,"Sat") == 0)
        run.intDay = 6;
    else if (strcmp(run.day,"Sun") == 0)
        run.intDay = 7;
    runs[*i] = run;
    *i += 1;
}

But this doesn't store the intDay value in my array runs[] and I really don't see why it doesn't. I've looked here and on other forums to see examples of how to do it but there must be something I keep missing, so if anyone can tell me what it is then it would be greatly appreciated :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

青萝楚歌 2024-12-25 23:46:05

这里的问题是“按值传递”。

当您调用该函数时:
void dayToInt(Run run, Run running[NUMBEROFLINES], int *i, int *a, int *b){

第一个参数 run 实际上被复制到函数的本地副本中。当您修改run.intDay时,它只会修改本地副本。

当您从函数返回时,所有本地修改都会丢失,并且调用者范围内的原始结构保持不变。

要解决此问题,请将函数更改为“Pass-by-Reference”,这意味着将指针传递给要更改的结构:

void dayToInt(Run *prun, Run runs[NUMBEROFLINES], int *i, int *a, int *b){
    if (strcmp(prun->day,"Mon") == 0)
        prun->intDay = 1;
    else if (strcmp(prun->day,"Tue") == 0)
        prun->intDay = 2;
[etc, etc]

编辑 进一步检查后,看起来

runs[*i] = run;

应该执行 以下行:结构的副本,并保留调用者范围内的更改。
所以我不确定为什么对 run.intDay 的更改会丢失。正在进一步调查。

The issue here is "Pass-by-Value".

When you call the function:
void dayToInt(Run run, Run runs[NUMBEROFLINES], int *i, int *a, int *b){

the first parameter, run, is actually copied into a local copy for the function. When you modify run.intDay, it only modifies the local copy.

When you return from the function, all local modifications are lost, and the original structure, at the caller's scope, remains unchanged.

To fix the problem, change the function to "Pass-by-Reference", meaning, pass a pointer to the struct you wish to change:

void dayToInt(Run *prun, Run runs[NUMBEROFLINES], int *i, int *a, int *b){
    if (strcmp(prun->day,"Mon") == 0)
        prun->intDay = 1;
    else if (strcmp(prun->day,"Tue") == 0)
        prun->intDay = 2;
[etc, etc]

Edit On further inspection, it looks like the line:

runs[*i] = run;

should perform a copy of the structure, and preserve changes at the caller's scope.
So I'm not sure why the changes to run.intDay are being lost. Investigating further.

仅此而已 2024-12-25 23:46:05

你的代码有效。

因此,您的麻烦要么在于如何调用该函数,要么在于如何检查它是否工作(您是否检查数组的正确元素,即当前值 i 之前的元素,这返回时刚刚增加?)

Your code works.

So, either your trouble lies in how you call the function, or on how you check that it works (do you check the correct element of the array, i.e. the one before the current value of i, which on return has just been incremented?)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文