依稀记得10年前的大学第一门课就是它——c语言,而指针作为c语言的核心之一,当时一直难以理解,从而转战java。而今10余年过去了,依然还是放不下它,也同时为了阅读openJDK源码做准备,就重新拾起。其实就是因为喜欢研究底层这个癖好,才让我走上了不归路。没办法,重新拾起c语言基础。
其实对技术学习本来就应该是兴趣驱动,但目前太多人学习技术都过于功利,工作中用什么学什么,失去了自己对技术的追求,我又何尝不是呢?其实都是为了生存罢了。
不再感慨了,下面一小段代码,总结了指针的基础:
#include <stdio.h>
#include <stdlib.h>
struct Person
{
char name;
int age;
};
int test(int a,int b) {
printf("%d %d\n", a, b);
return a + b;
};
int test2(int a,int b) {
printf("%d %d\n", a, b);
return a - b;
};
void test3(struct Person persons) {
persons.age = 10;
}
void test4(struct Person *persons_p) {
persons_p->age = 10;
}
int main() {
int a = 10;
int *pInt = &a;
printf("%p\n", &a);
printf("%p\n", pInt);
printf("%p\n", &pInt);
printf("%d\n", *pInt);
int *p = 0x11223344;
int a1 = p;
printf("%p\n", a1);
printf("%p\n", &a1);
printf("%p\n", &p);
int **pInt1 = &pInt;
printf("*pInt1:%p\n", *pInt1);
printf("**pInt1:%d\n", *(*pInt1));
printf("%p\n", &pInt1);
const int * c = 0x321312;
int * const c1 = 1;
const int * const c2 = 2;
/**
* 函数指针
*/
int (*p_test) (int, int);
p_test = test(1,2);
p_test = test2(1,2);
printf("p_test:%d\n", p_test);
int *t = 0x11223344;
char s = (char *)t;
long double *d = (long double *) t;
printf("%p\n", s);
printf("%p\n", d);
char name[] = "test";
char i = *(name + 1);
printf("%c\n", i);
struct Person person1;
person1.age = 1;
test3(person1);
printf("%d\n", person1.age);
struct Person person2;
person2.age = 1;
test4(&person2);
printf("%d\n", person2.age);
return 0;
}
本文这小段代码挺小白的,因为对于c语言来说,我也是个小白。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)