9

I have defined a "car" struct with a model (char *model) and the year of the model (int year). I have a function that will create a new car struct; however, it is seg faulting when copying the char pointers. This is supposed to create a new node for a linked list.

Car *newCar(char *model, int year){
    Car *new = malloc(sizeof(Car));
    new->year = year;
    new->model = malloc(MAX_LENGTH*sizeof(char));
    strcpy(new->model, model);
    new->next = NULL;
    return new;
}
8
  • How about new->model = malloc(strlen(model) + 1) ? Commented Mar 11, 2013 at 6:39
  • 4
    You should check that char *model is not NULL. Also, as good practice, always check the return of mallocs. Commented Mar 11, 2013 at 6:39
  • @cnicutar thanks; however, the issues is still there. Commented Mar 11, 2013 at 6:41
  • @Cong Xu char *model will never be NULL because it is scanning in data from a file in another function. Commented Mar 11, 2013 at 6:42
  • 1
    @KinjalPatel , he can safely use new with a c compiler Commented Mar 11, 2013 at 6:55

4 Answers 4

7

For future reference this function fixed my issue...

Car *createCar(char *model, int year){
    Car *new = malloc(sizeof(Car));
    new->year = year;
    new->model = malloc(strlen(model)+1);
    strcpy(new->model, model);
    new->next = NULL;
    return new;
}
Sign up to request clarification or add additional context in comments.

2 Comments

You malloc'd the wrong amount of space. It should be strlen(model)+1. If this appeared to fix your issue, you have been walking on eggshells!
@M.M you're right! I was going through my old homework assignments from freshman year and realized that I never posted the solution. I've updated my answer to reflect the error you caught.
4

Here your model is character pointer.

But strcpy requires two arguments - that should be array or character pointer to which memory allocated by malloc or calloc

But your strcpy(); takes one argument as character pointer which will not be accepted.

so make

new->model = malloc(strlen(model) + 1) and then write your strcpy () it will work.

3 Comments

or new->model = strdup (model); which does the same in one single instruction.
@EdouardThiel Except strdup isn't standard (although it can easily be implemented).
strdup() conforms to SVr4, 4.3BSD, POSIX.1-2001.
4

You may try this :

new->model = model == NULL ? NULL : strdup(model);

this prevents you from a bug if model is NULL, else malloc you the exact amount of space and strcopy it; plus, this allows you to free(new->model) at the end in all the cases.

Comments

2

Have a look at the code below and compare it with your program, am sure you will find out what's wrong with your program

#include <stdio.h>
#include <string.h>

typedef struct car{
char *model;
int year;
}Car;

Car * newCar(char *, int );

int main()
{

Car *benz = newCar("S-class",1990);

printf("\nModel = %s\n",benz->model);
printf("\nYear = %d\n",benz->year);

}

Car * newCar(char *model, int year)
{
    Car *new = malloc(sizeof(Car));
    new->year = year;
    new->model = malloc(strlen(model));
    strcpy(new->model, model);
    return new;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.