#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct _proc {
char **argv;
} PROC;
void test(PROC * this)
{
/* 申请指针数组空间 */
if (!(this->argv = (char**)malloc(sizeof(char *) * 3))) {
perror("malloc");
exit(1);
}
/* argv[0] */
if (!(this->argv[0] = malloc(0x40))) {
perror("malloc");
exit(1);
}
printf("this->argv = %p:%p\n", &this->argv, this->argv); // 0x7fffd65d9de0:0x5604d873e2a0
printf("this->argv[0] = %p:%p\n", &this->argv[0], this->argv[0]); // 0x5604d873e2a0:0x5604d873e2c0
memcpy(this->argv[0], "ac-test0", 8);
printf("argv[0]=%s\n", this->argv[0]);
/* argv[1] */
if (!(this->argv[1] = malloc(0x40))) {
perror("malloc");
exit(1);
}
printf("this->argv[1] = %p:%p\n", &this->argv[1], this->argv[1]); // 0x5564a05f42a8:0x5564a05f4720
memcpy(this->argv[1], "ac-test1", 8);
printf("argv[0]=%s\n", this->argv[1]);
/* argv[2] */
if (!(this->argv[2] = malloc(0x40))) {
perror("malloc");
exit(1);
}
printf("this->argv[2] = %p:%p\n", &this->argv[2], this->argv[2]); // 0x5564a05f42b0:0x5564a05f4770
memcpy(this->argv[2], "ac-test2", 8);
printf("argv[0]=%s\n", this->argv[2]);
/* argv[3] */
if (!(this->argv[3] = malloc(0x40))) {
perror("malloc");
exit(1);
}
printf("this->argv[3] = %p:%p\n", &this->argv[3], this->argv[3]); // 0x5564a05f42b8:0x5564a05f47c0
memcpy(this->argv[3], "ac-test3", 8);
printf("argv[0]=%s\n", this->argv[3]);
free(this->argv);
free(this->argv[0]);
free(this->argv[1]);
free(this->argv[2]);
free(this->argv[3]);
}
int main()
{
PROC this = {0};
test(&this);
return 0;
}
本站资源均来自互联网,仅供研究学习,禁止违法使用和商用,产生法律纠纷本站概不负责!如果侵犯了您的权益请与我们联系!
转载请注明出处: 免费源码网-免费的源码资源网站 » c语言之 **指针
发表评论 取消回复