Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Microsoft Visual C++ compiler, cast problem ( I think)?

Status
Not open for further replies.

jonnie75

Technical User
Nov 9, 2001
6
0
0
US
I am a beginner at programming, but I am slowly making progress. I was wondering if someone would be able to help me out with what I believe to be a casting problem. I have a small program that is used to read inputs/outputs on an ISA card (home project kit). The program will compile for the original programmer but he used a different compiler ( I would like to stay with Microsoft Visual C++). I did find the correct way to use Sleep()and also the _inp statement. When I tried to compile this program I get 3 warnings which say "warning C4761: integral size mismatch in argument; conversion supplied". When I highlight the first line of code with a warning "return ((_inp(switch_port) >> (input + 3)) & 1) ^ 1; // yes, return using switch_port" and press the F1 key. I get directed to the "Return Statement" definiton. I thought that trying to change this line to " return ((_inp((int)switch_port) >> (input + 3)) & 1) ^ 1; // yes, return using switch_port " would fix the error but it didn't. Am I way off of course here? Any help with this problem would be greatly appreciated. The entire program is as follows:

[ // experi2.c

#include <stdio.h> // contains prototypes for printf() and puts()
#include <conio.h> // contains prototypes for inp() and kbhit()
#include <windows.h> // contains prototyped for Sleep()

// local prototypes
int is_switch(int input);
void get_port(void);
void set_up_ppi(void);

unsigned int base;
unsigned int switch_port;

void main(void)
{
int x,y;

get_port(); // get the value of the base Port Address

set_up_ppi(); // set up ppi_porta = out, ppi_portb = in, ppi_portc = out

switch_port = base + 0x18;

while(1)
{
if(kbhit()) // &quot;keyboard hit&quot; -- a compiler function that sees if a key has been
break; // pressed. We will break out of the while() loop if a key is pressed.

puts(&quot;&quot;); // puts(&quot;&quot;) will put a new line on the screen
for(x=0; x<7; x++) // 0 through 6 -- purposely use 0 to produce an error
{
y = is_switch(x);
printf(&quot;x=%02d y=%02d|&quot;,x,y); // don't print a new line yet
}
puts(&quot;&quot;); // puts(&quot;&quot;) will put a new line on the screen
// after all 7 items have been printed

for(x=7; x<13; x++) // 7 through 12 -- purposely use 12 to produce an error
{
y = is_switch(x);
printf(&quot;x=%02d y=%02d|&quot;,x,y); // don't print a new line yet
}
puts(&quot;&quot;); // puts(&quot;&quot;) will put a new line on the screen
// after all 6 items have been printed

Sleep(3000); // sleep -- compiler function -- wait one second
// so printed lines can be seen
// remark sleep out or delete it for full speed

} // end while(1)

} // end main()


/*
The following are known only to the functions that follow. They
can't be modified or even accessed by anything above this point.
Their scope is from here to the end of the file.

*/

unsigned ppi_porta;
unsigned ppi_portb;
unsigned ppi_portc;


// ==================================================
==============
// is_switch
// 1. Return -1 error indicator if the input
// is less than 1 or greater than 11.
//
// 2. If there is a fall-through from the above and the input
// is less than 4, return the status based on switch_port.
//
// 3. If there is a fall-through from both of the above, then
// return the status based on ppi_portb.
// ==================================================
==============
int is_switch(int input)
{
if(input < 1 || input > 11) // if the input is less than 1 or greater
return -1; // than 11, then return -1 showing an error

if(input < 4) // else, we fall through the above and see if less than 4
return ((_inp(switch_port) >> (input + 3)) & 1) ^ 1; // yes, return using switch_port


return ((_inp(ppi_portb) >> (input - 4)) & 1) ^ 1; // fell through &quot;if(input < 4)&quot;
// so >= 4 (greater than or equal)
// so use PPI Port B

} // end is_switch()


// Get the port. This will grow into an
// auto-detect function in the future.
void get_port(void)
{
base = 0x300;
switch_port = base + 0x18;
ppi_porta = base + 0x20;
ppi_portb = base + 0x21;
ppi_portc = base + 0x22;
} // end get_port()


// Set up the ppi in mode 0.
// Make Port A an output, Port B an input and Port C an output.
void set_up_ppi(void)
{
unsigned control = base + 0x23;

_outp(control, 0x82); // a = out, b = in, c = out

} // end set_up_ppi()

// end experi2.c ]


 
If you want help on a warning, click the warning in the build tab and then hit F1.

The meaning of the warning is that you are returning a different type than the declared type. For example, this function would produce that warning:

int myfunc()
{
char v;

v = 123;

return v;
}

In this case, I was supposed to return int, but I returned char. When converting from a smaller type to a bigger type, it is completely safe. The warning is telling you that the types don't match, but a &quot;conversion was supplied&quot;, in other words, the compiler converted it to the right type for you.

When converting from a big type to a small type (which you didn't do BTW), will sometimes work, but the following function will not behave as expected:

char myfunc()
{
int myvar = 123456789;

return myvar;
}

See? The value 123456789 will not fit in a char.

The same thing could happen when passing a parameter:

void func2(int x)
{
printf(&quot;Hello! I got %d\n&quot;, x);
}

int func1()
{
char x;

x = 123;
func2(x);
}

In the example I am passing a char to a function that takes an int.

To make that last example compile with no warning, call func2 like this:

func2((int)x);

Doug Gale
 
Thanks for clearing up the confusion. I was under the impression that the program would not compile. It shows 3 warnings (conversion supplied) with this program but it does go ahead and execute. Thank you for your time and most excellent info xDougx. By the way do you do any C++ or Visual C++ programming? Do you know what the major diffrences between the two are? What would I need to do to place several circles on the screen that would change color red/green based on my returned results on/off from the above program? Can graphics like this be easily created in C? How about the use of a mouse? Will C++ and Visual C++ program always run in console window on a Windows OS machine? I want to thank you once again xDougx, you are the man.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top