By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,842 Members | 2,186 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,842 IT Pros & Developers. It's quick & easy.

What does this function return!

P: n/a
#define UWORD2 unsigned short int
typedef BYTE Boolean;
Boolean evaluateBit(UWORD2 *value, int bitnumber);
Boolean evaluateBit(UWORD2 *value, int bitnumber) {
int one=1;
return (one &(*value >bitnumber));
}

When I call the function like this:

#include <stdlib.h>
#define UWORD2 unsigned short int
typedef BYTE Boolean;
Boolean evaluateBit(UWORD2 *value, int bitnumber);
int main(void) {
Boolean island;
int row=15;
int col=15;
int i,j;
float qual[row][col];
/* another function assigns values to qual and at the same time casts
them to float */
/* below the float values are recasted to unsigned char before being
sent to the function */
for (i=0;i<col;i++){
for (j=0;j<row;j++){
island = evaluateBit((unsigned char)&qual[i][j],4);
}
}
return (1)
}

Boolean evaluateBit(UWORD2 *value, int bitnumber) {
int one=1;
return (one &(*value >bitnumber));
}

I get the following warning:
"warning: passing arg 1 of `evaluateBit' makes pointer from integer
without a cast"

I don't understand the return statement and why this warning occurs.

Thanks in advance,
Sheldon

Nov 5 '06 #1
Share this Question
Share on Google+
12 Replies


P: n/a
"Sheldon" <sh******@gmail.comwrites:

Please, use indention.
#include <stdlib.h>
#include <limit.h>
#define UWORD2 unsigned short int
typedef BYTE Boolean;
BYTE is undefined
Boolean evaluateBit(UWORD2 *value, int bitnumber);
char evaluateBit(unsigned char *value, unsigned bitnumber) {
int main(void) {
Boolean island;
int row = 15, col = 15;
int i, j;
float qual[row][col];
/* another function assigns values to qual and at the same time
casts them to float */
/* below the float values are recasted to unsigned char before
being sent to the function */
for (i = 0; i<col; i++) {
for (j = 0; j<row; j++) {
island = evaluateBit((unsigned char)&qual[i][j],4);
evaluateBit expects a pointer to unsigned short instead it gets
an unsigned char value. What you want is:

island = evaluateBit((unsigned short*)&qual[i][j], 4);

but still there may be some problems with that. You should rather use:

island = evaluateBit((unsigned char*)&qual[i][j], 4);

and change evaluateBit function.

BTW. I do hope you intend to do something with island.
}
}
return 1
Are you sure you want to return 1?
}

Boolean evaluateBit(UWORD2 *value, int bitnumber) {
int one = 1;
return (one &(*value >bitnumber));
}
Try this one:

char evaluateBit(unsigned char *value, unsigned bitnumber) {
return (value[bitnumber / CHAR_BIT] >(bitnumber % CHAR_BIT)) & 1;
}
I get the following warning:
"warning: passing arg 1 of `evaluateBit' makes pointer from integer
without a cast"

I don't understand the return statement
Which return statement?
and why this warning occurs.
I hope I've explained that.

--
Best regards, _ _
.o. | Liege of Serenly Enlightened Majesty of o' \,=./ `o
..o | Computer Science, Michal "mina86" Nazarewicz (o o)
ooo +--<mina86*tlen.pl>---<jid:mina86*chrome.pl>--ooO--(_)--Ooo--
Nov 5 '06 #2

P: n/a
In article <11**********************@b28g2000cwb.googlegroups .com>,
Sheldon <sh******@gmail.comwrote:

>island = evaluateBit((unsigned char)&qual[i][j],4);
>Boolean evaluateBit(UWORD2 *value, int bitnumber) {
>I get the following warning:
"warning: passing arg 1 of `evaluateBit' makes pointer from integer
without a cast"
>I don't understand the return statement and why this warning occurs.
Notice the island = assignment where you call evaluateBit.
You take &qual[i][j] and you cast that to (unsigned char).
An unsigned char is not a pointer, but evaluateBit needs a pointer
for its first argument.

Possibly you wanted to cast to (unsigned char*)

--
I was very young in those days, but I was also rather dim.
-- Christopher Priest
Nov 5 '06 #3

P: n/a
Sheldon <sh******@gmail.comwrote:
/* below the float values are recasted to unsigned char before being
sent to the function */
for (i=0;i<col;i++){
for (j=0;j<row;j++){
island = evaluateBit((unsigned char)&qual[i][j],4);
Contrary to what your comment says you don't cast the float value,
you cast its address. And then you pass the cast address (which has
now a type of unsigned int) to a function that expects an address
as its first argument, exactly as the compiler tells you with
"warning: passing arg 1 of `evaluateBit' makes pointer from integer
without a cast"
Since you can't take the address of of an expression (i.e. the
'(unsigned int) qual[i][j]' you actually seem to want to use),
you need an extra variable to store the result of the cast and
then pass its address to your function:

for (i = 0; i < col; i++) {
for (j = 0; j < row; j++) {
unsigned int val = (unsigned int) qual[i][j];
island = evaluateBit(&val, 4);
}
}

Of course, it could get simpler if you would just change your
function to take an unsigned int as its first argument instead
of an address...
Regards, Jens
--
\ Jens Thoms Toerring ___ jt@toerring.de
\__________________________ http://toerring.de
Nov 5 '06 #4

P: n/a

Michal Nazarewicz skrev:
"Sheldon" <sh******@gmail.comwrites:

Please, use indention.
#include <stdlib.h>

#include <limit.h>
#define UWORD2 unsigned short int
typedef BYTE Boolean;

BYTE is undefined
Boolean evaluateBit(UWORD2 *value, int bitnumber);

char evaluateBit(unsigned char *value, unsigned bitnumber) {
int main(void) {
Boolean island;
int row = 15, col = 15;
int i, j;
float qual[row][col];
/* another function assigns values to qual and at the same time
casts them to float */
/* below the float values are recasted to unsigned char before
being sent to the function */
for (i = 0; i<col; i++) {
for (j = 0; j<row; j++) {
island = evaluateBit((unsigned char)&qual[i][j],4);

evaluateBit expects a pointer to unsigned short instead it gets
an unsigned char value. What you want is:

island = evaluateBit((unsigned short*)&qual[i][j], 4);

but still there may be some problems with that. You should rather use:

island = evaluateBit((unsigned char*)&qual[i][j], 4);

and change evaluateBit function.

BTW. I do hope you intend to do something with island.
}
}
return 1

Are you sure you want to return 1?
}

Boolean evaluateBit(UWORD2 *value, int bitnumber) {
int one = 1;
return (one &(*value >bitnumber));
}

Try this one:

char evaluateBit(unsigned char *value, unsigned bitnumber) {
return (value[bitnumber / CHAR_BIT] >(bitnumber % CHAR_BIT)) & 1;
}
I get the following warning:
"warning: passing arg 1 of `evaluateBit' makes pointer from integer
without a cast"

I don't understand the return statement

Which return statement?
and why this warning occurs.

I hope I've explained that.

--
Best regards, _ _
.o. | Liege of Serenly Enlightened Majesty of o' \,=./ `o
..o | Computer Science, Michal "mina86" Nazarewicz (o o)
ooo +--<mina86*tlen.pl>---<jid:mina86*chrome.pl>--ooO--(_)--Ooo--
Thanks for your advice.
I forgot to include that BYTE is defined as unsigned char.
The return statement: return (one &(*value >bitnumber)); is cryptic.
Can you explain what it means?

/S

Nov 5 '06 #5

P: n/a

Walter Roberson skrev:
In article <11**********************@b28g2000cwb.googlegroups .com>,
Sheldon <sh******@gmail.comwrote:

island = evaluateBit((unsigned char)&qual[i][j],4);
Boolean evaluateBit(UWORD2 *value, int bitnumber) {
I get the following warning:
"warning: passing arg 1 of `evaluateBit' makes pointer from integer
without a cast"
I don't understand the return statement and why this warning occurs.

Notice the island = assignment where you call evaluateBit.
You take &qual[i][j] and you cast that to (unsigned char).
An unsigned char is not a pointer, but evaluateBit needs a pointer
for its first argument.

Possibly you wanted to cast to (unsigned char*)

--
I was very young in those days, but I was also rather dim.
-- Christopher Priest
I see! Thanks!

Nov 5 '06 #6

P: n/a

Jens Thoms Toerring skrev:
Sheldon <sh******@gmail.comwrote:
/* below the float values are recasted to unsigned char before being
sent to the function */
for (i=0;i<col;i++){
for (j=0;j<row;j++){
island = evaluateBit((unsigned char)&qual[i][j],4);

Contrary to what your comment says you don't cast the float value,
you cast its address. And then you pass the cast address (which has
now a type of unsigned int) to a function that expects an address
as its first argument, exactly as the compiler tells you with
"warning: passing arg 1 of `evaluateBit' makes pointer from integer
without a cast"

Since you can't take the address of of an expression (i.e. the
'(unsigned int) qual[i][j]' you actually seem to want to use),
you need an extra variable to store the result of the cast and
then pass its address to your function:

for (i = 0; i < col; i++) {
for (j = 0; j < row; j++) {
unsigned int val = (unsigned int) qual[i][j];
island = evaluateBit(&val, 4);
}
}

Of course, it could get simpler if you would just change your
function to take an unsigned int as its first argument instead
of an address...
Regards, Jens
--
\ Jens Thoms Toerring ___ jt@toerring.de
\__________________________ http://toerring.de

This is a good idea. I will take a look at this.
Thanks!

/S

Nov 5 '06 #7

P: n/a

Sheldon skrev:
#define UWORD2 unsigned short int
typedef BYTE Boolean;
Boolean evaluateBit(UWORD2 *value, int bitnumber);
Boolean evaluateBit(UWORD2 *value, int bitnumber) {
int one=1;
return (one &(*value >bitnumber));
}

When I call the function like this:

#include <stdlib.h>
#define UWORD2 unsigned short int
typedef BYTE Boolean;
Boolean evaluateBit(UWORD2 *value, int bitnumber);
int main(void) {
Boolean island;
int row=15;
int col=15;
int i,j;
float qual[row][col];
/* another function assigns values to qual and at the same time casts
them to float */
/* below the float values are recasted to unsigned char before being
sent to the function */
for (i=0;i<col;i++){
for (j=0;j<row;j++){
island = evaluateBit((unsigned char)&qual[i][j],4);
}
}
return (1)
}

Boolean evaluateBit(UWORD2 *value, int bitnumber) {
int one=1;
return (one &(*value >bitnumber));
}

I get the following warning:
"warning: passing arg 1 of `evaluateBit' makes pointer from integer
without a cast"

I don't understand the return statement and why this warning occurs.

Thanks in advance,
Sheldon
Dear all,

Your advice solved the problem and taught me more about pointers and
casting.
Much obliged!

/Sheldon

Nov 5 '06 #8

P: n/a
Sheldon wrote:
Michal Nazarewicz skrev:
"Sheldon" <sh******@gmail.comwrites:
<snip>
#include <limit.h>
#define UWORD2 unsigned short int
typedef BYTE Boolean;
<snip>
Boolean evaluateBit(UWORD2 *value, int bitnumber) {
int one = 1;
return (one &(*value >bitnumber));
}
<snip>
I forgot to include that BYTE is defined as unsigned char.
The return statement: return (one &(*value >bitnumber)); is cryptic.
Can you explain what it means?
as C goes it's not that cryptic. Get a good text book, eg. K&R "The C
Programming Language".

"*value" means dereference "value". That is return the value pointed to

by the pointer "value". ">>" is the right shift operator. So take the
value
extracted from "value" and right shift it bitnumber times. "&" is
bitwise
and so it ands the value from the last bit with 1. That it is only the
least
significant bit is left.

some examples:-
in bitnum out
1011 0 1
1011 1 1
1011 2 0
1011 3 1
--
Nick Keighley

Nov 5 '06 #9

P: n/a
Sheldon <sh******@gmail.comwrote:
I forgot to include that BYTE is defined as unsigned char.
The return statement: return (one &(*value >bitnumber)); is cryptic.
Can you explain what it means?
Boolean evaluateBit(UWORD2 *value, int bitnumber) {
int one = 1;
return (one &(*value >bitnumber));
}
'Boolean' is also not defined;-) But what the function does is rather
simple: '*value' is supposed to be an unsigned interger. This value
is now right-shifted(*) 'bitnumber' times and the result is logically
and'ed with 1, i.e. resulting in 1 if the lowest bit after the right-
shift is set and 0 otherwise. If you call the function with 'bitnumber'
set to 0 it will return if the lowest bit of the value is set. If you
call it with 'bitnumber' set to 1 it tells you if the second-lowest bit
of the value is set. So, if you number the bits in ascending order, i.e.
0 is the lowest, 1 is the second lowest etc., the function tells you
if the bit of the value the function receives a pointer to and indexed
this way by 'bitnumber' is set.

Of course, there's no real good reason why the variable 'one' is
needed at all (and it actually should also be an unsigned int), you
could write the function as e.g.

unsigned int evaluateBit(unsigned int *value, int bitnumber) {
return ( *value >bitnumber ) & 1U;
}

Another way to write it would be

unsigned int evaluateBit(unsigned int *value, int bitnumber) {
return *value & ( 1U << bitnumber ) ? 1 : 0;
}

and there are probably countless other ways to do it.

Regards, Jens

(*) If you never heard about the "shifting thing": imagine the number
in binary notation. If you have 9 in decimal it's 1001 in binary.
When you now right-shift this by one position, you get 0100 (the
lowest bit gets thrown away), i.e. te result is 4. If you would
right-shift it by 2 positions you get 0010, i.e. 2 in decimal. You
can also left-shift, so if you again have 9, i.e. 1001 in binary,
then left-shifting it by one position gives you 10010, i.e. 18
in decimal - the lowest bit gets filled with a 0 bit.
--
\ Jens Thoms Toerring ___ jt@toerring.de
\__________________________ http://toerring.de
Nov 5 '06 #10

P: n/a

Jens Thoms Toerring skrev:
Sheldon <sh******@gmail.comwrote:
I forgot to include that BYTE is defined as unsigned char.
The return statement: return (one &(*value >bitnumber)); is cryptic.
Can you explain what it means?
Boolean evaluateBit(UWORD2 *value, int bitnumber) {
int one = 1;
return (one &(*value >bitnumber));
}

'Boolean' is also not defined;-) But what the function does is rather
simple: '*value' is supposed to be an unsigned interger. This value
is now right-shifted(*) 'bitnumber' times and the result is logically
and'ed with 1, i.e. resulting in 1 if the lowest bit after the right-
shift is set and 0 otherwise. If you call the function with 'bitnumber'
set to 0 it will return if the lowest bit of the value is set. If you
call it with 'bitnumber' set to 1 it tells you if the second-lowest bit
of the value is set. So, if you number the bits in ascending order, i.e.
0 is the lowest, 1 is the second lowest etc., the function tells you
if the bit of the value the function receives a pointer to and indexed
this way by 'bitnumber' is set.

Of course, there's no real good reason why the variable 'one' is
needed at all (and it actually should also be an unsigned int), you
could write the function as e.g.

unsigned int evaluateBit(unsigned int *value, int bitnumber) {
return ( *value >bitnumber ) & 1U;
}

Another way to write it would be

unsigned int evaluateBit(unsigned int *value, int bitnumber) {
return *value & ( 1U << bitnumber ) ? 1 : 0;
}

and there are probably countless other ways to do it.

Regards, Jens

(*) If you never heard about the "shifting thing": imagine the number
in binary notation. If you have 9 in decimal it's 1001 in binary.
When you now right-shift this by one position, you get 0100 (the
lowest bit gets thrown away), i.e. te result is 4. If you would
right-shift it by 2 positions you get 0010, i.e. 2 in decimal. You
can also left-shift, so if you again have 9, i.e. 1001 in binary,
then left-shifting it by one position gives you 10010, i.e. 18
in decimal - the lowest bit gets filled with a 0 bit.
--
\ Jens Thoms Toerring ___ jt@toerring.de
\__________________________ http://toerring.de
Thanks. I think I understand it now. I will study your text some more
later today.

Nice explanation!

/Sheldon

Nov 5 '06 #11

P: n/a

Nick Keighley skrev:
Sheldon wrote:
Michal Nazarewicz skrev:
"Sheldon" <sh******@gmail.comwrites:

<snip>
#include <limit.h>
>
#define UWORD2 unsigned short int
typedef BYTE Boolean;

<snip>
Boolean evaluateBit(UWORD2 *value, int bitnumber) {
int one = 1;
return (one &(*value >bitnumber));
}

<snip>
I forgot to include that BYTE is defined as unsigned char.
The return statement: return (one &(*value >bitnumber)); is cryptic.
Can you explain what it means?

as C goes it's not that cryptic. Get a good text book, eg. K&R "The C
Programming Language".

"*value" means dereference "value". That is return the value pointed to

by the pointer "value". ">>" is the right shift operator. So take the
value
extracted from "value" and right shift it bitnumber times. "&" is
bitwise
and so it ands the value from the last bit with 1. That it is only the
least
significant bit is left.

some examples:-
in bitnum out
1011 0 1
1011 1 1
1011 2 0
1011 3 1
--
Nick Keighley
I am getting a book ASAP but this task is rather pressing and I forced
to use someone else's code. Thanks for the explanation. I understand
now what the "&" means in this case. I was confusing it with the & used
in pointers.

Thanks,
/Sheldon

Nov 5 '06 #12

P: n/a
Jens Thoms Toerring <jt@toerring.dewrote:
'Boolean' is also not defined;-) But what the function does is rather
simple: '*value' is supposed to be an unsigned interger. This value
is now right-shifted(*) 'bitnumber' times and the result is logically
and'ed with 1, i.e. resulting in 1 if the lowest bit after the right-
Oops, please read that as "bitwise and'ed", the logical and operator
is two ampersands. Using a logical and you get 1 if both operands are
non-zero while with the bitwise and the result has a bit set for
the corresponding bits that were set in both the operators, so e.g.
'9 && 4' is 1, while '9 & 4' is 0 but '9 & 12' is 8.

Regards, Jens
--
\ Jens Thoms Toerring ___ jt@toerring.de
\__________________________ http://toerring.de
Nov 5 '06 #13

This discussion thread is closed

Replies have been disabled for this discussion.