Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- WK Shearer, April 2012. This is a revision of pointers in C. Being largely a C++ guy myself,
- I ran into some confusion with pointer handling in C, which does not use dereferencing like C++.
- Here it is for clarificaction, in a pure C file.
- */
- #include <stdio.h>
- struct test
- {
- int i_snum;
- };
- void func_1(int*);
- void func_2(int*);
- void func_3(struct test*);
- void func_4(struct test**);
- //void func_5(int&);
- void func_1(int *ptr)
- {
- *ptr += 10;
- printf("i_num is %d\n", *ptr);
- func_2(ptr);
- }
- void func_2(int *ptr1)
- {
- *ptr1 += 10;
- }
- void func_3(struct test *p_struct)
- {
- p_struct->i_snum += 10;
- }
- void func_4(struct test **p_struct)
- {
- (*p_struct)->i_snum += 100;
- printf("struct_test.i_snum = %d\n", (*p_struct)->i_snum);
- (**p_struct).i_snum += 50;
- }
- // This is the C++ style that DOES NOT work.
- /*
- void func_5(int &num)
- {
- num += 1;
- }
- */
- int main()
- {
- int i_num;
- int *p_num;
- p_num = &i_num;
- *p_num = 14;
- printf("i_num is %d\n", i_num);
- func_1(p_num);
- printf("i_num is %d\n", i_num);
- func_1(&i_num);
- printf("i_num is %d\n", i_num);
- // func_5(i_num);
- // printf("i_num is %d\n", i_num);
- struct test struct_test;
- struct test *p_test;
- p_test = &struct_test;
- p_test->i_snum = 100;
- printf("struct_test.i_snum = %d\n", struct_test.i_snum);
- func_3(p_test);
- printf("struct_test.i_snum = %d\n", struct_test.i_snum);
- func_3(&struct_test);
- printf("struct_test.i_snum = %d\n", struct_test.i_snum);
- func_4(&p_test);
- printf("struct_test.i_snum = %d\n", struct_test.i_snum);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement