Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   comparing pointed-at value with another value (http://www.velocityreviews.com/forums/t315713-comparing-pointed-at-value-with-another-value.html)

Pierre McCann 10-10-2003 06:53 PM

comparing pointed-at value with another value
 
Hello:

I am writing the following program in C, but I do most of my coding in C++,
and am not intimately familiar with the C notation for pointers. As such,
I am trying to do a comparison between the contents of a pointer and some
hard-coded bounds on what the contents can be.

The warning being returned during compile-time is:
fre.c:47: warning: comparison between pointer and integer

Note: line 47 is
if(((int*) bufferPtr < 0) || ((int*) bufferPtr > 1999))

Any help on this woul be muchly appreciated.

regards,

pdm
======================================

#include <stdlib.h>
#include <stdio.h>

typedef struct{
int which, count, sz, qq;
char name[32];
float vec1[100], vec2[100];
int image[30][30];
} mystruct;

int main(int argc, char** argv)
{
FILE* data;
int recFirst, recLast, recCurrent;
int numSuccess = 0;
int direction;
mystruct *bufferPtr;

data = fopen("data2000", "rb+");
bufferPtr = (mystruct*) malloc (1);
// printf("size of bufferPtr: %d", sizeof(*bufferPtr));
printf("size of int: %d\n", sizeof(int));

printf("First record (1-2000)? ");
scanf("%d", &recFirst);
printf("Last record (1-2000)? ");
scanf("%d", &recLast);
// printf("recFirst: %d\trecLast: %d\n", recFirst, recLast);

if(recFirst > recLast)
direction = -1;
else
direction = 1;
recCurrent = recFirst;

while(recCurrent != recLast)
{
printf("1\n");
fseek(data, recCurrent * 4448, SEEK_SET);
printf("2\n");
fread(bufferPtr, 1, 4448, data);
printf("3\n");
// printf("Contents of bufferPtr: %d\n", (int*) *bufferPtr);

if(((int*) bufferPtr < 0) || ((int*) bufferPtr > 1999))
printf("Invalid record index\n");
else
numSuccess++;
printf("4\n");
}

return 0;
}



Tristan Miller 10-10-2003 07:10 PM

Re: comparing pointed-at value with another value
 
Greetings.

In article <WeDhb.8948$XS4.7783@edtnps84>, Pierre McCann wrote:
> I am writing the following program in C, but I do most of my coding in
> C++,
> and am not intimately familiar with the C notation for pointers.


It's much the same as in C++, actually. The code you post below is
problematic in both C and C++.

> The warning being returned during compile-time is:
> fre.c:47: warning: comparison between pointer and integer
>
> Note: line 47 is
> if(((int*) bufferPtr < 0) || ((int*) bufferPtr > 1999))


You are not derefencing bufferPtr, but rather casting it. (int *) is a
casting operator. In the first half of the expression, you are telling the
compiler, "I have some variable called bufferPtr. Please pretend that it's
a pointer-to-int, and then compare it with the integer value 0." Therewith
lies your error: you cannot compare pointers and integers.

You probably meant to write

if(*bufferPtr < 0 || *bufferPtr > 1999)

thus dereferencing bufferPtr.

Regards,
Tristan

--
_
_V.-o Tristan Miller [en,(fr,de,ia)] >< Space is limited
/ |`-' -=-=-=-=-=-=-=-=-=-=-=-=-=-=-= <> In a haiku, so it's hard
(7_\\ http://www.nothingisreal.com/ >< To finish what you

Mark Gordon 10-10-2003 07:28 PM

Re: comparing pointed-at value with another value
 
On Fri, 10 Oct 2003 18:53:42 GMT
"Pierre McCann" <mccannp@telus.net> wrote:

<snip>

> #include <stdlib.h>
> #include <stdio.h>
>
> typedef struct{
> int which, count, sz, qq;
> char name[32];
> float vec1[100], vec2[100];
> int image[30][30];
> } mystruct;
>
> int main(int argc, char** argv)
> {
> FILE* data;
> int recFirst, recLast, recCurrent;
> int numSuccess = 0;
> int direction;
> mystruct *bufferPtr;
>
> data = fopen("data2000", "rb+");
> bufferPtr = (mystruct*) malloc (1);

Don't cast the return value of malloc in C as it would hide a failure to
include stdlib.h

You've only allocated 1 byte, not enough space for one instance of the
structure.

Use:
bufferPtr = malloc(sizeof *bufferPts);

> // printf("size of bufferPtr: %d", sizeof(*bufferPtr));
> printf("size of int: %d\n", sizeof(int));
>
> printf("First record (1-2000)? ");

fflush(stdout);
Otherwise the prompt may not be displayed before the input.

> scanf("%d", &recFirst);
> printf("Last record (1-2000)? ");


fflush(stdout);

> scanf("%d", &recLast);
> // printf("recFirst: %d\trecLast: %d\n", recFirst, recLast);
>
> if(recFirst > recLast)
> direction = -1;
> else
> direction = 1;
> recCurrent = recFirst;
>
> while(recCurrent != recLast)
> {
> printf("1\n");
> fseek(data, recCurrent * 4448, SEEK_SET);
> printf("2\n");
> fread(bufferPtr, 1, 4448, data);
> printf("3\n");
> // printf("Contents of bufferPtr: %d\n", (int*)
> *bufferPtr);
>
> if(((int*) bufferPtr < 0) || ((int*) bufferPtr > 1999))


I think you mean
if((*bufferPtr < 0) || (*bufferPtr > 1999))

> printf("Invalid record index\n");
> else
> numSuccess++;
> printf("4\n");
> }
>
> return 0;
> }


You probably need a good C reference. Try the FAQ for this group and
K&R2 (see FAQ).
--
Mark Gordon
Paid to be a Geek & a Senior Software Developer
Although my email address says spamtrap, it is real and I read it.

Eric Sosman 10-10-2003 07:59 PM

Re: comparing pointed-at value with another value
 
Tristan Miller wrote:
>
> Greetings.
>
> In article <WeDhb.8948$XS4.7783@edtnps84>, Pierre McCann wrote:
> > I am writing the following program in C, but I do most of my coding in
> > C++,
> > and am not intimately familiar with the C notation for pointers.

>
> It's much the same as in C++, actually. The code you post below is
> problematic in both C and C++.
>
> > The warning being returned during compile-time is:
> > fre.c:47: warning: comparison between pointer and integer
> >
> > Note: line 47 is
> > if(((int*) bufferPtr < 0) || ((int*) bufferPtr > 1999))

>
> You are not derefencing bufferPtr, but rather casting it. (int *) is a
> casting operator. In the first half of the expression, you are telling the
> compiler, "I have some variable called bufferPtr. Please pretend that it's
> a pointer-to-int, and then compare it with the integer value 0." [...]


Not "pretend that it's," but "convert it to." There's no
pretense involved, even though people seem to think so when
pointers lurk about the premises. I've never heard anybody
use words like "pretend" to describe `(int)(celsius * 1.8) + 32';
it's clear that a conversion from `double' to `int' takes place,
not some kind of magical sleight-of-hand between the two types.
The conversion of one type of pointer to another type of pointer
(when it makes sense to do so) is similarly, er, unpretentious.

--
Eric.Sosman@sun.com

Arthur J. O'Dwyer 10-10-2003 08:07 PM

Re: comparing pointed-at value with another value
 

On Fri, 10 Oct 2003, Eric Sosman wrote:
>
> Tristan Miller wrote:
> > In article <WeDhb.8948$XS4.7783@edtnps84>, Pierre McCann wrote:
> > >
> > > Note: line 47 is
> > > if(((int*) bufferPtr < 0) || ((int*) bufferPtr > 1999))

> >
> > You are not derefencing bufferPtr, but rather casting it. (int *) is a
> > casting operator. In the first half of the expression, you are telling the
> > compiler, "I have some variable called bufferPtr. Please pretend that it's
> > a pointer-to-int, and then compare it with the integer value 0." [...]

>
> Not "pretend that it's," but "convert it to." There's no
> pretense involved, even though people seem to think so when
> pointers lurk about the premises. I've never heard anybody
> use words like "pretend" to describe `(int)(celsius * 1.8) + 32';
> it's clear that a conversion from `double' to `int' takes place,
> not some kind of magical sleight-of-hand between the two types.
> The conversion of one type of pointer to another type of pointer
> (when it makes sense to do so) is similarly, er, unpretentious.


Yes. I would only consistently use the word "pretend" when you're
engaging in "type punning," e.g.,

*(int *)&foo = 42;
"Pretend that 'foo' is actually an 'int'..."

So above, it'd be

"Convert 'bufferPtr' to a pointer-to-int..."
or "Pretend that 'bufferPtr' points to an 'int'..."

....because even though most pointers-to-int do point to ints,
'(int *)bufferPtr' might not. However, it *is* quite definitely
a pointer-to-int -- no pretense there!

-Arthur

John Bode 10-10-2003 10:33 PM

Re: comparing pointed-at value with another value
 
"Pierre McCann" <mccannp@telus.net> wrote in message news:<WeDhb.8948$XS4.7783@edtnps84>...
> Hello:
>
> I am writing the following program in C, but I do most of my coding in C++,
> and am not intimately familiar with the C notation for pointers. As such,
> I am trying to do a comparison between the contents of a pointer and some
> hard-coded bounds on what the contents can be.
>
> The warning being returned during compile-time is:
> fre.c:47: warning: comparison between pointer and integer
>
> Note: line 47 is
> if(((int*) bufferPtr < 0) || ((int*) bufferPtr > 1999))
>
> Any help on this woul be muchly appreciated.


Well, you have several problems.

>
> regards,
>
> pdm
> ======================================
>
> #include <stdlib.h>
> #include <stdio.h>
>
> typedef struct{
> int which, count, sz, qq;
> char name[32];
> float vec1[100], vec2[100];
> int image[30][30];
> } mystruct;
>
> int main(int argc, char** argv)
> {
> FILE* data;
> int recFirst, recLast, recCurrent;
> int numSuccess = 0;
> int direction;
> mystruct *bufferPtr;
>
> data = fopen("data2000", "rb+");
> bufferPtr = (mystruct*) malloc (1);


First issue: you're only allocating 1 byte for your buffer. This is
probably not what you intended. Second issue: casting the return
value of malloc() is frowned upon in C (I realize it's required in
C++).

bufferPtr = malloc (sizeof *bufferPtr);

will allocate memory for 1 object of type mystruct (type of bufferPtr
is pointer to mystruct; type of *bufferPtr is mystruct).

> // printf("size of bufferPtr: %d", sizeof(*bufferPtr));
> printf("size of int: %d\n", sizeof(int));
>
> printf("First record (1-2000)? ");
> scanf("%d", &recFirst);
> printf("Last record (1-2000)? ");
> scanf("%d", &recLast);
> // printf("recFirst: %d\trecLast: %d\n", recFirst, recLast);
>
> if(recFirst > recLast)
> direction = -1;
> else
> direction = 1;
> recCurrent = recFirst;
>
> while(recCurrent != recLast)
> {
> printf("1\n");
> fseek(data, recCurrent * 4448, SEEK_SET);


You might not want to use a magic number like that. Use sizeof
*bufferPtr instead.

> printf("2\n");
> fread(bufferPtr, 1, 4448, data);


I don't think this causes any problems, but you have your count and
size parameters backwards:

fread (bufferPtr, sizeof *bufferPtr, 1, data);

says "read 1 element of size sizeof *bufferPtr from stream data and
write to bufferPtr".

You'll also want to check the return value from fread in case you've
hit EOF.

> printf("3\n");
> // printf("Contents of bufferPtr: %d\n", (int*) *bufferPtr);
>
> if(((int*) bufferPtr < 0) || ((int*) bufferPtr > 1999))


And now we get to the meat of the matter. In all honesty, I have no
idea what you are attempting to check for here. What you are doing is
casting bufferPtr to type pointer to int, then attempting to compare
that pointer value against int constants (hence the warning).

If you're trying to compare the value of the first member of your
struct (which) against your bounds, then you can do either

if (bufferPtr->which < 0 || bufferPtr->which > 1999) /* The Right
Way */

or

/*
** cast bufferPtr to type int *, then dereference the pointer
*/
if (*((int *)bufferPtr) < 0 || *((int *)bufferPtr) > 1999)

If you're trying to check something else, then you'll need to give us
more information.

> printf("Invalid record index\n");
> else
> numSuccess++;
> printf("4\n");
> }
>
> return 0;
> }


Mark McIntyre 10-10-2003 10:56 PM

Re: comparing pointed-at value with another value
 
On Fri, 10 Oct 2003 18:53:42 GMT, in comp.lang.c , "Pierre McCann"
<mccannp@telus.net> wrote:

>I am writing the following program in C, but I do most of my coding in C++,
>and am not intimately familiar with the C notation for pointers.


The pointer notation is EXACTLY the same. Reading this code, I'd say
you'd have the same confusion if you were trying to do this in C++.

>I am trying to do a comparison between the contents of a pointer


You want to compare the value of an element of the struct that the
pointer points to, I think.

> if(((int*) bufferPtr < 0) || ((int*) bufferPtr > 1999))


assuming bufferPtr is a pointer, then you're casting it to a pointer
to int, and then comparing it to zero or 1999. Thats a "comparison of
pointer to int" so your compiler warning seems appropriate.

Do you really want to compare whats *inside* bufferPtr? Then refer to
the right element. This is just like C++ by the way, whats so
confusing?
if (bufferPtr.which < 0)

to examine the value of which etc.


--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>


----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---


All times are GMT. The time now is 11:36 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.