Advertisement

C\C++ question

Started by July 11, 2000 12:42 AM
3 comments, last by Fender148 24 years, 5 months ago
Im a little confused of the difference between two things like this (int *)hello(x,y) isnt (int *) mean a pointer and (int)hello(x,y) I have no idea what this is, the (int) in front Thanks
(int) and (long) and (float) and (char), etc. typecast the value which follows it to the type in the ().

For example:

char p;

p = 25;

printf ("the numner of p = %d\n", (int) p);

p has been casted as an int.


Another example:

float b = 3.4;
float c;

c = (int) b;

b is cast as an int, reducing it to 3 as opposed to 3.4 and then assigned to c. Therefore, c receives the value of 3.

What is (char *), (int *) or (float *)?
We are typecasting the value that follows as a pointer of the type in the (). Don''t do this unless the assigned value is also a pointer!






_______________________________
"To understand the horse you'll find that you're going to be working on yourself. The horse will give you the answers and he will question you to see if you are sure or not."
- Ray Hunt, in Think Harmony With Horses
ALU - SHRDLU - WORDNET - CYC - SWALE - AM - CD - J.M. - K.S. | CAA - BCHA - AQHA - APHA - R.H. - T.D. | 395 - SPS - GORDIE - SCMA - R.M. - G.R. - V.C. - C.F.
Advertisement
They are called "casts". They change the "type" of what is to the right of them; that is, as long as it''s legal to do so.

In the code you wrote, I assume that hello(x,y) is returning some sort of value. Placing "(int)" in front of it is telling the C/C++ compiler that you want to "cast" the return value into an integer. Ie- Treat the return value as an integer.

The same with "(int*)". You''re telling the compiler to cast the value as a pointer to an integer.

Casts are generally used to mold values into other values.

For example, if hello(x,y) was defined like this:

DWORD hello(WORD x, WORD y)
{
return((x + y) >> 1);
}

Then the return value is a DWORD (a 32-bit unsigned integer.)

But if you write the following line of code to save the return value in an integer, you will get (at least) a warning from the compiler:

int h = hello(1,2);

But you WANT the return value to be treated as an ''int'', not a DWORD.. so you can ''cast'' it:

int h = (int) hello(1,2);

Similarly for "(int*)". In fact, casts are used all the time with operators like ''new''. ''new'' returns a "void *" which you need to cast if you''re going to save it in anything but a void* type.

So you would declare a pointer to an ''int'' array like this:

int* pIntArray = (int*) new int[32];

If you don''t cast it to (int *) then the compiler will certainly give you an error.

Hope that helps!
// CHRIS
// CHRIS [win32mfc]
quote:
So you would declare a pointer to an 'int' array like this:
int* pIntArray = (int*) new int[32];
If you don't cast it to (int *) then the compiler will certainly give you an error.


Actually, it won't give you an error. I think you've got new mixed up with malloc/calloc. Those functions return a void pointer, and C++ requires an explicit cast for those, but C++ doesn't require a cast for new.

- Muzzafarath

Mad House Software
The Field Marshals

Edited by - Muzzafarath on July 11, 2000 8:35:23 AM
I'm reminded of the day my daughter came in, looked over my shoulder at some Perl 4 code, and said, "What is that, swearing?" - Larry Wall
Umm.. Muzzafarath is right on that. Replace the ''new'' in my example with the malloc() function. "new" doesn''t need a type-case if the type''s are already compatible.

That''s what I get for trying to think at nearly 3am.

// CHRIS
// CHRIS [win32mfc]

This topic is closed to new replies.

Advertisement