Thread: Going blind can't see my error, please help.

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    103

    Going blind can't see my error, please help.

    Hey guys I just started coding in C, so i'm no up to par with everything. I've been writing a program that deals with structs and linked lists, and I've run into an "imcompatible type in assignment" error and I can't see what's going on because I've been looking at this code for 1.5 hrs now. please help me. Thanks!

    here's my func file
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include "funcs.h"
    
    
    LIST_ELEM *add(LIST_ELEM *pHead,char type[],char name[], char title[], int year, int price, int size){
    
    	LIST_ELEM *new_elem = malloc(sizeof(LIST_ELEM));
    
    	new_elem->type = type;
    	new_elem->name = name;
    	new_elem->title = title;
    
    	new_elem->year = year;
    	new_elem->price = price;
    	new_elem->size = size;
    
    	return new_elem;
    
    }
    my header
    Code:
    typedef struct list_elem
    {
    	char type[5];
    	char name[50];
    	char title[50];
    	int year;
    	int price;
    	int size;
    	struct list_elem *pNext;
    }LIST_ELEM;
    
    LIST_ELEM *add(LIST_ELEM *pHead,char type[],char name[], char title[], int year, int price, int size);
    int size(LIST_ELEM *phead);
    LIST_ELEM *find(LIST_ELEM *pHead, int value);
    void print(LIST_ELEM *pHead);
    and my main (its a noob program, but i'm a noob so its ok )
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include "funcs.h"
    
    int main(void){
    
      LIST_ELEM *pHead = NULL;
      char type[5];
      char name[50];
      char title[50];
      int year;
      int price;
      int size;
    
      int choice;
    
      do {
    	printf("What would you like to do? \n\n");
    	printf("0. Exit \n");
    	printf("1. Print books/cds. \n");
    	scanf("%d", &choice);
    
    	switch(choice){
    	  case 1:
    	  	print(pHead);
    		break;
    	  case 2:
    		printf("Enter 'book' or 'cd' for type: \n");
    		scanf("%s", &type);
    		printf("Enter name of author/artist: \n");
    		scanf("%s", &name);
    		printf("Enter title: \n");
    		scanf("%s", &title);
    		printf("Enter year published/released: \n");
    		scanf("%d", &year);
    		printf("Enter price: \n");
    		scanf("%d", &price);
    		printf("Size of book/cd: \n");
    		scanf("%d", &size);
    
    		pHead = add(pHead,type,name,title,year,price,size);
    		break;
    		
    			}
    		}while(choice != 0);
    	return 0;
    }

    I know it has to do with the passing in of character string for my add() method, but I don't know what the right syntax is to get it to work. Thanks again!

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,334
    You can't copy strings with "=" (as in new_elem->type = type), you must use strcpy.

  3. #3
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    The problem is when you're attempting to fill your struct...

    new_elem->type = type

    The string array type is passed to your function as a pointer only. You want to save the contents of that string... so use

    strcpy(new_elem->type, type)

    ... which moves the actual characters.... Hopefully the destination array sizes are always large enough to accommodate the user's input. This is a common "bug", when an evil user (aren't they all?) writes and writes and writes, and causes your program to overshoot the destination array.

  4. #4
    Registered User
    Join Date
    Oct 2008
    Posts
    103
    THnx that worked. But now when I run it, and enter a title thats bigger then 7 letters it skips one or two of the following printfs. Am i experiencing the whole overshooting the destination array? How would i fix that? I set my char array to be [50] but that didn't work.
    Last edited by TaiL; 10-02-2008 at 06:45 PM.

  5. #5
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    It's called a header file. Yes, it's a good idea to include that whenever you use any string functions.

  6. #6
    Registered User
    Join Date
    Oct 2008
    Posts
    103
    Sorry I edited but it didn't work.

    When I type in a string of size larger then 7 letters it skips a couple printfs.

    so say for title i put in : the cat and the hat

    it jumps over the author and goes right to the publish date. But it doesn't make sense since i allocate sizes of 50 for the title and 50 for the author.

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,334
    But five characters works fine? Are you sure you don't have %7s or something like that in your program?

  8. #8
    Registered User
    Join Date
    Oct 2008
    Posts
    103
    yeah pretty sure.

    if i put in

    name: heres myname

    its skips down to date published:

    but when i do a print out of it
    i get

    name: heres
    title: name

  9. #9
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Yeah I can't replicate the "skipping printfs" at all. I did notice that the new_elem->pNext is not being updated... to link the malloced elements together. But that's not the issue at the moment.

    No wait.... if I enter in anything which has spaces in it, the scanf absorbs it into the numbers.... I should have seen that coming. It's late in the day.

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,334
    Quote Originally Posted by TaiL View Post
    yeah pretty sure.

    if i put in

    name: heres myname

    its skips down to date published:

    but when i do a print out of it
    i get

    name: heres
    title: name
    Ha ha! It's not five characters -- it's the space. &#37;s stops at any whitespace (spaces, tabs, or enter key). To read the whole line, use fgets.

    (Note to reader: Ha ha! must be given your best over-dramatic villain interpretation.)

  11. #11
    Registered User
    Join Date
    Oct 2008
    Posts
    103
    thanks. for that one. Interesting how you don't get the skipping printfs.

  12. #12
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    No I do.... sort of. They go flying by real fast because their associated inputs are already satisfied. No waiting at the keyboard for those.

  13. #13
    Registered User
    Join Date
    Oct 2008
    Posts
    103
    Not too sure with fgets

    from what i can understand fgets takes in a (str,int, file);

    but in my case i don't have a file so can I leave file blank?

  14. #14
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,334
    Your file is stdin.

  15. #15
    Registered User
    Join Date
    Oct 2008
    Posts
    103
    so instead of

    Code:
    printf("Enter title: ");
    scanf("%s", &title);
    i do

    Code:
    printf("Enter title: ");
    fgets(&title,50, stdin);
    so then I don't have to declare the size of the char array at the beginning of my file then?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help needed, going blind.
    By datait in forum C Programming
    Replies: 3
    Last Post: 12-14-2007, 08:01 AM
  2. Bachelor for blind and deaf
    By afreedboy in forum A Brief History of Cprogramming.com
    Replies: 20
    Last Post: 12-18-2003, 07:42 AM
  3. Blind date...Oh, my God!
    By Carlos in forum A Brief History of Cprogramming.com
    Replies: 28
    Last Post: 09-18-2001, 10:44 PM