当已经声明** p时,* p是什么意思

编程入门 行业动态 更新时间:2024-10-26 02:32:33
本文介绍了当已经声明** p时,* p是什么意思的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

代码

Code

short **p = (short **)malloc(sizeof(short *)); *p = malloc(sizeof(short)); **p = 10; printf("**p = %d", **p);

输出

** p = 10

**p = 10

在此代码中,声明了多指针**p,并且使用了*p而没有任何声明(可能是**p).

In this code, a multiple pointer **p is declared and *p is used without any declaration(maybe it's by **p).

在我的情况下,*p是什么意思?对不起,一个非常简单的问题.

What does *p mean in my case? Sorry for very simple question.

我看到了C标准和堆栈溢出,但是我找不到东西.

I saw C standard and stack overflow, but I couldn't find out something.

推荐答案

对于任何数组或指针p和索引i,表达式p[i]完全等于*(p + i)(其中*是一元解引用运算符,它在指针上的结果就是指针指向的值.

For any array or pointer p and index i, the expression p[i] is exactly equal to *(p + i) (where * is the unary dereference operator, the result of it on a pointer is the value that the pointer is pointing to).

因此,如果我们有p[0],则它精确等于*(p + 0),等于*(p),等于*p.从此倒退,*p等于p[0].

So if we have p[0] that's then exactly equal to *(p + 0), which is equal to *(p) which is equal to *p. Going backwards from that, *p is equal to p[0].

所以

*p = malloc(sizeof(short));

等于

p[0] = malloc(sizeof(short));

还有

**p = 10;

等于

p[0][0] = 10;

(**p等于*(*(p + 0) + 0)等于*(p[0] + 0)等于p[0][0])

请务必注意,星号*在不同的上下文中可能表示不同的意思.

It's important to note that the asterisk * can mean different things in different contexts.

可以在声明变量时使用,然后表示声明为指针":

It can be used when declaring a variable, and then it means "declare as pointer":

int *p; // Declare p as a pointer to an int value

它可以用来取消引用一个指针,以获取该指针指向的值:

It can be used to dereference a pointer, to get the value the pointer is pointing to:

*p = 0; // Equal to p[0] = 0

它可以用作乘法运算符:

And it can be used as the multiplication operator:

r = a * b; // Multiply the values in a and b, store the resulting value in r

更多推荐

当已经声明** p时,* p是什么意思

本文发布于:2023-08-01 04:19:54,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1265159.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:声明

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!