Thread: Character problem!!

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    36

    Character problem!!

    Hi everyone, i kind of having trouble here with character copy. I currently building an linked list where stored character by character not word by word, i don't quite understand how to implement it. i means the error of the "assignment makes a pointer from integer without a cast". Can anyone give me a tips or idea how to deal with this problem cos i am not very good at c..Thanks.

    Here is my sample code that doesn't compile..sorry for incovenient.
    Code:
     
    typedef struct stringStruct
    {
       char character;
       struct stringStruct *next;
    }String;
    
    typedef struct head
    {
       struct stringStruct *head;
       
    }Head;
    
    /* This function copy the line from main.c character by character into
         the linked list */
    
    void InsertCharacter(Head *head, char *_char)
    {
    
    String *current;
    
     /* some function */
     
     /* I try to copy a character by character into the list */
     while ((_char = getchar()) != '\0')
     {
    
     /* some function here */
    
     current -> character = _char; /* error : assignment makes pointer                 
                                                     from integer without a cast */
     }
    }
    
    main()
    {
    
    Head head;
    char _char[30] = "This is a string";
    
    InsertCharacter(head, _char);  /* Here got an error too */
    
    }

  2. #2
    Registered User
    Join Date
    Mar 2005
    Posts
    20
    Code:
    void InsertCharacter(Head *head, char *_char)
    {
    
    String *current;
    
     /* some function */
     
     /* I try to copy a character by character into the list */
     while ((_char = getchar()) != '\0')
     {
    
     /* some function here */
    
     current -> character = _char; /* error : assignment makes pointer                 
                                                     from integer without a cast */
     }
    }
    Firstly, the parameter that your function takes in is a pointer of char type. However you try to assign this to current->character which is of char type. Those are of different types, hence causing the error. Also getchar() returns an integer, thus you should also be receiving the same error on that line.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    while ((_char = getchar()) != '\0')
     {
    
     /* some function here */
    
     current -> character = _char; /* error : assignment makes pointer
    To expand on this, both the top and bottom line here are incorrect. You need to dereference the pointer to assign a value to what it points to. Otherwise, you need to assign an address to it. Remember that pointers are variables whose value they hold is an address. Thus:
    Code:
    int x;
    The value this holds is an integer. (1, 345, -23, whatever)
    Code:
    int *ptr;
    The value this holds is the address of another integer. So the only thing you can assign this is another pointer to an integer, or the address of a non-pointer integer. Like so:
    Code:
    int x;
    int *ptr;
    int *pt2;
    
    ptr = &x; /* The address of a non-pointer integer... */
    pt2 = ptr; /* The value of another pointer... (ie: the address the other pointer contains) */
    So reading the above, can you figure out what's wrong with those two lines?


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

  4. #4
    Registered User
    Join Date
    Mar 2005
    Posts
    36
    ok..i get it..thanks both of u..

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. about wide character and multiple byte character
    By George2 in forum C Programming
    Replies: 3
    Last Post: 05-22-2006, 08:11 PM
  2. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  3. printing strings (was similar problem)
    By weirdbeardmt in forum C Programming
    Replies: 5
    Last Post: 06-01-2004, 01:12 PM
  4. problem comparing character
    By winsonlee in forum C Programming
    Replies: 2
    Last Post: 03-19-2004, 12:15 PM
  5. Problem with character arrays in classes
    By spoketoosoon in forum C++ Programming
    Replies: 3
    Last Post: 03-16-2004, 03:57 AM