0

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");
    }
Semih
  • 87
  • 2
  • 2
  • 10
  • Your `title` and `singerName` are arrays of pointers. They dont need to be. You can declare them like this `char *title` or like this `char title[MAX]` take a look at this http://stackoverflow.com/questions/859634/c-pointer-to-array-array-of-pointers-disambiguation – TsSkTo Oct 15 '13 at 07:25

4 Answers4

6
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.

zakinster
  • 10,508
  • 1
  • 41
  • 52
3

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

char name[MAX];
Louis Hugues
  • 586
  • 4
  • 6
0

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.

lordkain
  • 3,061
  • 1
  • 13
  • 18
  • `strncpy` also depends on a trailing `\0` if you don't know the length of one of the parameters (like in this case). – zakinster Oct 15 '13 at 07:28
  • In this case there is a MAX defined :) – lordkain Oct 15 '13 at 12:55
  • Yes, but the size of the string parameters of `add` (`albumTitle` and `singerName`) could be less than `MAX`, so they would still have to be null-terminated strings, even with `strncpy`. – zakinster Oct 15 '13 at 13:00
0

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;
};
Batuu
  • 595
  • 1
  • 8
  • 22