【问题标题】:Records not updating in C because struct pointer isn't updating由于结构指针未更新,记录未在 C 中更新
【发布时间】:2015-10-10 17:32:45
【问题描述】:

这是我的代码:

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

typedef struct{
    char item[1000];
} itemset;

void additem(itemset* items,char* thing,unsigned long* numadded){
    memcpy(items->item,thing,1000);
    items++;(*numadded)++;
}

void showitems(itemset* items,unsigned long numitems){
    itemset* p=items;
    unsigned long count;
    for (count=1;count<=numitems;count++){
        printf("%d %s\n",count,p->item);p++;
    }
}

int main(){
    itemset* myitems=calloc(1,sizeof(itemset)*10);
    itemset* itemstart=myitems;
    unsigned long added=0;
    additem(myitems,"Test",&added);
    additem(myitems,"Test2",&added);
    additem(myitems,"Test3",&added);
    printf("Count=%d\n",added);
    showitems(itemstart,added);
    free(itemstart);
}

我试图通过运行此代码在屏幕上看到的是:

Count=3
1 Test
2 Test2
3 Test3

但是,我看到了:

Count=3
1 Test3
2
3

因此,我的 additem 功能无法正常工作。我发现解决方法是在每个函数调用之后立即添加 myitems++;,但我试图在 additem 函数内部发生相同的行为,这样我就不必在函数外部使用 myitems++;。我还能做些什么来解决这个问题?

【问题讨论】:

    标签: c function pointers struct


    【解决方案1】:

    items 使用双指针,如下所示:

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    typedef struct{
        char item[1000];
    } itemset;
    
    void additem(itemset** items,char* thing,unsigned long* numadded){
        memcpy((**items)->item,thing,1000);
        (*items)++;
        (*numadded)++;
    }
    
    void showitems(itemset* items,unsigned long numitems){
        itemset* p=items;
        unsigned long count;
        for (count=1;count<=numitems;count++){
            printf("%d %s\n",count,p->item);
            p++;
        }
    }
    
    int main(void){
        itemset* myitems=calloc(1,sizeof(itemset)*10);
        itemset* itemstart=myitems;
        unsigned long added=0;
        additem(&myitems,"Test",&added);
        additem(&myitems,"Test2",&added);
        additem(&myitems,"Test3",&added);
        printf("Count=%d\n",added);
        showitems(itemstart,added);
        free(itemstart);
        return 0;
    }
    

    输出:

    Count=3
    1 Test
    2 Test2
    3 Test3
    

    另外,检查一下:How to print an unsigned long int with printf in C?,它表示您应该使用%lu,而不是%d

    【讨论】:

      【解决方案2】:

      我只是设法为我找到了一个有效的答案,而刚刚回答的人也给出了类似的答案。

      #include <stdio.h>
      #include <stdlib.h>
      #include <string.h>
      
      typedef struct{
          char item[1000];
      } itemset;
      
      void additem(itemset** items,char* thing,unsigned long* numadded){
          memcpy((**items).item,thing,1000);
          (*items)++;(*numadded)++;
      }
      
      void showitems(itemset* items,unsigned long numitems){
          itemset* p=items;
          unsigned long count;
          for (count=1;count<=numitems;count++){
              printf("%d %s\n",count,p->item);p++;
          }
      }
      
      int main(){
          itemset* myitems=calloc(1,sizeof(itemset)*10);
          itemset* itemstart=myitems;
          unsigned long added=0;
          additem(&myitems,"Test",&added);
          additem(&myitems,"Test2",&added);
          additem(&myitems,"Test3",&added);
          printf("Count=%d\n",added);
          showitems(itemstart,added);
          free(itemstart);
      }
      

      【讨论】:

      • 我仍然觉得我的答案更好。你的编译吗? :) 好问题顺便说一句,+1。
      • 是的,否则我不会发布它
      【解决方案3】:

      您在添加项目函数中编写了相同的指针。您需要将指针传递给指针才能使其工作。还使用 strcpy 进行字符串复制。并且对于 showitems 传递在 calloc 调用中分配的指针,因为它不会改变

      【讨论】:

        【解决方案4】:

        您还需要通过“引用”传递items,而不仅仅是numadded。或者可能返回新的指针。

        【讨论】:

        • 通过“引用”是 C++ 方式。 C中不存在,只能在C中给出变量的地址。
        • @CaptainWise 这就是我将引用放在引号中的原因,因为 C 没有它,但可以模拟它。
        猜你喜欢
        • 2021-03-13
        • 2021-06-08
        • 2019-05-26
        • 2020-10-17
        • 2021-11-15
        • 1970-01-01
        • 1970-01-01
        • 2020-11-20
        • 1970-01-01
        相关资源
        最近更新 更多