Thread: Pointer types

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    11

    Pointer types

    I'm having a problem with incompatible pointer types

    [Warning] assignment from incompatible pointer type
    Code:
    struct CD {
      unsigned int ID;
      char *Title;
      char *Artist;
      char *Genre;
    
      int  Year;
      int  Time;
      int  Number_Tracks;
      struct Songs *Track_Ptr;
    };
    
    struct Node {
      struct CD *CD_Ptr;       // payload (CD information)
      struct Node *Next_Ptr; // point to next (linked-list)
    };
    
    void dbload();
    
    char temp[MAX_TITLE_LEN], tempa[MAX_NAME_LEN];
    struct Node *head_ptr, *tail_ptr, *cur_ptr;
    
    fscanf(fp1, "%[^\t]\t %[^\t]\n", &temp, &tempa);
    cur_ptr = (struct Node*)malloc(sizeof(struct Node));
    cur_ptr->CD_Ptr = (struct CD*)malloc(sizeof(struct CD));
    if (cur_ptr == NULL) exit(1);
    cur_ptr->Next_Ptr = NULL;
    if (head_ptr == NULL)
      head_ptr = cur_ptr;
    else
      tail_ptr->Next_Ptr = cur_ptr;
    tail_ptr = cur_ptr;
    
    
    cur_ptr->CD_Ptr->Title = (struct CD*)malloc(strlen(temp));
    strncpy(cur_ptr->CD_Ptr->Title, temp, MAX_TITLE_LEn);    
    
    cur_ptr->CD_Ptr->Artist = (struct CD*)malloc(strlen(tempa));
    strncpy(cur_ptr->CD_Ptr->Artist, tempa, MAX_NAME_LEN);
    I tried switching the (struct CD*) to (struct Node*), thinking that would fix it, but it doesn't

    Oddly enough, it gives me a warning if I have the Title bit in there, but runs fine. As soon as I add the Artist bit, crash and burn.

    Also, after putting the code into this message, I hit F9 trying to compile it....

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Next time give us the line number.

    Title in struct CD is a char *. Artist in struct CD is a char *. So why are you trying to cast the result of malloc() in both cases to a struct CD *?

  3. #3
    Registered User
    Join Date
    Mar 2007
    Posts
    11
    Woops, meant to give a line number on that...
    Are you saying I don't need to cast it at all?

  4. #4
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    You never need to cast malloc. Further, in this case, if you were to cast it, it'd be to char* and not to struct CD*.
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  5. #5
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Why malloc shouldn't be casted

    ssharish2005

  6. #6
    Registered User
    Join Date
    Mar 2007
    Posts
    11
    Hrmm, still gives me problems, but thanks for the help.

  7. #7
    Day Dreamer
    Join Date
    Apr 2007
    Posts
    45
    "You never need to cast malloc"

    Could you explain? How did you come to that conclusion?

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You never need to typecast assignments from a void pointer to another type. That's just how C works.


    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    "You never need to cast malloc"

    Could you explain? How did you come to that conclusion?
    If you bothered to read the link that ssharish2005 posted you'd understand.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. assignment from incompatible pointer types
    By iunah in forum C Programming
    Replies: 13
    Last Post: 10-12-2008, 03:32 AM
  2. Ban pointers or references on classes?
    By Elysia in forum C++ Programming
    Replies: 89
    Last Post: 10-30-2007, 03:20 AM
  3. scope of a pointer?
    By Syneris in forum C++ Programming
    Replies: 6
    Last Post: 12-29-2005, 09:40 PM
  4. Question About Pointer To Pointer
    By BlitzPackage in forum C++ Programming
    Replies: 2
    Last Post: 09-19-2005, 10:19 PM
  5. declaring function pointer types for callbacks
    By Pea in forum C Programming
    Replies: 6
    Last Post: 01-06-2005, 09:46 PM