我如何在C中的Malloc中启动多个字符串
我正在尝试学习C,并且我有一个使用Malloc和struct的Asignment,并且我将其打印出了队列编号,但字符串不会打印。我已经附上了印刷品的图片,但是只有在评论strcpy并且无法弄清楚时才起作用。
谁能解释我做错了什么,该怎么办?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct car
{
int queue;
char *Regnr;
char *Manufactor;
char *WashType;
char *CarId;
struct car *next;
};
typedef struct car CarWash;
/* print the list out from ... */
void printlist (CarWash * head)
{
CarWash *temp = head;
while (temp != NULL)
{
printf ("%d\t\t %s\t\t %s\t\t %s\t \n", temp->queue, temp->Regnr,
temp->Manufactor, temp->WashType);
temp = temp->next;
}
printf ("\n");
};
CarWash *create_new_queue(int val,char CarId)
{
CarWash *result = malloc (sizeof (CarWash));
char strcpy(char Regnr, char CarId);
result->Regnr = CarId;
result->queue = val;
result->next = NULL;
return result;
};
编辑
int
main ()
{
int queue;
char Regnr[10];
char Manufactor[10];
char WashType[10];
char CarId[10];
CarWash *head;
CarWash *tempB;
FILE *fp;
fp = fopen ("Car wash.txt", "r");
fscanf (fp, "%d%s%s%s", &queue, Regnr, Manufactor, WashType);
fclose (fp);
printf (" \n"); //voodoo to show my printlist
tempB = create_new_queue (queue, Regnr);
head = tempB;
printf ("%s\t %s\t\t %s\t %s\n", "kø plads", "Regnr", "Manufactor",
"vaske type");
printlist (head);
return 0;
}
编辑的
印刷内容:
I'm trying to learn C and I have an asignment to use malloc and struct and I have it print out the queue number but the string wont print. I have attached a picture of the print, but only works when strcpy is commented out and I cant figure it out.
Could anyone explain what I'm doing wrong and what should be done?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct car
{
int queue;
char *Regnr;
char *Manufactor;
char *WashType;
char *CarId;
struct car *next;
};
typedef struct car CarWash;
/* print the list out from ... */
void printlist (CarWash * head)
{
CarWash *temp = head;
while (temp != NULL)
{
printf ("%d\t\t %s\t\t %s\t\t %s\t \n", temp->queue, temp->Regnr,
temp->Manufactor, temp->WashType);
temp = temp->next;
}
printf ("\n");
};
CarWash *create_new_queue(int val,char CarId)
{
CarWash *result = malloc (sizeof (CarWash));
char strcpy(char Regnr, char CarId);
result->Regnr = CarId;
result->queue = val;
result->next = NULL;
return result;
};
edited
int
main ()
{
int queue;
char Regnr[10];
char Manufactor[10];
char WashType[10];
char CarId[10];
CarWash *head;
CarWash *tempB;
FILE *fp;
fp = fopen ("Car wash.txt", "r");
fscanf (fp, "%d%s%s%s", &queue, Regnr, Manufactor, WashType);
fclose (fp);
printf (" \n"); //voodoo to show my printlist
tempB = create_new_queue (queue, Regnr);
head = tempB;
printf ("%s\t %s\t\t %s\t %s\n", "kø plads", "Regnr", "Manufactor",
"vaske type");
printlist (head);
return 0;
}
edited
What gets printed:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可能想要这个:
校正的
create_new_queue
函数:我建议您阅读有关指针的章节以及与学习材料中的字符串有关的章节。
You probably want this:
And the corrected
create_new_queue
function:I recommend you read the chapter dealing with pointers and the chapter dealing with strings in your learning material.