473,471 Members | 1,696 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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;
}
Nov 13 '05 #1
6 1881
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
Nov 13 '05 #2
On Fri, 10 Oct 2003 18:53:42 GMT
"Pierre McCann" <mc*****@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.
Nov 13 '05 #3
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.

--
Er*********@sun.com
Nov 13 '05 #4

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
Nov 13 '05 #5
"Pierre McCann" <mc*****@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;
}

Nov 13 '05 #6
On Fri, 10 Oct 2003 18:53:42 GMT, in comp.lang.c , "Pierre McCann"
<mc*****@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 =---
Nov 13 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

26
by: William Park | last post by:
How do you compare 2 strings, and determine how much they are "close" to each other? Eg. aqwerty qwertyb are similar to each other, except for first/last char. But, how do I quantify that? ...
5
by: Curtis Gilchrist | last post by:
I am required to read in records from a file and store them in descending order by an customer number, which is a c-style string of length 5. I am storing these records in a linked list. My...
4
by: agent349 | last post by:
First off, I know arrays can't be compared directly (ie: if (arrary1 == array2)). However, I've been trying to compare two arrays using pointers with no success. Basically, I want to take three...
7
by: Thomas Lorenz | last post by:
Hello, a standard template singleton looks basically like this: template<class T> class Singleton1 { private: Singleton1(); static Singleton1* m_Instance; public:
6
by: Erwin | last post by:
Suppose I have a string, char *str = "1 2 a f g < )" and I need to check each of the components in the loop using isalpha, isdigit, isspace. How can I do that? I tried to do using this code, but...
88
by: William Krick | last post by:
I'm currently evaluating two implementations of a case insensitive string comparison function to replace the non-ANSI stricmp(). Both of the implementations below seem to work fine but I'm...
12
by: barcaroller | last post by:
Is it legal to compare the contents of two multi-field variables (of the same struct) using "==" and "!="? struct { int a; int b; } x,y; ...
18
by: william | last post by:
below is a short piece of code I wrote to testify my understanding of char *, and array. #include <stdio.h> int main() { char *str=NULL; char x="today is good!"; printf("%s", str);
27
by: Thomas Kowalski | last post by:
Hi everyone, To determine equality of two doubles a and b the following is often done: bool isEqual ( double a, double b ) { return ( fabs (a-b) < THRESHOLD ); } But this a approach usually...
18
by: Carramba | last post by:
Hi! is there a better/faster way to compare mantissas of to real number then in following code? #include <stdio.h> #include <stdlib.h> int main(void) { float a,b; int test;
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.