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
Chat with our AI personalities
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.
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.
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.
yes we can initialize null characterfor example syntax :string='\0';
in dynamic stack we don't have to initialize the size of array while in static stack we have 2 initialize it ......
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) { }