间接赋值

  1. void changeValue(int *p)
  2. {
  3. *p = 100;
  4. }
  5. int main() {
  6. int a = 10;
  7. changeValue(&a);
  8. printf("%d\n", a); //100
  9. getchar();
  10. return 0;
  11. }

指针做参数输入

#include <iostream>
#include <stddef.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
void printString(const char *str) {
    printf("打印内容: %s\n", str);
}
void printStringArray(char **arr, int len)
{
    //arr[0]是char *类型的
    for (int i = 0; i < len; ++i) {
        printf("%s\n", arr[i]);
    }
}
int main() {
    //堆上分配内存
    char * s = (char *)malloc(sizeof(char) * 100);
    memset(s, 0, 100);
    strcpy(s, "hello world");
    printString(s);
    //栈上分配内存
    char * strs[] = {
            "aaaaa",
            "bbbb",
            "ccc",
            "dddd",
    };
    int len = sizeof(strs) / sizeof(strs[0]);
    printStringArray(strs, len);
    getchar();
    return 0;
}

指针做输出

#include <iostream>
#include <stddef.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
void allocateSpace(char **temp)
{
    char *p = (char *)malloc(100);
    memset(p, 0, 100);
    strcpy(p, "hello world");
    //指针的间接赋值
    *temp = p;
}
int main() {
    char *p = NULL;
    allocateSpace(&p);
    getchar();
    return 0;
}