企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持知识库和私有化部署方案 广告
[TOC] # 间接赋值 ~~~ void changeValue(int *p) { *p = 100; } int main() { int a = 10; changeValue(&a); printf("%d\n", a); //100 getchar(); return 0; } ~~~ # 指针做参数输入 ~~~ #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; } ~~~