-4

I'm attempting to print a string from a linked list..for some reason it is outputting random symbols and characters.

Here is my code:

int main(){

char c;
int titleCount;
int authorCount;
char bookTitle[35];
char author[35];



/* Create Struct */
typedef struct bookData{
char bookTitle[35];
char author[35];
int book_stock;
float retail_price;
int wholesale_purchased;
int customer_purchased;
struct bookData *book;
}new_book;

/*Create Node */

typedef struct Node{
new_book books;
struct Node *next;
}node_t;




/* We are GUARANTEED at least 1 input value, so go ahead and initialize 
the 
head */

node_t *head = NULL; /*Initalize head to NULL since it is empty */
head = malloc(sizeof(node_t));
if(head == NULL){
printf("allocation failed");
return 0;
}
head -> next = NULL;
/*Memory allocation successful */

/*Might as well populate the head with data from the user */

titleCount = 0;
authorCount = 0;


printf("Enter Title\n");
while(( c = getchar()) != '\n'){
    bookTitle[titleCount++] = c;

}
bookTitle[titleCount] = '\0';

printf("%s", bookTitle);
strcpy((head -> books).bookTitle,bookTitle);

printf("Enter Author\n");
while(( c = getchar()) != '\n'){
    author[authorCount++] = c;
}
author[authorCount] = '\0';
strcpy((head -> books).author, author);


printf("Bookstock #:\n");
scanf("%d", &(head -> books).book_stock);

printf("Enter retail price $:\n");
scanf("%f", &(head -> books).retail_price);

printf("Enter Wholesale purchased quanity:\n");
scanf("%d", &(head  -> books).wholesale_purchased);

printf("Enter quantity sold:\n");
scanf("%d", &(head -> books).customer_purchased);



printf("%c\n", head -> books.bookTitle);

printf("%c\n", head -> books.author);

printf("%d\n", head -> books.book_stock);

printf("%.2f\n", head -> books.retail_price);

printf("%d\n", head -> books.wholesale_purchased);

printf("%d\n", head -> books.customer_purchased);


}

And my output as follows:

Output

I'm forced to use char array of 35 so there is no getting around that. I'm positive that the pointer calls are correct as that seems to be what all the answers related say.

Thanks

user2736738
  • 30,591
  • 5
  • 42
  • 56
Kyle
  • 695
  • 6
  • 24

2 Answers2

0

head->books.bookTitle is a char array and assigning directly this with another array causing error as

error: assignment to expression with array type

The below statement

head -> books.bookTitle = bookTitle;

should be

strcpy(head -> books.bookTitle, bookTitle);

Similarly head -> books.author = author; -> strcpy((head -> books).author,author);

Also wholesale_purchased is declared as int inside the structure and you are using %f as a format specifier which is wrong, it should be

scanf("%d", &(head  -> books).wholesale_purchased);

Suggestion : don't ignore compiler warnings and use GDB to debug it.

Achal
  • 11,821
  • 2
  • 15
  • 37
0

Here the problem is in here,

typedef struct bookData{
char bookTitle[35];
char author[35];
int book_stock;
float retail_price;
int wholesale_purchased;
int customer_purchased;
struct bookData *book;
}new_book;

Change it with following:

typedef struct bookData{
char* bookTitle;
char* author;
int book_stock;
float retail_price;
int wholesale_purchased;
int customer_purchased;
struct bookData *book;
}new_book;

or use strcpy() function as suggested in achals answer. In the LHS, you're using an array type, which is not assignable. so you have to copy it with strcpy() or copy the pointer of the string that is been assigned.

other solution is to use

 head->books = (new_book){bookTitle,author};

this is not a direct assignment so it will work.

Mandar Sadye
  • 689
  • 2
  • 9
  • 30