-
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 */
}
-
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.
-
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:
The value this holds is an integer. (1, 345, -23, whatever)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.
-
ok..i get it..thanks both of u..