• c_cpp: 指针的双重赋值


    c/cpp: 指针的双重赋值

     

     

     

     

    一、指针使用、引用的运用

     

     

      1、指针具有双重赋值的属性:第一重赋值,为指针变量赋值内存地址;第二重赋值,为指针变量所指的内存的存储空间赋予内容。

     

     

     

     

     

     二、代码部分

     

     

      1、c代码

     

     1 [root@rockylinux tmp]# uname -a
     2 Linux rockylinux 4.18.0-372.19.1.el8_6.x86_64 #1 SMP Tue Aug 2 16:19:42 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux
     3 [root@rockylinux tmp]# 
     4 [root@rockylinux tmp]# 
     5 [root@rockylinux tmp]# gcc --version
     6 gcc (GCC) 8.5.0 20210514 (Red Hat 8.5.0-10)
     7 Copyright (C) 2018 Free Software Foundation, Inc.
     8 This is free software; see the source for copying conditions.  There is NO
     9 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    10 
    11 [root@rockylinux tmp]# 
    12 [root@rockylinux tmp]# 
    13 [root@rockylinux tmp]# cat pointer_double.c
    14 #include<stdio.h>
    15 #include<stdlib.h>
    16 
    17 
    18 void msg()
    19 {
    20 
    21         int para = 30;
    22 
    23 
    24         // 指针第一重赋值
    25         int* pointer_int = &para;
    26 
    27 
    28         printf("FIRST:\t para_address=%p, pointer_int=%p\n", &para, pointer_int);
    29         printf("FIRST:\t para_value=%d, *pointer_int=%d\n", para, *pointer_int);
    30 
    31 
    32         // 指针的第二重赋值
    33         *pointer_int=60;
    34 
    35         printf("SECOND:\t para_address=%p, pointer_int=%p\n", &para, pointer_int);
    36         printf("SECOND:\t para_value=%d, *pointer_int=%d\n", para, *pointer_int);
    37 
    38 }
    39 
    40 
    41 int main(int argc, char* argv[], char* envp[])
    42 {
    43         msg();
    44 
    45         return 0;
    46 }
    47 [root@rockylinux tmp]# 
    48 [root@rockylinux tmp]# 
    49 [root@rockylinux tmp]# gcc -o pointer_double_c pointer_double.c
    50 [root@rockylinux tmp]# 
    51 [root@rockylinux tmp]# 
    52 [root@rockylinux tmp]# ./pointer_double_c
    53 FIRST:   para_address=0x7ffd3636d594, pointer_int=0x7ffd3636d594
    54 FIRST:   para_value=30, *pointer_int=30
    55 SECOND:  para_address=0x7ffd3636d594, pointer_int=0x7ffd3636d594
    56 SECOND:  para_value=60, *pointer_int=60
    57 [root@rockylinux tmp]# 
    58 [root@rockylinux tmp]# 

     

     

     

      2、cpp代码

     

     1 [root@rockylinux tmp]# uname -a
     2 Linux rockylinux 4.18.0-372.19.1.el8_6.x86_64 #1 SMP Tue Aug 2 16:19:42 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux
     3 [root@rockylinux tmp]# 
     4 [root@rockylinux tmp]# 
     5 [root@rockylinux tmp]# g++ --version
     6 g++ (GCC) 8.5.0 20210514 (Red Hat 8.5.0-10)
     7 Copyright (C) 2018 Free Software Foundation, Inc.
     8 This is free software; see the source for copying conditions.  There is NO
     9 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    10 
    11 [root@rockylinux tmp]# 
    12 [root@rockylinux tmp]# 
    13 [root@rockylinux tmp]# cat  pointer_double.cpp
    14 #include<iostream>
    15 
    16 
    17 using namespace std;
    18 
    19 
    20 void msg()
    21 {
    22 
    23         int para = 30;
    24 
    25 
    26         // 指针第一重赋值
    27         int* pointer_int = &para;
    28 
    29         cout <<"FIRST:\tpara_address="<< &para << ", pointer_int=" << pointer_int << endl;
    30         cout <<"FIRST:\tpara_value="<< para << ", *pointer_int=" << *pointer_int << endl;
    31 
    32 
    33         // 指针的第二重赋值
    34         *pointer_int=60;
    35 
    36         cout <<"SECOND:\tpara_address="<< &para << ", pointer_int=" << pointer_int << endl;
    37         cout <<"SECOND:\tpara_value="<< para << ", *pointer_int=" << *pointer_int << endl;
    38 
    39 }
    40 
    41 
    42 int main(int argc, char* argv[], char* envp[])
    43 {
    44         msg();
    45 
    46         return 0;
    47 }
    48 [root@rockylinux tmp]# 
    49 [root@rockylinux tmp]# 
    50 [root@rockylinux tmp]# g++ -o pointer_double_cpp pointer_double.cpp 
    51 [root@rockylinux tmp]# 
    52 [root@rockylinux tmp]# 
    53 [root@rockylinux tmp]# ./pointer_double_cpp
    54 FIRST:  para_address=0x7fffab0dfad4, pointer_int=0x7fffab0dfad4
    55 FIRST:  para_value=30, *pointer_int=30
    56 SECOND: para_address=0x7fffab0dfad4, pointer_int=0x7fffab0dfad4
    57 SECOND: para_value=60, *pointer_int=60
    58 [root@rockylinux tmp]# 
    59 [root@rockylinux tmp]#

     

  • 相关阅读:
    Leetcode 1414. 和为 K 的最少斐波那契数字数目(贪心)
    A ConvNet for the 2020论文阅读笔记
    Leetcode 957. N 天后的牢房(暴力)
    洛谷P1879 [USACO06NOV]Corn Fields G(状压DP)
    CF1553D Backspace(双指针)
    26
    37
    36
    3.4
    3.3
  • 原文地址:https://www.cnblogs.com/lnlidawei/p/16647216.html
Copyright © 2020-2023  润新知