C++ Primer Exercise -


i started learn c++ , , come across 2 exercises in c++ primer . 1 of exercises can't understand .

exercise 2.22 c++ primer
assuming p pointer int,explain following code:

if (p) //....   if (*p) //....   

as understand in 1 statement checking condition of pointer p whether true or false
in 2 statement actions same except time use dereference operator
if i'm wrong, can tell me mistake.

and next exercise ,this exercise cant understand
exercise 2.23
given pointer p,can determine whether p points valid object ? if so, how? if not , why not?

the thing know when variable initialized , have same type pointer might know pointer points valid object. , trying access invalid pointer can bring code problem , compiler ant find problem.
there else exercise can added ? or guessing wrong ?
thank time !

assuming

int *p = null; 

then:

if (p) 

checks whether p null or not , return false.

if (*p) 

checks whether (*p) == 0, i.e. integer pointed p 0 or not.

if have

int *p; 

i.e., without initializing it, p point random address in memory, and

if (p) 

will of time return true. ,

if (*p) 

will give undefined behavior.


Comments

Popular posts from this blog

android - getbluetoothservice() called with no bluetoothmanagercallback -

sql - ASP.NET SqlDataSource, like on SelectCommand -

ios - Undefined symbols for architecture armv7: "_OBJC_CLASS_$_SSZipArchive" -