Velocity Reviews - Computer Hardware Reviews

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

Reply
Thread Tools

Comparison between "Pointer and Integer"

 
 
Martin Ambuhl
Guest
Posts: n/a
 
      05-27-2007
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> 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.



[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.
 
Reply With Quote
 
 
 
 
Richard Tobin
Guest
Posts: n/a
 
      05-27-2007
In article <(E-Mail Removed) om>,
(E-Mail Removed) <(E-Mail Removed)> wrote:

>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.
 
Reply With Quote
 
 
 
 
Keith Thompson
Guest
Posts: n/a
 
      05-27-2007
"(E-Mail Removed)" <(E-Mail Removed)> writes:
> 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.


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) (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
 
blufox
Guest
Posts: n/a
 
      05-28-2007
On May 28, 2:14 am, "(E-Mail Removed)" <(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){
>
> }

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



 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      05-28-2007
blufox <(E-Mail Removed)> writes:
> On May 28, 2:14 am, "(E-Mail Removed)" <(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){
>>
>> }

> 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) (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
 
psr
Guest
Posts: n/a
 
      05-28-2007
On May 28, 9:53 am, Keith Thompson <(E-Mail Removed)> wrote:
> blufox <(E-Mail Removed)> writes:
> > On May 28, 2:14 am, "(E-Mail Removed)" <(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){

>
> >> }

> > 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) (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
 
Joe Wright
Guest
Posts: n/a
 
      05-28-2007
(E-Mail Removed) wrote:
> 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
>

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 ---
 
Reply With Quote
 
Rui Maciel
Guest
Posts: n/a
 
      05-28-2007
(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){
> }



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
 
Reply With Quote
 
Rui Maciel
Guest
Posts: n/a
 
      05-28-2007
(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){
> }



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
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      05-28-2007
Rui Maciel <(E-Mail Removed)> writes:
> (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){
>> }

>
>
> 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) (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
 
 
 
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