- int *p กับ int* p (เว้นช่องไฟไม่เหมือนกัน) เหมือนกันทุกประการ จะต่างกันตรงที่การอ่านและความเข้าใจ เช่น
int* p, q ;
// ความหมายคือ int *p; int q; แต่บางทีอาจทำให้เข้าใจผิดได้ว่าเป็น int* p ; int* q; ซึ่งแบบหลังนี้ไม่ถูก
int *p, q ;
// ความหมายก็เหมือนกับข้างบน แต่บางทีก็ทำให้ดูคล้ายเป็นการ deference ( ซึ่งไม่ใช่ )
- การประกาศ pointer to pointer ก็คือการใช้ * ตามจำนวนชั้นของพอร์ยเตอร์นั้น เช่น
int a = 4;
int *p = &a;
int **q = &p;
int ***r = &q;
int ****s = &r;
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
int main(void) | |
{ | |
int a = 3; | |
int *p = &a; | |
int **q = &p; | |
printf("a --------- %d\n",a); | |
printf("&a -------- %d\n",&a); | |
printf("p -------- %d\n",p); | |
printf("*p -------- %d\n",*p); | |
printf("&p -------- %d\n",&p); | |
printf("q --------- %d\n",q); | |
printf("*q -------- %d\n",*q); | |
printf("**q ------- %d\n",**q); | |
return 0; | |
} |