1
struct list_el {
    unsigned int data;
    char name[16];
    char grade;
    struct list_el * next;
};

typedef struct list_el item;

int main(int argc, char **argv) {
    item *curr, *head;

    //first item
    curr->data = 3141592;
    strcpy(curr->name, "Carl");
    curr->grade = 'A';
    curr->next = head;
    head = curr;

Trying to figure out why this isn't working when I try to set name to "Carl". I'm getting "too few arguments to function 'strcpy'" even though I have 2 arguments in it (destination, source). When I add a 3rd argument (how many characters to copy?), I end up getting "assignment to expression with array type".

5
  • 2
    Post an MCVE and you haven't allocated memory for curr before using it. Commented May 1, 2015 at 5:37
  • Not sure why this would fail to compile but it's definitely UB - you're declaring a pointer to an item but have not initialized it to an actual instance. Commented May 1, 2015 at 5:40
  • I'm sorry, this is preventing eclipse from building my project for me... Commented May 1, 2015 at 5:40
  • it's working after allocating memory x.x Thanks for the help! Wonder why it didn't give me an error for the rest of it... Commented May 1, 2015 at 5:45
  • Does you true code have #include<string.h>? Commented May 1, 2015 at 11:50

2 Answers 2

1

I am not getting your error "too few arguments to function 'strcpy' " but i can see that you have declare a item *curr but never initialized.So i tried with correcting this and updated your code as below and its not giving any error.

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct list_el {
    unsigned int data;
    char name[16];
    char grade;
    struct list_el * next;
};

typedef struct list_el item;

int main(int argc, char **argv) {
    item *curr, *head;
    curr = malloc(sizeof(struct list_el));
    //first item
    curr->data = 3141592;
    strcpy(curr->name, "Carl");
    curr->grade = 'A';
    curr->next = head;
    head = curr;
}

I think, it will work for you.

Sign up to request clarification or add additional context in comments.

1 Comment

Idea: rather than curr = malloc(sizeof(struct list_el));, consider curr = malloc(sizeof *curr); Easier to code, less likely to be wrong and easy to maintain.
1

You did not allocate memory for curr before using it. Two options:

  1. Make curr point to an instance of item:

    item i;
    curr = &i;
    
  2. Allocate memory dynamically for curr using malloc/calloc:

    curr = malloc(sizeof(*curr));
    /*OR*/
    curr = calloc( 1, sizeof(*curr));
    

    and later, free it after its use. It is also a good idea to check if malloc/calloc did not fail by checking its return value. It will return NULL on failure.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.