了解指针中的*运算符. [英] Understanding * operator in pointers.
问题描述
你好,
我不习惯C编程,所以我想了解*运算符在C指针中的用法.我尝试用google搜索它,但是没有什么令人信服的.(一个例子将是非常好的)
我想知道以下指示或功能:-
1. int * i;
2.结构My_DataBase * My_Name; (My_DataBase之前定义为结构)
3. int * i;
4. j = *(int *)k; (j和k定义为整数).
5.结构My_DataBase * My_Name1 =(My_DataBase *)(j +(k * l));
请帮忙,让我知道您是否有任何
Hello,
I am not used to C programming so i would like to understand the useage of * operator in C pointers.I tried to google it but nothing is quite convincing.(An example will be very good)
I would like to know What the following indicate or function as:-
1. int *i;
2. Struct My_DataBase *My_Name; (My_DataBase is defined earlier as a struct)
3. int* i;
4. j = *(int *)k; (j and k are defined as integers).
5. Struct My_DataBase *My_Name1 = (My_DataBase *) (j + (k * l));
Kindly help and let me know if you have any
推荐答案
(1)将i
声明为指向int
的指针.
(2)将My_Name
声明为指向My_Database struct
的指针. (3)与(1)的含义相同.
(4)将k
解释为指向int
的指针(即假设k
是地址),将j
设置为所指向的int
值. (即获取地址为k
的int值).
(5)将表达式(j+(k*l))
解释为指向struct My_DataBase
的指针,设置My_Name1
指向相同的位置.
我猜(2),(5)应该写成:
(1) declaresi
as a pointer to anint
.
(2) declaresMy_Name
as a pointer to aMy_Database struct
.
(3) has the same meaning of (1).
(4) interpretingk
as pointer toint
(i.e. assuming thatk
is an address) setj
to theint
value pointed.
(i.e. get the int value having addressk
).
(5) interpreting the expression(j+(k*l))
as a pointer to astruct My_DataBase
, setMy_Name1
pointing to the same location.
I guess (2),(5) should be written:
2. struct My_DataBase *My_Name;
5. struct My_DataBase *My_Name1 = (struct My_DataBase *) (j + (k * l));
:)
请注意 C 和 C ++ 之间的细微差别:
Be careful about the little differences between C and C++:
-
编写int* p;
和int *p;
来声明p
作为指向整数的指针.在 C 中,您应该在 C 中编写int *p;
- ,以声明
struct
类型的变量,您应始终使用以下语法:
允许使用 C ++ 中的
in C++ is admitted to writeint* p;
andint *p;
to declarep
as a pointer to integer. In C you should writeint *p;
- in C to declare a variable of a
struct
type you should always use the following syntax:
struct MyStruct
{
...
};
struct MyStruct a;
struct MyStruct *b;
在 C ++ 中,可以在声明变量时省略struct
关键字:
In C++ you can omit the struct
keyword when declaring variables:
struct MyStruct
{
...
};
MyStruct a;
MyStruct *b;
这篇关于了解指针中的*运算符.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!