SD> #include
SD> main()
SD> {
SD> unsigned char Monster_Name;
SD> Monster_Name = "Bear";
SD> clrscr();
SD> printf("%s", Monster_Name);
SD> getch();
SD> return(0);
SD> }
SD> The warning I get is this :
SD> Warning Nonportable pointer conversion in function main
sd>....
You are specifying a variable of type char, and then
attempting to assign a string to it, and then attempting to use
it as a string, or a pointer to type char, in your printf().
You will need to use a pointer to type char, or create an array
of type char, which is handled as a pointer when only the
variable name, with no referencing, is given. Simple assignment
is not used with an array of type char, since each element of
the array must receive the value of the string being assigned
to that array, where a pointer just takes the address of the
constant data within the program. It may be argued that a const
char pointer should be used to prevent accidental writing to
the constant data within the program, and this will become
increasingly important when OOP is considered, wherein the
principles of data protection and encapsulation are applied.
#include
#include
#include
int main(void)
{
unsigned char *Monster_Name; /* Monster_Name is a pointer */
unsigned char Monster_Two[16]; /* Monster_Two is a char array */
unsigned char Monster_Char; /* Monster_Char is a char */
Monster_Name = "Bear";
strcpy(Monster_Two, "Boar");
Monster_Char = 'M';
clrscr();
printf("%s\n%s\n%c\n",
Monster_Name, Monster_Two, Monster_Char);
getch();
return 0;
}
In a C++ implementation, you might use an overloaded assignment
in a Monster class to give it its value.
#include
#include
class Monster {
public:
Monster(char*);
const char *what(void) { return m_name; }
void operator =(char *N) { m_name = N; };
private:
char *m_name;
};
Monster::Monster(char *M = "VOID")
{ m_name = M; }
int main(void)
{
Monster M_one("Bear"), M_two("Boar"), M_three;
cout << M_one.what() << '\n'
<< M_two.what() << '\n'
<< M_three.what() << '\n'
<< flush;
M_three = "Spam";
cout << "ooPs! I meant " << M_three.what() << endl;
getch();
return 0;
}
> ] I'm not materialistic. I'm just Object Oriented.............
---
---------------
* Origin: *YOPS ]I[* 3.1 GIG * RA/FD/FE RADist * Milwaukee, WI (1:154/750)
|