Semih
Semih

Reputation: 87

Strcpy incompatible pointer type in C

This code which you see the below is part of my project. When I compile this code , I get the error.Error is "passing argument 1 of 'strcpy' from incompatible pointer type" and expected 'char ' but argument is of type 'char *'.How can I fix this ? Thank you.

 struct songs
{
    char name[MAX];
    double length;
    struct songs *next;
};
typedef struct songs songs;

struct albums
{
    char title[MAX];
    int year;
    char singerName[MAX];
    songs *bas;
    songs *current;
    struct albums *next;
};
        void add(char albumTitle[],char singerName[], int releaseYear )
    {
        struct albums *temp;
        temp=(struct albums *)malloc(sizeof(struct albums));
        strcpy( temp->title, albumTitle ); /* ERROR */
        temp->year=releaseYear; 
        strcpy( temp->singerName, singerName ); /* ERROR */
        if (head== NULL)
        {
        curr=head=temp;
        head->next=NULL;
        curr->next=NULL;
        }
         else
        {
         curr->next=temp;
         curr=temp;
        }

        printf("Done\n");
    }

Upvotes: 0

Views: 18972

Answers (4)

zakinster
zakinster

Reputation: 10688

char * strcpy ( char * destination, const char * source );

strcpy manipulates strings of characters, which in C are represented with a null-terminated array of char, which has the type char[] or char*.

However, in your code :

struct albums
{
    char* title[MAX];
    ...
    char* singerName[MAX];
    ...
};

char* [] means an array of char*, which is an array of pointer to char. albums.title and albums.singerName are thus not strings, but arrays of pointers. You should change it to char title[MAX] in order to have strings.

Upvotes: 6

Batuu
Batuu

Reputation: 595

You declared arrays of pointers. Get rid of the pointers:

struct albums
{
    char title[MAX];
    int year;
    char singerName[MAX];
    songs *bas;
    songs *current;
    struct albums *next;
};

Upvotes: 0

lordkain
lordkain

Reputation: 3109

A note of importent, you main question had alreaddy been answered by zakinster and SioulSeuguh

use strncpy instead of strcpy

strcpy depends on trailing \0. if this is not there, you got problems with buffer overflow.

Upvotes: 0

Louis Hugues
Louis Hugues

Reputation: 596

You are defining array of pointers to char and not array of chars. use instead.

char name[MAX];

Upvotes: 3

Related Questions