-3

I have a programme that adds new positions to structure list. Structure definition is:

struct data
{
    char name[50];
    char surname[50];
    float income;
    float taxed_income;
    char account[20];
    char id[10];
    struct data *next, *prev;
};

and function that adds data is :

struct data* add_element()

{
    struct data *tmp;
    bool flag=true;
    float input;
    char inpt[50];
    tmp = NULL;
    do {
        printf("Id: ");
        fgets(inpt,49,stdin);
        if(head != NULL)
            flag=id_check(inpt);
    }
    while(!flag);
    strcpy(tmp->id,inpt);
    do {
        printf("Name: ");
        fgets(inpt,49,stdin);
        flag=check(inpt);
    }
    while(!flag);
    strcpy(tmp->name,inpt);
    do {
        printf("Surname: ");
        fgets(inpt,49,stdin);
        flag=check(inpt);
     }
    while(!flag);
    strcpy(tmp->surname,inpt);
    do {
        printf("Account: ");
        fgets(inpt,49,stdin);
        flag=check_num(inpt);
    }
    while(!flag);
    strcpy(tmp->account,inpt);
    printf("Income: ");
    scanf("%f",&input);
    tmp->income=input;
    printf("taxed Income: ");
    scanf("%f",&input);
    tmp->taxed_income=input;
    return tmp;
}

programme crashes when I reach first strcpy. all checks are assuring that input meet project spec.

jacob
  • 4,656
  • 1
  • 23
  • 32
  • 3
    you never allocate memory for the struct, tmp just points at null and you write there which will crash the program – Kai Iskratsch Jan 20 '16 at 07:50
  • 2
    Please oh please try to format your code in a consistent and readable (for others) way. Consistent indentation and extra line-breaks makes wonders for readability (and therefore maintainability). – Some programmer dude Jan 20 '16 at 07:50
  • Try to allocate memory followed by snprintf function to copy string. This will work fine for you. – Dhananjay Kashyap Jan 20 '16 at 07:56
  • Again, we desperately need a canonical duplicate for "my program copies data into an uninitialized pointer why does it crash". If anyone got a better duplicate than [this one](http://stackoverflow.com/questions/6923574/what-can-cause-segmentation-faults-in-c) please let me know. – Lundin Jan 20 '16 at 09:59

2 Answers2

2

The pointer to the data structure that you created does not point to anything in memory since it points to NULL. Hence when you try to call the function strcpy() to copy data into the pointer, the program crashes since it is not copying the data to valid memory.

You might want to do some research into the function malloc() which allows you to allocate memory on the heap for your tmp ptr.

Something like this should suffice:

tmp = malloc(sizeof(struct data));

You should also check if the malloc() returned memory that you can use by adding the line:

assert(tmp != NULL);

Note: Don't forget to include the <assert.h> library.

Paul R
  • 208,748
  • 37
  • 389
  • 560
MatthewT53
  • 109
  • 5
  • you should not use `assert` for checking the return value of `malloc`: http://stackoverflow.com/q/17732/3684343 – mch Jan 20 '16 at 08:56
-1

The problem is that your have not allocated memory to the pointer to your structure.

struct data *temp can't take data in its fields unless your allocate it memory of the size of the structure as follows:

temp =  malloc (sizeof(struct data));

EDIT: Removed typecast for malloc

Avikalp Srivastava
  • 139
  • 1
  • 1
  • 9
  • Everything except typecast helps. – user3426015 Jan 20 '16 at 08:23
  • @PaulR dependent on compiler also. If using C++ compiler for `C` code, then typecast is needed. – SKD Jan 20 '16 at 08:25
  • 1
    Casting the return value of malloc will hide issues such as failure to include `stdlib.h` in your source file. It's a bad idea to do in C as a `void *` can be implicitly casted to any other pointer, so it's redundant. – jacob Jan 20 '16 at 08:41