c中指针的用途是什么? [英] What is use of pointer in c?

查看:77
本文介绍了c中指针的用途是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

指针的用途是什么?

使用指针比没有指针可以做得更好?

哪些事情只能通过指针来完成?

请给我一个简单的例子。

What is the use of pointers?
Which things that can be done better with pointers than without?
And which things that can be done only with pointers?
Please give me a simple example of it.

推荐答案

指针可能是C中最有用(也是最危险)的语言元素。



他们允许什么?它们允许您编写代码而无需知道始终保存数据的变量的名称。它们允许你在运行时分配内存,这样你就可以根据需要拥有多个不同的副本 - 没有指针你必须定义一个固定大小的数组,并分配太多的空间(这是浪费)或太少的空间(带来灾难性的)。它们允许您形成链接列表和包含不同其他结构列表的结构,并在不知道运行时它是哪个函数的情况下引用函数。



他们基本上让你做得恰当,灵活。没有它们,它就像一本没有索引,内容或页码的大型思想书。信息在某处,但找到它真的很痛!
Pointers are probably the single most useful (and the most dangerous) language element in C.

What do they allow? They let you code without having to know the name of the variable that holds the data all the time. They let you allocate memory at run time, so that you have as many different copies of it as you need - without pointers you would have to define a fixed size array, and either allocate too much space (which is wasteful) or too little space (with is disastrous). They let you form linked lists, and structures that contain lists of different other structures, and to reference functions without knowing which function it is at run time.

They basically let you do things properly, and flexibly. Without them, it would be like a big think book without an index, contents or page numbers. the info is in there somewhere, but it''s a real pain to find it!


指针被声明为:



< pointer type => *< pointer-name>

在上面的声明中:



pointer-type:它指定指针的类型。它可以是int,char,float等。这个类型指定了这个指针可以存储的地址的变量类型。

pointer-name:它可以是用户指定的任何名称。在专业上,每个代码都遵循一些编码样式。指针名称通常以'p'开头或以'ptr'结尾

指针声明的示例可以是:



char * chptr;

在上面的声明中,'char'表示指针类型,chptr是指针的名称,而星号'*'表示'chptr'是指针变量。



如何初始化指针?



指针按以下方式初始化:



< pointer> = <地址>







< pointer declaration =>

<指针名称> =


请注意,上面的变量类型应该与指针类型相同。(虽然这不是严格的规则,但对于初学者,应该记住这一点。)



例如:



char ch =''c'';

char * chptr =& ch; //初始化







char ch =''c'';

char * chptr;

chptr =& ch // initialize

在上面的代码中,我们声明了一个存储值'c'的字符变量ch。现在,我们声明了一个字符指针'chptr'并用变量'ch'的地址初始化它。



注意'&'运算符用于访问任何类型的变量的地址。



如何使用指针?



指针可以是在两种情况下使用。



上下文1:用于访问指针存储的存储器地址变量的地址。



再次考虑以下代码:



char ch =''c'';

char * chptr =& ch ;

现在,每当我们在上面两行之后的代码中引用名称'chptr'时,编译器就会尝试获取此指针变量所包含的值,这是变量的地址( ch)指针指向的。即'chptr'给出的值等于'& ch'。



例如:



char * ptr = chptr;

'chptr'保存的值(在本例中是变量'ch'的地址)被赋给新指针'ptr'。



上下文2:用于访问指针存储的内存地址变量的值。



继续上面使用的代码片段:



char ch =''c'';

char t;

char * chptr =& ch;

t = * chptr;

我们在上面的最后一行看到,我们在指针名称前面使用了'*' 。这个星号运算符做了什么?



好​​吧,这个运算符应用于指针变量名称(如上面的最后一行)时会产生变量的值这个指针指向。这意味着,在这种情况下,'* chptr'将产生保持在chptr所持有的地址的值。由于'chptr'保存变量'ch'的地址,'ch'的值是'c',所以'* chptr'yeilds'c'。



与指针一起使用时,星号'*'运算符也称为'运算符'。



C指针示例



考虑以下代码:



代码:



#include< stdio.h中>



int main(无效)

{

char ch =''c'';

char * chptr =& ch;



int i = 20;

int * intptr =& i;



float f = 1.20000;

float * fptr =& f;



char * ptr =我是一个字符串;



printf(\ n [%c],[%d],[%f],[%c],[%s] \ n ,* chptr,* intptr,* fptr,* ptr,ptr);



返回0;

}

输出:


A pointer is declared as :

<pointer type=""> *<pointer-name>
In the above declaration :

pointer-type : It specifies the type of pointer. It can be int,char, float etc. This type specifies the type of variable whose address this pointer can store.
pointer-name : It can be any name specified by the user. Professionally, there are some coding styles which every code follows. The pointer names commonly start with ‘p’ or end with ‘ptr’
An example of a pointer declaration can be :

char *chptr;
In the above declaration, ‘char’ signifies the pointer type, chptr is the name of the pointer while the asterisk ‘*’ signifies that ‘chptr’ is a pointer variable.

How to initialize a Pointer?

A pointer is initialized in the following way :

<pointer> =


OR

<pointer declaration="">
<name-of-pointer> =

Note that the type of variable above should be same as the pointer type.(Though this is not a strict rule but for beginners this should be kept in mind).

For example :

char ch = ''c'';
char *chptr = &ch; //initialize

OR

char ch = ''c'';
char *chptr;
chptr = &ch //initialize
In the code above, we declared a character variable ch which stores the value ‘c’. Now, we declared a character pointer ‘chptr’ and initialized it with the address of variable ‘ch’.

Note that the ‘&’ operator is used to access the address of any type of variable.

How to Use a Pointer?

A pointer can be used in two contexts.

Context 1: For accessing the address of the variable whose memory address the pointer stores.

Again consider the following code :

char ch = ''c'';
char *chptr = &ch;
Now, whenever we refer the name ‘chptr’ in the code after the above two lines, then compiler would try to fetch the value contained by this pointer variable, which is the address of the variable (ch) to which the pointer points. i.e. the value given by ‘chptr’ would be equal to ‘&ch’.

For example :

char *ptr = chptr;
The value held by ‘chptr’ (which in this case is the address of the variable ‘ch’) is assigned to the new pointer ‘ptr’.

Context 2: For accessing the value of the variable whose memory address the pointer stores.

Continuing with the piece of code used above :

char ch = ''c'';
char t;
char *chptr = &ch;
t = *chptr;
We see that in the last line above, we have used ‘*’ before the name of the pointer. What does this asterisk operator do?

Well, this operator when applied to a pointer variable name(like in the last line above) yields the value of the variable to which this pointer points. Which means, in this case ‘*chptr’ would yield the value kept at address held by chptr. Since ‘chptr’ holds the address of variable ‘ch’ and value of ‘ch’ is ‘c’, so ‘*chptr’ yeilds ‘c’.

When used with pointers, the asterisk ‘*’ operator is also known as ‘value of’ operator.

An Example of C Pointers

Consider the following code :

CODE :

#include <stdio.h>

int main(void)
{
char ch = ''c'';
char *chptr = &ch;

int i = 20;
int *intptr = &i;

float f = 1.20000;
float *fptr = &f;

char *ptr = "I am a string";

printf("\n [%c], [%d], [%f], [%c], [%s]\n", *chptr, *intptr, *fptr, *ptr, ptr);

return 0;
}
OUTPUT :


./ pointers



[c], [20],[1.200000],[I],[我是一个字符串]
./pointers

[c], [20], [1.200000], [I], [I am a string]


这篇关于c中指针的用途是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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