指针、进程空间、字符串

1、指针(续)

指针做函数参数,可以实现地址传递
//写一个函数,将主函数中的变量a、b的值进行交换
void swap(int *x, int *y)
{
    int temp = *x;
    *x = *y;
    *y = temp;
}

int fc(int *x)
{
    *x = 100;
    return 200;
}
        int a = 10;
        int b = 20;
        NSLog(@"a=%d,b=%d", a, b);
        swap(&a, &b);
        NSLog(@"a=%d,b=%d", a, b);

        x = 0;
        int y = fc(&x);
        NSLog(@"x=%d,y=%d", x, y);
指针作为函数返回值的类型
#import <Foundation/Foundation.h>

int* add(int *x)
{
    *x += 10;
    return x;
}

int* get1()
{
    int x = 12345;
    return &x;//返回一个局部变量的地址时,该局部变量会被释放,从而造成返回的地址变成无效地址
}

void get2()
{
    int y = 54321;
}

int* get3()
{
    static int x = 55555;
    return &x;
}

int main()
{
    @autoreleasepool {
        int a = 0;
        int *p1 = add(&a);
        NSLog(@"%d", *p1);

        p1 = get1();
        get2();
        NSLog(@"%d", *p1);

        p1 = get3();
        get2();
        NSLog(@"%d", *p1);
    }
    return 0;
}
指针加减
#import <Foundation/Foundation.h>

int main()
{
    @autoreleasepool {
        int a[5] = {10,20,30,40,50};

        int* p1 = &a[0];
        NSLog(@"%d", *p1);
        NSLog(@"%d", *(p1 + 1));
        NSLog(@"%d", *(p1 + 3));
        p1++;
        NSLog(@"%d", *p1);

        int* p2 = &a[4];
        NSLog(@"%d", *p2);
        NSLog(@"%d", *(p2 - 1));
        NSLog(@"%d", *(p2 - 3));
        p2--;
        NSLog(@"%d", *p2);

        int x = 100;//指针加减只能与数组配合使用,不能用于单个变量
        p1 = &x;

        p1 = &a[0];
        int f = *(p1++);
        NSLog(@"f=%d,*p1=%d", f, *p1);
        f = *(++p1);
        NSLog(@"f=%d,*p1=%d", f, *p1);
        f = (*p1)++;
        NSLog(@"f=%d,*p1=%d", f, *p1);
    }
    return 0;
}
指针变量的存储空间,与操作系统的位数有关,位数除以8就是指针的字节数
#import <Foundation/Foundation.h>

int main()
{
    @autoreleasepool {
        int* p1;
        NSLog(@"%lu", sizeof(p1));

        float* p2;
        NSLog(@"%lu", sizeof(p2));

        double* p3;
        NSLog(@"%lu", sizeof(p3));

        char* p4;
        NSLog(@"%lu", sizeof(p4));
    }
    return 0;
}
赋值,p[i]<=>*(p+i)
#import <Foundation/Foundation.h>

int main()
{
    @autoreleasepool {
        int a[10] = {1,2,3,4,5,6,7,8,9,0};
        int* p = a;

        NSLog(@"%d", a[0]);
        NSLog(@"%d", *p);
        NSLog(@"%d", a[1]);//a[1]<=>*(a+1)
        NSLog(@"%d", *(a + 1));
        NSLog(@"%d", *(p + 1));
        NSLog(@"%d", p[1]);//p[1]<=>*(p+1)

        for (int i = 0; i < 10; i++)
        {
            printf("%d ", p[i]);
        }
        printf("\n");

        p = a;
        for (int i = 0; i < 10; i++)
        {
            printf("%d ", *p++);
        }
        printf("\n");
        p = a;//此句不可或缺
        for (int i = 0; i < 10; i++)
        {
            //printf("%d ", *p);
            //p++;
            printf("%d ", p[0]);//p[0]<=>*(p+0)<=>*p
            p++;
        }
        printf("\n");

        for (int i = 0; i < 10; i++)
        {
            printf("%d ", *(a + i));//a[i]
            //printf("%d ", *a++);//常量数组名a不能做左值
        }
        printf("\n");

        for (int i = 0; i < 10; i++)
        {
            //printf("%d ", a[i]);//a[i]<=>*(a+i)
            printf("%d ", i[a]);//i[a]<=>*(i+a)
        }
        printf("\n");
    }
    return 0;
}
数组做函数形参
#import <Foundation/Foundation.h>

void fa(int data[10])//数组做形参时,会被编译成指针。int* data;
{
    NSLog(@"%lu", sizeof(data));
}

void print(int* data, int size)
{
    for (int i = 0; i < size; i++)
    {
        printf("%d ", data[i]);
    }
    printf("\n");
}

//写一个函数,将传过来的数组的每个元素加1
void increment(int *a, int size)
{
    for (int i = 0; i < size; i++)
    {
        a[i]++;
    }
    print(a, size);
}

int main()
{
    @autoreleasepool {
        int a[10] = {1,2,3,4,5,6,7,8,9,0};
        NSLog(@"%lu", sizeof(a));
        fa(a);
        print(a, 10);
        int b[5] = {10,20,30,40,50};
        print(b, 5);

        increment(a, 10);
        print(a, 10);
        increment(b, 5);
        print(b, 5);

        //C99新增的数组常量
        increment((int[5]){11,22,33,44,55}, 5);
        int *d = (int[10]){1,2,3,4,5,6,7,8,9,0};
        print(d, 10);
    }
    return 0;
}

2、进程空间

进程:正在运行的可执行文件
进程的内存空间
    代码区:程序的源代码(机器码)、常量(只读的)
    数据区:全局变量和静态变量
    堆区:malloc申请的空间
    栈区:局部变量、形参

3、字符串

用双引号引起来的多个字符
存储方式
    字符数组
    字符指针
        char str1[] = "Hello";
        str1[0] = 'h';
        //str1 = "world";

        char *str2 = "Hello";
        //str2[0] = 'h';
        str2 = "world";
字符串的键盘输入
        char str[100];
        //char* str1;
        NSLog(@"请输入一个字符串:");
        scanf("%s", str);
        NSLog(@"%s", str);
字符串的库函数
#import <Foundation/Foundation.h>

int main()
{
    @autoreleasepool {
        //字符串的赋值
        char str1[100] = "Hello";
        char str2[100];
        strcpy(str2, str1);
        NSLog(@"%s", str2);

        char str3[5];
        //strcpy(str3, str1);
        //NSLog(@"%s", str3);
        strncpy(str3, str1, 4);
        NSLog(@"%s", str3);

        //求字符串长度
        NSLog(@"%lu", strlen(str1));

        //字符串拼接
        strcpy(str1, "Hello ");
        strcpy(str2, "world!");
        strcat(str1, str2);
        NSLog(@"%s", str1);
        strcpy(str3, "he");
        //strcat(str3, str2);
        strncat(str3, str2, 2);
        NSLog(@"%s", str3);

        //字符串比较
        strcpy(str1, "Hello");
        if (strcmp(str1, "Hello") == 0)
        {
            NSLog(@"两个字符串相同");
        }
    }
    return 0;
}

思考练习

1、从键盘输入一个密码,显示密码强度
包含大小写字母数字还有其他字符的及密码长度大于12,为极强
包含大小写字符数字其他字符但长度长度小于12为强
包含大小写字母数字其他字符中的三种为中等为中等
包含大小写字母数字其他字符中的两种一下为弱

|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
解析

#import <Foundation/Foundation.h>

int main()
{
    @autoreleasepool {
        char password[100];
        NSLog(@"请输入一个密码:");
        scanf("%s", password);

        int upper = 0;//大写字母
        int lower = 0;//小写字母
        int digit = 0;//数字
        int sign = 0;//其他字符
        int length = 0;//密码长度
        for (int i = 0; password[i] != '\0'; i++)
        {
            if (password[i] >= 'A' && password[i] <= 'Z')
            {
                upper = 1;
            }
            else if (password[i] >= 'a' && password[i] <= 'z')
            {
                lower = 1;
            }
            else if (password[i] >= '0' && password[i] <= '9')
            {
                digit = 1;
            }
            else
            {
                sign = 1;
            }
            length++;
        }
        int sum = upper + lower + digit + sign;
        if (sum == 4 && length > 11)
        {
            NSLog(@"极强");
        }
        else if (sum == 4)
        {
            NSLog(@"强");
        }
        else if (sum == 3)
        {
            NSLog(@"中等");
        }
        else if (sum == 2 || sum == 1)
        {
            NSLog(@"弱");
        }
    }
    return 0;
}
发布了52 篇原创文章 · 获赞 5 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/shuan9999/article/details/52324888