answersLogoWhite

0

A pointer is initialized by assigning the address of some object to it ...

int a; // an integer

int *pa; // a pointer

pa = &a; // initialize the pointer

(*pa); // use the pointer

... by allocating memoryand assigning that address to it ...

int *pa; // a pointer to an integer

pa = malloc (1 * sizeof(int)); // allocate

if (pa == NULL) {... exception processing ...}

(*pa); // use the pointer

... or by doing address computation with it ...

int a[10]; // an array of integers

int *pa; // a pointer to an integer

pa = &(a+3); // initialize to the fourth element

(*pa); // use the fourth element

User Avatar

Wiki User

15y ago

Still curious? Ask our experts.

Chat with our AI personalities

TaigaTaiga
Every great hero faces trials, and you—yes, YOU—are no exception!
Chat with Taiga
ReneRene
Change my mind. I dare you.
Chat with Rene
ViviVivi
Your ride-or-die bestie who's seen you through every high and low.
Chat with Vivi
More answers

Pointers should be initialized for the same reason that all variables should be initialized. If you don't initialize them before using them, your program's behavior is undefined. In the case of pointers, this is particularly true because a random pointer could easily point to something valid, but the wrong thing, and changing that wrong thing will cause all sorts of weird, hard-to-explain, behavior; and it will be difficult to track this down, because the point of real failure will not be the same as the point of detected failure.

My practice is to always initialize pointers to NULL at the point of definition, unless you can initialize them immediately. This way, accidentally using one results in a hard crash, usually memory access violation (0xC5 for windows) at location zero.

I would fire any programmer that repeatedly fails to initialize objects before use, and I would similarly flunk any student that does the same thing. It is that important.

User Avatar

Wiki User

13y ago
User Avatar

Add your answer:

Earn +20 pts
Q: Why is it a good practice to initialize pointers?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

When does segmentation fault occur for a prog?

You either reference memory that is non existent, or you attempt to modify memory that is read only. This is usually a result of failure to properly initialize or use pointers or arrays.


Why array is a implicit pointer?

An array of pointers is exactly what it sounds like - one or more pointers arranged in order in memory, accessible through a common base name and indexed as needed. Philosophically, there is no difference between an array of pointers and an array of objects...int a[10]; // 10 integers, named a[0], a[1], a[2], ..., a[9]int *b[10]; // 10 pointers to int, named b[0], b[1], b[2], ..., b[9]If you initialize the array of pointers...int i;for (i = 0; i < 10; i++) b[i] = &a[i];... then *b[0] would be the same as a[0], etc.


Is it possible to initialize null character in string?

yes we can initialize null characterfor example syntax :string='\0';


Which condition is not necessary in dynamic stack?

in dynamic stack we don't have to initialize the size of array while in static stack we have 2 initialize it ......


Why do we need constructor?

when the object is instantiated(created) and delared,it has to assigned with variables.constructor is used for this purpose. syntax: classname ojbectname=new classname(); here classname() is a constructor. eg: box bb=new box(5,10); when this object is instantiated,it can be directly accessed by the constructor(similar to method but without a void because the instance variables are directly used) box(5,10) { }