in Programming in C edited by
2,054 views
4 votes
4 votes

Prior to using a pointer variable it should be

  1. declared.
  2. initialized.
  3. both declared and initialized.
  4. none of these.
in Programming in C edited by
by
2.1k views

10 Comments

It should be both declared as  well as initialized.

So C is the correct option.
1
1
What's the difference between initialisation and assignment?
1
1
Sir, I think these are somehow related to each other. We usually initialize any type of variable using assignment operator which is nothing but assignment of a particular value to that varibale..

If we assign any value just after the declaration of the variable, then it is to be considered as Initialization. i.e. first time when we assign any value to a variable after its declaration then it is to be considered as initialization..

In case of assignment if we place any value into the variable any time in the program, it is to be considered as assignment..

Please suggest if I am missing the actual concept!!
Thank you!
0
0
What you told is correct. So, is initialisation mandatory before we dereference a pointer?
1
1
Sir, i think it is not mandatory to initialize a pointer before dereferencing it, as when we try to dereference an uninitialized pointer it should throw a runtime error.

But sir  is it  good to dereference  a pointer before initialization?

Please suggest!

Thank you!
0
0
I think initialization isn’t necessary, but assigning a valid address to a pointer variable is necessary before dereferencing.
0
0

@neel19 are assigning valid address to pointer and initialisation of pointer different? 

0
0
The comment above has explained it better. It is even confirmed by Arjun sir.

The first time assigning a value to a pointer variable is initialization. If we reassign a value to that pointer variable again that’s an assignment. We can have a pointer variable that isn’t initialized. But dereferencing an uninitialized pointer is invalid.

This is what I understood.
0
0
But still you said initialisation is not necessary.

Without initialisation, pointer will always point to some invalid memory location. And using (dereferencing) it may result in runtime error.
0
0
Yup, it isn’t. Is anything wrong with it?
0
0

1 Answer

2 votes
2 votes
 int a = 10;
    int *ptr;       //pointer declaration
    ptr = &a;       //pointer initialization

 

or 

 int *ptr = NULL;
    return 0;

answer c is correct

Answer:

Related questions