程序适用于VS2012,但在使用Tiny C时遇到错误

时间:2013-10-03 08:33:35

标签: c linked-list typedef singly-linked-list

我想知道将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编译这个文件时,这很奇怪的是,它运行正常。

2 个答案:

答案 0 :(得分:0)

试试这个

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

您的上述陈述无效

item[counter] = p;

查看类型

答案 1 :(得分:0)

如果您要将struct nodebase *(又名data *)指向的内容复制到struct nodebase []data []),您确实可以使用{{1} }:

memcpy

memcpy(&item[counter], p, sizeof(struct nodebase)); 文件中所述:

  

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

因此无论是整数还是其他任何内容都无关紧要。

但是,你需要一个足够大的阵列。所以一定要实例化一个足够大的memcpy数组。