我如何在C中的Malloc中启动多个字符串

发布于 2025-01-23 07:20:40 字数 1707 浏览 4 评论 0原文

我正在尝试学习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:

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鹿童谣 2025-01-30 07:20:40

您可能想要这个:

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;
};

我建议您阅读有关指针的章节以及与学习材料中的字符串有关的章节。

You probably want this:

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;
};

And the corrected create_new_queue function:

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;
};

I recommend you read the chapter dealing with pointers and the chapter dealing with strings in your learning material.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文