473,471 Members | 4,625 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

query pointer && *pointer: safe?

Hi,

if I want to make sure that neither the pointer arg nor the value to
which it points to is NULL: Is solution (a) safe -- or does it have to
be like (b) ?
Felix

void f(int* p){

/* (a) */
if(p==NULL || *p==NULL) return;

/* (b) */
if(p==NULL) return;
if(*p==NULL) return;

}
Dec 2 '06 #1
5 1674
Felix Kater wrote:
Hi,

if I want to make sure that neither the pointer arg nor the value to
which it points to is NULL: Is solution (a) safe -- or does it have to
be like (b) ?
NULL is a null pointer constant. It might be a literal 0 on your
implementation, but this is not true on several others. If you're
comparing *p to NULL because p is a pointer to a pointer to something,
you should declare p as such. If you're comparing *p to NULL because
you want to know if it is zero, just check for 0. That said, the && and
|| operators don't evaluate their right operands if the result is known
from the left, so both ways are just as safe.
Felix

void f(int* p){

/* (a) */
if(p==NULL || *p==NULL) return;

/* (b) */
if(p==NULL) return;
if(*p==NULL) return;

}
Dec 2 '06 #2
Felix Kater wrote:
Hi,

if I want to make sure that neither the pointer arg nor the value to
which it points to is NULL: Is solution (a) safe -- or does it have to
be like (b) ?
Felix

void f(int* p){

/* (a) */
if(p==NULL || *p==NULL) return;

/* (b) */
if(p==NULL) return;
if(*p==NULL) return;

}
Both are wrong. Testing `p == NULL' is fine, but `*p' is
an int, not a pointer. On some systems, `*p == NULL' will get
an error message from the compiler.

--
Eric Sosman
es*****@acm-dot-org.invalid
Dec 2 '06 #3
Eric Sosman <es*****@acm-dot-org.invalidwrote:
void f(int* p){

/* (a) */
if(p==NULL || *p==NULL) return;

/* (b) */
if(p==NULL) return;
if(*p==NULL) return;

}

Both are wrong.
Please forgive. I meant an int** arg (not int*), while it is
additionally granted that the address of an int* is passed by
the client programmer. Is it safe in *this* case to evaluate both
like in (a)?
Felix

void f(int** pp){

/* (a) */
if(pp==NULL || *pp==NULL) return;

/* (b) */
if(pp==NULL) return;
if(*pp==NULL) return;

}

Dec 2 '06 #4
Felix Kater said:

<snip>
>
Please forgive. I meant an int** arg (not int*), while it is
additionally granted that the address of an int* is passed by
the client programmer. Is it safe in *this* case to evaluate both
like in (a)?
Felix

void f(int** pp){

/* (a) */
if(pp==NULL || *pp==NULL) return;
Yes, that's fine. If pp is NULL, the "or-expression" is true, and shortcut
evaluation means there is no need to evaluate the second part, and it WILL
NOT be done (this is guaranteed).

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Dec 2 '06 #5
Felix Kater wrote:
Please forgive. I meant an int** arg (not int*), while it is
additionally granted that the address of an int* is passed by
the client programmer. Is it safe in *this* case to evaluate both
like in (a)?
Yes.
void f(int** pp){

/* (a) */
if(pp==NULL || *pp==NULL) return;

/* (b) */
if(pp==NULL) return;
if(*pp==NULL) return;

}
--
pete
Dec 2 '06 #6

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

Similar topics

4
by: xuatla | last post by:
Hi, How to copy a pointer to another pointer? Can I do in the following way: // START double *copyfrom = new double; double *copyto = new double;
1
by: Bruce | last post by:
I have designed a form (Project Form) using a Select query and it works just fine. I created another very similar form (Promotional Form) that uses the same table with another select query - very...
2
by: Yossarian | last post by:
Hi, I'm a bit confused about something, hopefully someone can put me straight. I'd like to be able to call a function which takes a pointer to pointer, have that function allocate memory and...
7
by: key9 | last post by:
Hi ALL. Basic Question. sample: class MyClass { ....
1
by: AJ | last post by:
Folllowing on from a previous post, i have created a stored query as follows. SELECT c.ID, c.Company_Name, p., 1 As QueryNbr FROM (Company AS c LEFT JOIN Sale AS s ON c.ID = s.Company_ID) LEFT...
0
by: cfaheybestpitch | last post by:
Hi There, I have designed a DTS package which extracts a query into an excel file. It uses a query that changes dynamically based on user preferences, so I have used the dynamic property...
8
by: toton | last post by:
HI, One more small doubt from today's mail. I have certain function which returns a pointer (sometimes a const pointer from a const member function). And certain member function needs reference...
3
by: sshafer1 | last post by:
Hi. I have a query that I would like to run periodically to drop the data in an existing table and re-write the table with the query information. However, when I do this, some of my field...
10
by: MeeMee | last post by:
Hi I have a problem appending data into an oracle table from access. I imported the new data from an excel sheet into a table in access and useed an append query to add the data into a linked...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
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: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
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.