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 .
它给了我错误 - long类型的值不能用于初始化long *类型的实体。请帮帮我 。谢谢 。
3 个解决方案
#1
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.
任何正常变量的访问可以是: - 1.使用变量名称。 2.使用变量的地址。注意: - 就像你可以通过使用他/她的名字或在社会中使用他/她的地址来找到一个人。
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.
我们可以有一个指向任何变量类型的指针。第2点。它用于间接访问变量。第3点。要记住的关键注意事项。指针类型定义了它可以指向(引用)的变量类型。
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".
现在,遇到问题: - 你想在指针变量中存储一个长值,即1L。根据“使用指针”完全错误。
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.
让我们假设系统已为变量a分配了存储器位置80F。
int a = 10 ;
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.
我们可以使用变量名a或地址80F来访问值10。由于存储器地址只是数字,因此可以将它们分配给其他变量。保存内存地址的变量称为指针变量。因此,指针变量只是包含地址的变量,而地址是另一个变量的位置。指针变量的值将存储在另一个存储单元中。
#2
1
You are assigning value 1L
to an address. Change it to :
您正在为地址分配值1L。将其更改为:
long x = 1L;
long *status = &x;
#3
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 :
指针变量用于存储一些内存地址。你在这里尝试做的是在指针中存储一些值(这是错误的)。 1L是long类型的值,您试图将其存储在long *类型的指针中(这就是错误所说的)。正确的方法是:
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:
要访问值1L,您可以选择以下任一方法:
printf("%ld",lg);
OR
printf("%ld",*lptr);
Output of both the printf will be the same
printf的输出都是一样的
#1
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.
任何正常变量的访问可以是: - 1.使用变量名称。 2.使用变量的地址。注意: - 就像你可以通过使用他/她的名字或在社会中使用他/她的地址来找到一个人。
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.
我们可以有一个指向任何变量类型的指针。第2点。它用于间接访问变量。第3点。要记住的关键注意事项。指针类型定义了它可以指向(引用)的变量类型。
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".
现在,遇到问题: - 你想在指针变量中存储一个长值,即1L。根据“使用指针”完全错误。
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.
让我们假设系统已为变量a分配了存储器位置80F。
int a = 10 ;
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.
我们可以使用变量名a或地址80F来访问值10。由于存储器地址只是数字,因此可以将它们分配给其他变量。保存内存地址的变量称为指针变量。因此,指针变量只是包含地址的变量,而地址是另一个变量的位置。指针变量的值将存储在另一个存储单元中。
#2
1
You are assigning value 1L
to an address. Change it to :
您正在为地址分配值1L。将其更改为:
long x = 1L;
long *status = &x;
#3
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 :
指针变量用于存储一些内存地址。你在这里尝试做的是在指针中存储一些值(这是错误的)。 1L是long类型的值,您试图将其存储在long *类型的指针中(这就是错误所说的)。正确的方法是:
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:
要访问值1L,您可以选择以下任一方法:
printf("%ld",lg);
OR
printf("%ld",*lptr);
Output of both the printf will be the same
printf的输出都是一样的