Tuesday, July 28, 2009

In C I have a data structure that has a character array. How do I pass the array to another function?

If my array in the structure is: char word[20];


how do I pass this to this function?





pointer NewListNode(char n) {


pointer p;


p = (pointer) malloc(sizeof(list));


p-%26gt;word = n;


p-%26gt;ptr = NULL;


return p;


}





This is wrong...the compiler said "incompatible types in assignment" where it says "p-%26gt;word=n. What is the correct syntax?

In C I have a data structure that has a character array. How do I pass the array to another function?
Well, your variable "n" is a character (char), and not a character array. So, something more appropriate would be:





"p-%26gt;word[3] = n;"





This would make the 4th character in the array equal to whatever "n" is. If you want "n" to be an array, the function should be like:





"pointer NewListNode(char* n) {"





Where the "char*" means that "n" would be a pointer to the array of characters.
Reply:use inheritanceand make sure that the class is public


No comments:

Post a Comment