c语言指针的指针_C指针介绍

c语言指针的指针

A Pointer in C language is a variable which holds the address of another variable of same data type.

C语言中的指针是一个变量,它保存同一数据类型的另一个变量的地址。

Pointers are used to access memory and manipulate the address.

指针用于访问内存和操作地址。

Pointers are one of the most distinct and exciting features of C language. It provides power and flexibility to the language. Although pointers may appear a little confusing and complicated in the beginning, but trust me, once you understand the concept, you will be able to do so much more with C language.

指针是C语言最独特和令人兴奋的功能之一。 它为语言提供了强大的功能和灵活性。 尽管指针一开始可能看起来有些混乱和复杂,但是请相信我,一旦您理解了这个概念,就可以使用C语言做更多的事情。

Before we start understanding what pointers are and what they can do, let's start by understanding what does "Address of a memory location" means?

在开始理解什么是指针以及指针可以做什么之前,让我们首先了解“内存位置的地址”是什么意思?

C地址 (Address in C)

Whenever a variable is defined in C language, a memory location is assigned for it, in which it's value will be stored. We can easily check this memory address, using the & symbol.

只要使用C语言定义了变量,就会为其分配存储位置,并将其值存储在其中。 我们可以使用&符号轻松检查该内存地址。

If var is the name of the variable, then &var will give it's address.

如果var是变量的名称,则&var将给出其地址。

Let's write a small program to see memory address of any variable that we define in our program.

让我们编写一个小程序来查看我们在程序中定义的任何变量的内存地址。

#include<stdio.h>

void main()
{
    int var = 7;
    printf("Value of the variable var is: %d\n", var);
    printf("Memory address of the variable var is: %x\n", &var);
}

Value of the variable var is: 7 Memory address of the variable var is: bcc7a00

变量var的值为:7变量var的内存地址为:bcc7a00

You must have also seen in the function scanf(), we mention &var to take user input for any variable var.

您还必须在函数scanf()看到过,我们提到&var来接受用户输入的任何变量var

scanf("%d", &var);

This is used to store the user inputted value to the address of the variable var.

这用于将用户输入的值存储到变量var的地址中。

指针的概念 (Concept of Pointers)

Whenever a variable is declared in a program, system allocates a location i.e an address to that variable in the memory, to hold the assigned value. This location has its own address number, which we just saw above.

每当在程序中声明变量时 ,系统都会在内存中为该变量分配一个位置,即地址,以保存分配的值。 这个位置有自己的地址号码,我们刚才在上面看到过。

Let us assume that system has allocated memory location 80F for a variable a.

让我们假设系统已经为变量a分配了内存位置80F

int a = 10;

int a = 10;

storage of variable in C

We can access the value 10 either by using the variable name a or by using its address 80F.

我们可以使用变量名a或地址80F来访问值10

The question is how we can access a variable using it's address? Since the memory addresses are also just numbers, they can also be assigned to some other variable. The variables which are used to hold memory addresses are called Pointer variables.

问题是如何使用变量的地址访问变量? 由于存储器地址也只是数字,因此也可以将它们分配给其他变量。 用于保存内存地址的变量称为指针变量

A pointer variable is therefore nothing but a variable which holds an address of some other variable. And the value of a pointer variable gets stored in another memory location.

因此, 指针变量不过是保存其他变量地址的变量。 指针变量的值将存储在另一个存储位置中。

Pointer to a variable

使用指针的好处 (Benefits of using pointers)

Below we have listed a few benefits of using pointers:

下面我们列出了使用指针的一些好处:

  1. Pointers are more efficient in handling Arrays and Structures.

    指针在处理数组和结构时效率更高。

  2. Pointers allow references to function and thereby helps in passing of function as arguments to other functions.

    指针允许引用函数,从而有助于将函数作为参数传递给其他函数。

  3. It reduces length of the program and its execution time as well.

    它减少了程序的长度及其执行时间。

  4. It allows C language to support Dynamic Memory management.

    它允许C语言支持动态内存管理。

In the next tutorial we will learn syntax of pointers, how to declare and define a pointer, and using a pointer. See you in the next tutorial.

在下一个教程中,我们将学习指针的语法,如何声明和定义指针以及如何使用指针。 在下一个教程中见。

翻译自: https://www.studytonight.com/c/pointers-in-c.php

c语言指针的指针

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值