-3

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

  • 3
    This code is too incomplete. Please provide complete code as a [mre] as well as the exact input, expected result and actual result. – kaylum Apr 22 '22 at 11:20
  • 3
    `CarWash *create_new_queue(int val)` must return a value. You have lost the memory allocation, leaving it dangling. – Weather Vane Apr 22 '22 at 11:27
  • 1
    You're missing `return result;` at the end of the `create_new_queue` function. But there may be more problems in the other parts of your code you didn't show. – Jabberwocky Apr 22 '22 at 11:36
  • 2
    The reason you can't `strcpy()` is because that `struct` member `char *CarId;` is also a pointer, and must be allocated too. Each of those four pointer-members must be allocated. You *could* use `strdup()` but it is non-standard. – Weather Vane Apr 22 '22 at 11:36
  • 1
    `//char *strcpy(char CarId, char Regnr);` This line makes no sense whatsoever, but uncommenting it should make no difference. On the other hand, `CarWash *create_new_queue(int val)` doesn't have a `return` statement, which is fatal. See [this](https://stackoverflow.com/questions/57842756/why-should-i-always-enable-compiler-warnings) for more detail. – n. m. could be an AI Apr 22 '22 at 11:51
  • @WeatherVane @n-1-8e9-wheres-my-share-m the reason for the ```char *strcpy(char CarId, char Regnr);``` is that only answear i have found so far to cast int to char, so thats what i have tryed to use – Michal Andersen Apr 22 '22 at 12:08
  • 1
    @MichalAndersen `char *strcpy(char CarId, char Regnr);` doesn't do anything. It merely _declares_ `strcpy` (and it declares it wrong), which shouldn't be done anyway, because it's already declared in `string.h` – Jabberwocky Apr 22 '22 at 12:12

1 Answers1

0

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.

Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
  • you a god sent thank you, it prints out what i want WOOP WOOP. now i just need to get the free funktion to work and i be one step closer to become a programmer :D and thanks to all who put in your input and your time :) – Michal Andersen Apr 22 '22 at 13:02