472,353 Members | 1,475 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,353 software developers and data experts.

Comparison between "Pointer and Integer"

I'm trying to compare between pointer and integer in an "IF" statement

how do I make this work?

if(patient[index].id != NULL){
}

Thanks
Chris

May 27 '07 #1
20 12112
ch****@gmail.com wrote:
I'm trying to compare between pointer and integer in an "IF" statement

how do I make this work?

if(patient[index].id != NULL){
}
Why do you think its doesn't?

--
Ian Collins.
May 27 '07 #2
"ch****@gmail.com" <ch****@gmail.comwrites:
I'm trying to compare between pointer and integer in an "IF" statement

how do I make this work?

if(patient[index].id != NULL){
}
Why would you want to compare a pointer and an integer?

What is "id"? If it's a pointer, the above should work. If it's an
integer, comparing it to NULL makes no sense.

It's helpful to post a small, complete program that illustrates your
problem. If you're having a compilation program, show us the exact
error message your compiler gives you. Otherise, your sample should
be compilable by itself.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
May 27 '07 #3
On 27 May, 22:45, Keith Thompson <k...@mib.orgwrote:
"chu...@gmail.com" <chu...@gmail.comwrites:
I'm trying to compare between pointer and integer in an "IF" statement
how do I make this work?
if(patient[index].id != NULL){
}

Why would you want to compare a pointer and an integer?

What is "id"? If it's a pointer, the above should work. If it's an
integer, comparing it to NULL makes no sense.

It's helpful to post a small, complete program that illustrates your
problem. If you're having a compilation program, show us the exact
error message your compiler gives you. Otherise, your sample should
be compilable by itself.

--
Keith Thompson (The_Other_Keith) k...@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
ok..

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

#define MAXPATIENTS 20

struct details {
int id;
char forename[20];
char initial;
char surname[20];
int day_of_entry;
int max_wait;
};

//Lists individual patient information -Work in Progress
int list_patient(int index){
int time_left = 0;
int x = 0;
int i;

//Should check if index points to a record,
//Or else it should return -1!!
if( patient[index].id != NULL){
printf("Patient Details: \n");
printf("ID: %d |",patient[index].id);
printf("Name: %s ",patient[index].forename);
printf("%c ",patient[index].initial);
printf("%s |",patient[index].surname);
printf("Days on queue %d |",day_now()- patient[index].day_of_entry);
printf("Maximum waiting days %d \n",time_left =
patient[index].max_wait - (day_now() - patient[i].day_of_entry));
}
else {
x = -1;
}
return x;
}

int main(void){
list_patient(0);
return 0;
}

May 27 '07 #4
On 27 May, 22:45, Keith Thompson <k...@mib.orgwrote:
"chu...@gmail.com" <chu...@gmail.comwrites:
I'm trying to compare between pointer and integer in an "IF" statement
how do I make this work?
if(patient[index].id != NULL){
}

Why would you want to compare a pointer and an integer?

What is "id"? If it's a pointer, the above should work. If it's an
integer, comparing it to NULL makes no sense.

It's helpful to post a small, complete program that illustrates your
problem. If you're having a compilation program, show us the exact
error message your compiler gives you. Otherise, your sample should
be compilable by itself.

--
Keith Thompson (The_Other_Keith) k...@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
ok..

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

#define MAXPATIENTS 20

struct details {
int id;
char forename[20];
char initial;
char surname[20];
int day_of_entry;
int max_wait;
};

//Lists individual patient information -Work in Progress
int list_patient(int index){
int time_left = 0;
int x = 0;
int i;

//Should check if index points to a record,
//Or else it should return -1!!
if( patient[index].id != NULL){
printf("Patient Details: \n");
printf("ID: %d |",patient[index].id);
printf("Name: %s ",patient[index].forename);
printf("%c ",patient[index].initial);
printf("%s |",patient[index].surname);
printf("Days on queue %d |",day_now()- patient[index].day_of_entry);
printf("Maximum waiting days %d \n",time_left =
patient[index].max_wait - (day_now() - patient[i].day_of_entry));
}
else {
x = -1;
}
return x;
}

int main(void){
list_patient(0);
return 0;
}

May 27 '07 #5
On 27 May, 22:45, Keith Thompson <k...@mib.orgwrote:
"chu...@gmail.com" <chu...@gmail.comwrites:
I'm trying to compare between pointer and integer in an "IF" statement
how do I make this work?
if(patient[index].id != NULL){
}

Why would you want to compare a pointer and an integer?

What is "id"? If it's a pointer, the above should work. If it's an
integer, comparing it to NULL makes no sense.

It's helpful to post a small, complete program that illustrates your
problem. If you're having a compilation program, show us the exact
error message your compiler gives you. Otherise, your sample should
be compilable by itself.

--
Keith Thompson (The_Other_Keith) k...@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
ok..

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

#define MAXPATIENTS 20

struct details {
int id;
char forename[20];
char initial;
char surname[20];
int day_of_entry;
int max_wait;
};

//Lists individual patient information -Work in Progress
int list_patient(int index){
int time_left = 0;
int x = 0;
int i;

//Should check if index points to a record,
//Or else it should return -1!!
if( patient[index].id != NULL){
printf("Patient Details: \n");
printf("ID: %d |",patient[index].id);
printf("Name: %s ",patient[index].forename);
printf("%c ",patient[index].initial);
printf("%s |",patient[index].surname);
printf("Days on queue %d |",day_now()- patient[index].day_of_entry);
printf("Maximum waiting days %d \n",time_left =
patient[index].max_wait - (day_now() - patient[i].day_of_entry));
}
else {
x = -1;
}
return x;
}

int main(void){
list_patient(0);
return 0;
}

May 27 '07 #6
ch****@gmail.com wrote:
I'm trying to compare between pointer and integer in an "IF" statement

how do I make this work?
You don't. Comparing pointers and integers is meaningless.
if(patient[index].id != NULL){
If patient[index].id is an integer, compare it with 0, if that's what
you mean.
If patient[index[.id is a pointer, compare it with NULL (as long as one
of the headers defining it is included) or with 0.

In either case, a simple
if(patient[index].id) {
would do.
}
No matter where you got in the very bad habit of treating pointers and
integers as things of the same sort, it is time for you to break with
those ways. Repent!
May 27 '07 #7
"ch****@gmail.com" <ch****@gmail.comwrites:
From: "ch****@gmail.com" <ch****@gmail.com>
Subject: Re: Comparison between "Pointer and Integer"
Newsgroups: comp.lang.c
Date: 27 May 2007 15:00:04 -0700
Organization: http://groups.google.com

On 27 May, 22:45, Keith Thompson <k...@mib.orgwrote:
>"chu...@gmail.com" <chu...@gmail.comwrites:
I'm trying to compare between pointer and integer in an "IF" statement
how do I make this work?
if(patient[index].id != NULL){
}

Why would you want to compare a pointer and an integer?

What is "id"? If it's a pointer, the above should work. If it's an
integer, comparing it to NULL makes no sense.

It's helpful to post a small, complete program that illustrates your
problem. If you're having a compilation program, show us the exact
error message your compiler gives you. Otherise, your sample should
be compilable by itself.

ok..

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

#define MAXPATIENTS 20

struct details {
int id;
char forename[20];
char initial;
char surname[20];
int day_of_entry;
int max_wait;
};

//Lists individual patient information -Work in Progress
"//" comments are not recommended on Usenet. They're not legal in C90,
and they can create syntax errors when Usenet software causes long lines
to wrap around. If a long "/* ... */" comment wraps around to two
lines, it's still a valid comment.
int list_patient(int index){
int time_left = 0;
int x = 0;
int i;

//Should check if index points to a record,
//Or else it should return -1!!
if( patient[index].id != NULL){
Here's your problem. You didn't show us your compiler's error
message, as I requested, but here's what I got:

c.c: In function `list_patient':
c.c:25: error: `patient' undeclared (first use in this function)

That's not the error you described originally. Assuming that
"patient" is supposed to be either an array of "struct details", or a
pointer to "struct details", the member "patent[index].id" is of type
int.

NULL is a null pointer constant. Why are you trying to compare an int
value to a null pointer constant?

(Incidentally, if your compiler complained about comparing an integer
to a pointer, you're lucky. A legal definition of the NULL macro is
0; that's both an integer expression and a null pointer constant. If
your implementation chose to use that definition, your compiler
wouldn't have diagnosed the error. See section 5 of the comp.lang.c
FAQ, <http://www.c-faq.com/>.)

I'm guessing that you want "id" to be able to hold some distinguished
value that means "this structure does not refer to a patient; ignore
it". If "id" were a pointer, using a null pointer would make sense.
But there is built-in null value for integer types.

You probably want to define some specific value to serve this purpose.
0 might be a good choice, unless 0 is a valid id. -1 might also be a
good choice. In either case, I suggest defining symbolic constant
rather than using "magic numbers" in your code, something like:

#define NOT_A_PATIENT (-1)

...

if (patient[index].id != NOT_A_PATIENT) {
...

This is assuming that a distinguished id value is the way to handle
this; it may not be.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
May 27 '07 #8
On 27 May, 23:20, Martin Ambuhl <mamb...@earthlink.netwrote:
chu...@gmail.com wrote:
I'm trying to compare between pointer and integer in an "IF" statement
how do I make this work?

You don't. Comparing pointers and integers is meaningless.
if(patient[index].id != NULL){

If patient[index].id is an integer, compare it with 0, if that's what
you mean.
If patient[index[.id is a pointer, compare it with NULL (as long as one
of the headers defining it is included) or with 0.

In either case, a simple
if(patient[index].id) {
would do.
}

No matter where you got in the very bad habit of treating pointers and
integers as things of the same sort, it is time for you to break with
those ways. Repent!
I'm not too sure you get what I'm trying to achieve here...I'm trying
to compare the database "patient", inside patient I need to know its
not NULL (Empty)....if you compare it with 0, your saying that the id
should not be 0.

chris

May 27 '07 #9
ch****@gmail.com wrote:
On 27 May, 22:45, Keith Thompson <k...@mib.orgwrote:
>"chu...@gmail.com" <chu...@gmail.comwrites:
>>I'm trying to compare between pointer and integer in an "IF" statement
how do I make this work?
if(patient[index].id != NULL){
}
Why would you want to compare a pointer and an integer?

What is "id"? If it's a pointer, the above should work. If it's an
integer, comparing it to NULL makes no sense.

It's helpful to post a small, complete program that illustrates your
problem. If you're having a compilation program, show us the exact
error message your compiler gives you. Otherise, your sample should
be compilable by itself.

[quoted .sig suppressed. Don't quite .sigs unless you are commenting on
them.]
ok..
Not ok. You code is not compilable. You have no declaration for
patient visible in list_patient. The function day_now() is used without
a declaration or definition. In list_patient you use the variable i
without initialization.

Further, the defintion of struct details, with the member id being an
int, and of index in list_patient being an int, makes this
//Should check if index points to a record,
//Or else it should return -1!!
if( patient[index].id != NULL){
nonsense.

index is an int, and does not point to a record, and you don't check it
anyway.
patient[index].id is an int, does not point, and should be compared to
0, not NULL.

If you what patient[index].id to be a pointer, declare it so.
May 27 '07 #10
In article <11*********************@o5g2000hsb.googlegroups.c om>,
ch****@gmail.com <ch****@gmail.comwrote:
>I'm not too sure you get what I'm trying to achieve here...I'm trying
to compare the database "patient", inside patient I need to know its
not NULL (Empty)....if you compare it with 0, your saying that the id
should not be 0.
In C, NULL is a special value a pointer can have, which doesn't point
to anything. There's no similar value for integer integer variables -
they always have an integer value. Perhaps you're used to databases,
where any field can be null. C doesn't work like that. If there's
some integer value you are sure you will never normally use (0 or -1
perhaps) you can use that instead. But if the value really might be
any integer, you'll need another variable to indicate whether it's
valid.

-- Richard
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
May 27 '07 #11
"ch****@gmail.com" <ch****@gmail.comwrites:
On 27 May, 23:20, Martin Ambuhl <mamb...@earthlink.netwrote:
>chu...@gmail.com wrote:
I'm trying to compare between pointer and integer in an "IF" statement
how do I make this work?

You don't. Comparing pointers and integers is meaningless.
if(patient[index].id != NULL){

If patient[index].id is an integer, compare it with 0, if that's what
you mean.
If patient[index[.id is a pointer, compare it with NULL (as long as one
of the headers defining it is included) or with 0.

In either case, a simple
if(patient[index].id) {
would do.
}

No matter where you got in the very bad habit of treating pointers and
integers as things of the same sort, it is time for you to break with
those ways. Repent!

I'm not too sure you get what I'm trying to achieve here...I'm trying
to compare the database "patient", inside patient I need to know its
not NULL (Empty)....if you compare it with 0, your saying that the id
should not be 0.
Then you don't understand what NULL means.

You have an object of a struct type. That object cannot be "null"
unless you define your own convention for marking it as "null (say,
setting id to -1 or whatever).

NULL is a macro that expands to a null pointer constant; a null
pointer constant evaluates to a null pointer value. More briefly,
NULL is used *only* for pointer types. Each pointer type has a single
distinct value that indicates that it doesn't point to anything; we
call that a "null pointer" or NULL.

The language provides this distinctive null value for pointer types.
There is no null integer or null structure value. There is no "empty"
value for a structure type; each object of that type contains all its
members.

You need to re-think your approach. You can define and enforce your
own convention for marking a structure as being unused. Or you can
keep track of which members of your array are currently valid, perhaps
just with a simple count. Or you can use pointers to structures, but
you might not want to tackle that just yet.

The comp.lang.c FAQ, <http://www.c-faq.com/>, has a lot of good
answers, many of them to questions you probably haven't thought to ask
yet.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
May 27 '07 #12
ch****@gmail.com wrote:

<snip>
struct details {
int id;
<snip>
if( patient[index].id != NULL){
<snip>

NULL if for pointers, why do you think comparing an integer to NULL
makes sense? As id is an int you are probably expected to use a value of
0 to indicate an empty record.
--
Flash Gordon
May 27 '07 #13
"ch****@gmail.com" wrote:
>
I'm trying to compare between pointer and integer in an "IF"
statement how do I make this work?

if(patient[index].id != NULL){
}
I assume you mean that the id field is defined as an integer. You
don't. pointers don't convert to integers. If id is an integer,
try 0 in place of NULL.

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>
<http://kadaitcha.cx/vista/dogsbreakfast/index.html>
cbfalconer at maineline dot net

--
Posted via a free Usenet account from http://www.teranews.com

May 28 '07 #14
On May 28, 2:14 am, "chu...@gmail.com" <chu...@gmail.comwrote:
I'm trying to compare between pointer and integer in an "IF" statement

how do I make this work?

if(patient[index].id != NULL){

}
pretty weird here.
Just to help you make it
if(patient[index].id != !!NULL) {

But this is no excuse for mindlessly comparing an integer with a NULL
value.

Thanks
--psr
Thanks
Chris

May 28 '07 #15
blufox <25**********@gmail.comwrites:
On May 28, 2:14 am, "chu...@gmail.com" <chu...@gmail.comwrote:
>I'm trying to compare between pointer and integer in an "IF" statement

how do I make this work?

if(patient[index].id != NULL){

}
pretty weird here.
Just to help you make it
if(patient[index].id != !!NULL) {
Was that intended to be helpful??

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
May 28 '07 #16
psr
On May 28, 9:53 am, Keith Thompson <k...@mib.orgwrote:
blufox <2500.prad...@gmail.comwrites:
On May 28, 2:14 am, "chu...@gmail.com" <chu...@gmail.comwrote:
I'm trying to compare between pointer and integer in an "IF" statement
how do I make this work?
if(patient[index].id != NULL){
}
pretty weird here.
Just to help you make it
if(patient[index].id != !!NULL) {

Was that intended to be helpful??
Well ...
a better answer would have been
if(patient[index].id) {

Its just how not to use NULL and int for comparing, note above reply
very well removes warning you 'll get eventually on compiling the
original code.(at least on gcc)

But still comparing a pointer with int is not wise.

thanks
--same person who wrote shitty code above :-)
>
--
Keith Thompson (The_Other_Keith) k...@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

May 28 '07 #17
ch****@gmail.com wrote:
On 27 May, 23:20, Martin Ambuhl <mamb...@earthlink.netwrote:
>chu...@gmail.com wrote:
>>I'm trying to compare between pointer and integer in an "IF" statement
how do I make this work?
You don't. Comparing pointers and integers is meaningless.
>>if(patient[index].id != NULL){
If patient[index].id is an integer, compare it with 0, if that's what
you mean.
If patient[index[.id is a pointer, compare it with NULL (as long as one
of the headers defining it is included) or with 0.

In either case, a simple
if(patient[index].id) {
would do.
>>}
No matter where you got in the very bad habit of treating pointers and
integers as things of the same sort, it is time for you to break with
those ways. Repent!

I'm not too sure you get what I'm trying to achieve here...I'm trying
to compare the database "patient", inside patient I need to know its
not NULL (Empty)....if you compare it with 0, your saying that the id
should not be 0.

chris
You are probably looking for the conceptual NIL which doesn't exist in C.

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
May 28 '07 #18
ch****@gmail.com wrote:
I'm trying to compare between pointer and integer in an "IF" statement

how do I make this work?

if(patient[index].id != NULL){
}

Don't use integers to store pointers. In some platforms those types may be
identical but in others (intel and AMD's 64 bit platforms, IIRC) the
integers are 32 bit and the pointers are 64 bit.
Rui Maciel
May 28 '07 #19
ch****@gmail.com wrote:
I'm trying to compare between pointer and integer in an "IF" statement

how do I make this work?

if(patient[index].id != NULL){
}

Don't use integers to store pointers. In some platforms those types may be
identical but in others (intel and AMD's 64 bit platforms, IIRC) the
integers are 32 bit and the pointers are 64 bit.
Rui Maciel
May 28 '07 #20
Rui Maciel <ru********@gmail.comwrites:
ch****@gmail.com wrote:
>I'm trying to compare between pointer and integer in an "IF" statement

how do I make this work?

if(patient[index].id != NULL){
}


Don't use integers to store pointers. In some platforms those types may be
identical but in others (intel and AMD's 64 bit platforms, IIRC) the
integers are 32 bit and the pointers are 64 bit.
He wasn't trying to store integers in pointers. He was trying to use
the "null" value of type int, analogous to a null pointer but for
integers rather than pointers. His problem is that there is no such
thing.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
May 28 '07 #21

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

Similar topics

1
by: Vijay | last post by:
<xs:element name="product"> <xs:complexType> <xs:complexContent> <xs:restriction base="xs:integer"> <xs:attribute name="prodid"...
3
by: signuts | last post by:
I am wondering what it means when a pointer is aligned? Could someone perhaps enlighten me or point me in the right direction? Thank you in...
188
by: infobahn | last post by:
printf("%p\n", (void *)0); /* UB, or not? Please explain your answer. */
16
by: jose_luis_fdez_diaz_news | last post by:
Hi, If I don't include <libgen.h> I get then warnig below in regcmp call: warning: improper pointer/integer combination: op "=" but if I...
3
by: baumann | last post by:
hi all, i could not understand the "unnecessary" pointer comparison. /* 207 * min()/max() macros that also do 208 * strict type-checking.....
0
by: mailforpr | last post by:
Hi. Let me introduce an iterator to you, the so-called "Abstract Iterator" I developed the other day. I actually have no idea if there's...
1
by: pbaldridge | last post by:
I'm trying to code a simple function that will prompt a user if they want to see another set of problems. The error I receive is "ANSI C++ forbids...
2
by: jthep | last post by:
I'm creating an addrecord and a delete record for a linked list below: int addRecord(struct record **start, char name, char address, int...
1
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand....
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python...

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.