Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Comparison between "Pointer and Integer"

Reply
Thread Tools

Comparison between "Pointer and Integer"

 
 
chutsu@gmail.com
Guest
Posts: n/a
 
      05-27-2007
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

 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      05-27-2007
http://www.velocityreviews.com/forums/(E-Mail Removed) 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.
 
Reply With Quote
 
 
 
 
Keith Thompson
Guest
Posts: n/a
 
      05-27-2007
"(E-Mail Removed)" <(E-Mail Removed)> writes:
> 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) (E-Mail Removed) <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"
 
Reply With Quote
 
chutsu@gmail.com
Guest
Posts: n/a
 
      05-27-2007
On 27 May, 22:45, Keith Thompson <(E-Mail Removed)> wrote:
> "(E-Mail Removed)" <(E-Mail Removed)> writes:
> > 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) (E-Mail Removed) <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;
}

 
Reply With Quote
 
Flash Gordon
Guest
Posts: n/a
 
      05-27-2007
(E-Mail Removed) 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
 
Reply With Quote
 
chutsu@gmail.com
Guest
Posts: n/a
 
      05-27-2007
On 27 May, 22:45, Keith Thompson <(E-Mail Removed)> wrote:
> "(E-Mail Removed)" <(E-Mail Removed)> writes:
> > 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) (E-Mail Removed) <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;
}

 
Reply With Quote
 
chutsu@gmail.com
Guest
Posts: n/a
 
      05-27-2007
On 27 May, 22:45, Keith Thompson <(E-Mail Removed)> wrote:
> "(E-Mail Removed)" <(E-Mail Removed)> writes:
> > 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) (E-Mail Removed) <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;
}

 
Reply With Quote
 
Martin Ambuhl
Guest
Posts: n/a
 
      05-27-2007
(E-Mail Removed) 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!
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      05-27-2007
"(E-Mail Removed)" <(E-Mail Removed)> writes:
> From: "(E-Mail Removed)" <(E-Mail Removed)>
> 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 <(E-Mail Removed)> wrote:
>> "(E-Mail Removed)" <(E-Mail Removed)> writes:
>> > 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) (E-Mail Removed) <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"
 
Reply With Quote
 
chutsu@gmail.com
Guest
Posts: n/a
 
      05-27-2007
On 27 May, 23:20, Martin Ambuhl <(E-Mail Removed)> wrote:
> (E-Mail Removed) 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Comparison of 2 files and generating the output based on comparison Deepu Perl Misc 1 02-07-2011 03:09 PM
Price Comparison Service. Best Deal. Special Coupon atBest-Price-Comparison.com rapee Digital Photography 0 03-14-2008 06:46 AM
Comparison between dbxml,tamino and eXist.. Shuch XML 0 01-02-2006 01:15 PM
Product Comparison Between BEA and Microsoft... Bob Morvay ASP .Net 1 01-30-2004 04:27 PM
Shot speed and a comparison between digital and film Eigenvector Digital Photography 6 09-25-2003 01:14 AM



Advertisments