comparing pointed-at value with another value

P

Pierre McCann

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;
}
 
T

Tristan Miller

Greetings.

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
 
M

Mark Gordon

On Fri, 10 Oct 2003 18:53:42 GMT

#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 said:
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).
 
E

Eric Sosman

Tristan said:
Greetings.

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.
 
A

Arthur J. O'Dwyer

Tristan said:
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
 
J

John Bode

Pierre McCann said:
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.
 
M

Mark McIntyre

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,527
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top