程序在VS2012上工作正常,但在使用Tiny C时遇到错误



我想知道我需要什么函数才能将typedef struct指针的数据复制到数组typedef struct

typedef struct nodebase{
    char company[254];
    int counter;
    int rows;
    int column;
    struct nodebase *next;
}data;

我知道我可以将memcpy用于char company但是对于整数值呢?

我想做这样的事情:

int main()
{
data *p;
data item[] = {0};
int counter = 0;
/*Calling for the roster file to scan and store the data into `data *p` using fscanf*/
/*Code for singly linked-list*/
counter++ //This happens everytime the program has scanned 4 variables in the file
item[counter] = p; //This definitely is now working..   

编辑:我现在正在使用memcpy,之前的问题已经解决。

(感谢您的回答!

现在,我遇到了一个新问题,它在我的单链接列表中。

显然,名册文件中

有 12 个"计数器"(这意味着,名册文件中有 48 个变量用于读取和存储数据)。

//Code for Singly Linked-list
int main()
{
data *p;
data *head;
data *tail;
data item[] = {0};
FILE *f;
int counter = 0;
head = NULL;
tail = NULL;
while(!feoe(f)
{
    p = malloc(sizeof(data));
/*Opens the roster file and Read & Store the data in the file to the respective variables inthe `typedef struct`.*/
    if(head ==NULL)
    {
        head = p;
    }
    else
    {
        tail->next = p;
    }
    tail = p;
    if(head!=NULL)
    {
        do{
           printf(":||%s||: Name",p->name); //Just to check if the linked list is working
           memcpy(&item[counter], p, sizeof(data*));
           counter++;
           p = p->next;
           p = NULL;
        }while(p!=NULL);
    }free(p);
}
}

问题:从名册文件中打印 12 个"集"的每个名称时,程序正确打印前 10 个集,然后突然停止工作。(在Windows上使用Tiny C)

奇怪的是,当我使用 VS2012 编译此文件时,它工作正常。

如果要

struct nodebase *(又名data *)指向的内容复制到struct nodebase []data []),确实可以使用memcpy

memcpy(&item[counter], p, sizeof(struct nodebase));

memcpy文档中所述:

源指针和目标指针指向的对象的基础类型与此函数无关;结果是数据的二进制副本。

所以它是整数还是其他任何东西都没有关系。

但是,您将需要一个足够大的数组。 因此,请确保以足够大的大小实例化item数组。

试试这个

memcpy(&item[counter], p, sizeof(data));

您的上述声明无效

item[counter] = p;

查看类型

相关内容