-2
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>

struct BOOK{
char name[15];
char author[33];
int year[33];
};

struct BOOK *books;
int main(){
int i,noBooks;
noBooks=2;
books=malloc(sizeof(struct BOOK)*noBooks);
books[0].year=1986;
books[0].author="JackLondon";
books[0].name='MartinEden';

getch();
return 0;
}

my code is that. when i use scanfit works but i cant appoint directly like that.

errors are:

error: incompatible types when assigning to type 'int[33]' from type 'int'|
error: incompatible types when assigning to type 'char[33]' from type 'char *'|
error: incompatible types when assigning to type 'char[15]' from type 'int'|
Build finished: 3 errors, 1 warnings 

how can i appoint directly, where is my wrong?

kfb
  • 6,252
  • 6
  • 40
  • 51

3 Answers3

1

What you did:

// assign a numeric value to an array
int year[33];
books[0].year=1986;

// assign a pointer to a memory location of an array 
char name[15];
char author[33];
books[0].author="JackLondon";
books[0].name='MartinEden';

How it should look

struct BOOK{
    char name[15];
    char author[33];
    int year;
};

// ==============================

// assign numeric value to a normal int variable
books[0].year=1986;
// copy values to arrays
strcpy(books[0].author, "JackLondon");
strcpy(books[0].name, "MartinEden");
Dariusz
  • 21,561
  • 9
  • 74
  • 114
1

You cannot assign a value of one type to a variable defined as a different type. In your example, you attempt to assign the single integer 1986 to an array of 33 int variables.

Your other errors are slightly more subtle to explain. In C, the value "a string" has the type char *, which is not the same type as char[], so the assignment is invalid. Whilst they are roughly equivalent, but have slightly different properties—read up on the difference between char pointers and char arrays when it comes to dealing with strings.

kfb
  • 6,252
  • 6
  • 40
  • 51
0

There are multiple problems with this code. One problem is that you declare 'year' to be an array of 33 integers. You probably want int year; not int year[33]; unless, for some reason, you are keeping a list of 33 different years in association with each book. This explains the first error.

The problem with the second and third assignments (author and year) is that you are confused about the difference between arrays and pointers in C. (Sometimes pointers and the names of arrays can be used interchangeably in C, but not always, and this is a constant source of frustration to new C programmers, because it's not immediately intuitive why some usages are wrong and others are safe.) There are many references on the net that explain this better than I can. Here's one: http://eli.thegreenplace.net/2009/10/21/are-pointers-and-arrays-equivalent-in-c/

It doesn't help that the compiler aids this confusion by giving you special syntax to initialize arrays of characters. When you declare a char array, like so:

char c[33]="JackLondon";

the compiler will reserve 33 bytes for c, then copy the bytes of "JackLondon" in to the beginning of your array, more or less as if you had declared the array, and then initialized it with a strcpy().

In your specific case, what you want is to use strcpy (or, preferably strncpy) to load your structure, like so:

strncpy(books[0].author, "JackLondon", sizeof(books[0].author));
JVMATL
  • 2,064
  • 15
  • 25