C指针,指向语法 [英] C pointer syntax

查看:136
本文介绍了C指针,指向语法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是code?

int *ptr = &x;

void* q = &x;
int* p = q;

我很新的C和指针的概念 - 已经主要是在Java的教 - 所以只是有点困惑

I'm very new to C and the concept of pointers - having been taught primarily in Java - so just a little confused.

先谢谢了。

推荐答案

无效* 用于注意C.结果通用指针
这意味着它可以指向任何类型。

void * is used to note a generic pointer in C.
Meaning it can point to any type.

因此​​,在第一种情况下为int * PTR =安培; X; 你用一个指针 INT 等等使用它的人都知道它是操纵一个整数。

So in the first case int *ptr = &x; you use a pointer to an int so anyone using it knows it is manipulating an integer.

在第二种情况下无效* Q =&放大器; X; 您指向通过gereric指针的整数地址结​​果
的问题是,它是不明确该指针指的是什么类型的。

In the second case void* q = &x; you point to an integer address via a gereric pointer.
The problem is that it is not clear what type this pointer is refering to.

所以第2例有相同的效果(在你的具体的例子),但无效* 是不是安全这样被使用。

So the first and 2 examples have the same effect (in your specific example) but the void * is not safe to be used like this.

这篇关于C指针,指向语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆