如何在 C 中的 malloc 中启动多个字符串
How do I initiate multiple strings in malloc in C
我正在尝试学习 C,我有一个使用 malloc 和 struct 的任务,我让它打印出队列号,但字符串不会打印。我附上了印刷品的图片,但只有在 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;
}
已编辑
打印的内容:
你可能想要这个:
struct car
{
int queue;
char Regnr[10]; // don't declare pointers but arrays
char Manufactor[10];
char WashType[10];
char CarId[10];
struct car *next;
};
以及更正后的 create_new_queue
函数:
CarWash* create_new_queue(int val, char *CarId) // add the *, CarId is not a char
{
CarWash* result = malloc(sizeof(CarWash));
strcpy(result->Regnr, CarId); // actually call strcpy
result->queue = val;
result->next = NULL;
return result;
};
我建议你在学习中阅读处理指针的章节和处理字符串的章节material。
我正在尝试学习 C,我有一个使用 malloc 和 struct 的任务,我让它打印出队列号,但字符串不会打印。我附上了印刷品的图片,但只有在 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;
}
已编辑
打印的内容:
你可能想要这个:
struct car
{
int queue;
char Regnr[10]; // don't declare pointers but arrays
char Manufactor[10];
char WashType[10];
char CarId[10];
struct car *next;
};
以及更正后的 create_new_queue
函数:
CarWash* create_new_queue(int val, char *CarId) // add the *, CarId is not a char
{
CarWash* result = malloc(sizeof(CarWash));
strcpy(result->Regnr, CarId); // actually call strcpy
result->queue = val;
result->next = NULL;
return result;
};
我建议你在学习中阅读处理指针的章节和处理字符串的章节material。