2

I am trying to initialize long type pointer variable like

long *status =1L;

it is giving me error that- value of type long cannot be used to initialize an entity of type long* . please help me . Thank you .

BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70
Robert
  • 65
  • 3
  • 12

3 Answers3

1

You are assigning value 1L to an address. Change it to :

long x = 1L;
long *status = &x;
Marievi
  • 4,951
  • 1
  • 16
  • 33
1

Pointer variable is used store some memory address. What you are trying to do here is store some value in a pointer(which is wrong). 1L is a value of type long which your are trying to store in a pointer of type long*(this is what the error says). The correct way to do this is :

First, store the value in the memory:

long lg = 1L;

Second, initialize a pointer pointing to the memory location of above variable:

long* lptr = ≶ //&lg means address of the variable lg

To access the value 1L you can chose any of the following methods:

printf("%ld",lg);

OR

printf("%ld",*lptr);

Output of both the printf will be the same

Avantika Saini
  • 792
  • 4
  • 9
1

Acess to any normal variable can be :- 1. By use of name of variable. 2. By use of address of variable. Note:- it's like you can find a person by using his/her name or by using his/her address in the society.

Ques. What is Pointer variable? Is it diffrent from normal variable we use to? Ans. A pointer is a "special" variable which contains(point to/refer to) the address in memory of another variable.

point 1. We can have a pointer to any variable type. point 2. It's used to indirect access of variable. point 3. its key note to remember. Type of pointer defines the type of variable to which it can point(refer) to.

Now, coming to you problem:- you want to store a long value i.e. 1L in a pointer variable . which is totally wrong according the "use of pointer".

Concept of Pointer-->

Whenever a variable is declared, system will allocate a location to that variable in the memory, to hold value. This location will have its own address number.

Let us assume that system has allocated memory location 80F for a variable a.

int a = 10 ;

enter image description here

We can access the value 10 by either using the variable name a or the address 80F. Since the memory addresses are simply numbers they can be assigned to some other variable. The variable that holds memory address are called pointer variables. A pointer variable is therefore nothing but a variable that contains an address, which is a location of another variable. Value of pointer variable will be stored in another memory location.

enter image description here

Shivam Sharma
  • 1,015
  • 11
  • 19