指针的学习(上)

数组名其实是数组第一个元素的地址

#include <cstdio>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
    char str[345];
    cout << "please enter a string" << endl;
    scanf("%s" ,str);
    printf("the address of str %p\n", str);
    printf("the address of str[0] %p\n", &str[0]);
    return 0;
}

指针和数组的关系

int main(int argc, char *argv[])
{
    char str[125];
    cout << "please enter a string" << endl;
    scanf("%s", str);
    cout << str << endl;
    printf("the address of str %p\n", str);
    printf("the address of str[0] %p\n", &str[0]);
    return 0;
}

指针运算

#include <cstdio>
#include <iostream>

using namespace std;

int main(int argc, char *argv[]) {
    char a[] = "zjf";
    char *p = a;
    printf("*p = %c, *(p+1) = %c", *p, *(p + 1));
    return 0;
}

数组和指针相互转换

#include <cstdio>
#include <cstring>
#include <iostream>

using namespace std;

int main(int argc, char *argv[]) {
    char *str1 = "I love c";
    int length1;
    char str2[] = "I love java";
    char *str3 = str2;
    int length2;
    length1 = strlen(str1);
    for (int i = 0; i < length1; ++i) {
        printf("%c", str1[i]);
    }
    cout << endl;
    length2 = strlen(str2);
    for (int j = 0; j < length2; ++j) {
        printf("%c", *(str3 + j));
    }
    cout << endl;
}

数组与指针的区别

#include <cstdio>
#include <cstring>
#include <iostream>

using namespace std;

int main(int argc, char *argv[]) {
    char str[] = "I love c++";
    char *target = str;
    int count = 0;
    /*
    *str++ 写法错误,自增运算符的操作对象要求是可修改的左值
    数组名等同于常量指针,所有不可以对数组名进行复制
    常量指针:表明这个指针是一个指向常量的指针变量
    */
    while (*target++ != '\0') {
        count++;
    }
    printf("there are %d string", count);

    return 0;
}

指针数组

#include <cstdio>
#include <cstring>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
    /*通过字符优先级来判断是指针数组或者是数组指针*/

    /*指针数组:int *p1[5]
        指针数组是一个数组,每个数组元素存放一个指针变量*/
    
    /*指针数组用来存放字符串*/
    char *p1[5] = {
            "I love you",
            "I love c",
            "I love java",
            "I love spring",
            "I love maven"
    };
    for (int i = 0; i < 5; ++i) {
        printf("%s\n", p1[i]);
    }
    return 0;
}

指针数组是一个数组,每个数组元素存放一个指针变量

数组指针

#include <cstdio>
#include <cstring>
#include <iostream>

using namespace std;

int main(int argc, char *argv[]) {
    /*数组指针是一个指针,它指向的是一个数组*/

    /*int (*p2)[5] = {1, 2, 3, 4, 5};
    for (int i = 0; i < 5; ++i) {
        printf("%d\n", *(p2 + i));
    }*/

    /*上面代码是一个错误示范,数组指针本质上是一个指针*/

    int temp[5] = {1, 2, 3, 4, 5};
    int (*p2)[5] = &temp;
    /*区别于:
     * int temp[5] = {1, 2, 3, 4, 5};
     * int *p = temp;
     * 使用 *(p + i) 可以遍历输出
     * 这里的p仅仅是指向数组的首地址,并不是指向数组
     * 而数组指针要求指向整个数组
     * */
    
    for (int i = 0; i < 5; ++i) {
        printf("%d\n", *(*p2 + i));
    }
    /**p2是数组的地址*/
    return 0;
}

数组指针是一个指针,它指向的是一个数组

二维数组和指针

#include <cstdio>
#include <iostream>

using namespace std;

int main(int argc, char *argv[]) {
    int array[4][5];
    printf("%d\n", sizeof(int));
    printf("%d\n", sizeof(array));
    printf("%p\n", array);
    printf("%p\n", array + 1);
    /*
     * 运行结果
     * 4
     * 80
     * 0061FED0
     * 0061FEE4
     * */
    return 0;
}
#include <cstdio>

int main(int argc, char const *argv[])
{
    int array[4][5] = {0};
    int i, j, k = 0;
    for (i = 0; i < 4; i++)
    {
        for (j = 0; j < 5; j++)
        {
            array[i][j] = k++;
        }
    }
    printf("*(array + 1): %p\n", *(array + 1));
    printf("array[1]: %p\n", array[1]);
    printf("&array[1][0]: %p\n", &array[1][0]);
    printf("**(array + 1): %d\n", **(array + 1));
    printf("*(*(array + 1) + 3): %d\n", *(*(array + 1) + 3));
    printf("array[1][3]: %d\n", array[1][3]);
    //      结果
    //      *(array + 1): 0061FED8
    //      array[1]: 0061FED8
    //      &array[1][0]: 0061FED8
    //      **(array + 1): 5
    //      *(*(array + 1) + 3): 8
    //      array[1][3]: 8
    return 0;
}

*(array + i) == array[i];
*(*(array + i)) == array[i][j];
*(*(*(array + i) + j) +k) == array[i][j][k];

#include <cstdio>

int main(int argc, char const *argv[])
{
    int array[4][5] = {0};
    int (*p)[5] = array;
    int i, j, k = 0;
    for (i = 0; i < 4; i++)
    {
        for (j = 0; j < 5; j++)
        {
            array[i][j] = k++;
        }
    }
    printf("**(array + 1): %d\n", **(array + 1));
    printf("**(p + 1): %d\n", **(p + 1));
    printf("*(*(array + 1) + 3): %d\n", *(*(array + 1) + 3));
    printf("*(*(p + 1) + 3): %d\n", *(*(p + 1) + 3));
    printf("array[1][3]: %d\n", array[1][3]);
    printf("p[1][3]: %d\n", p[1][3]);
 

    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值