- int *p[4] ; // มีความหมายเท่ากับการประกาศอาเรย์ ที่มีสมาชิก 4 ตัว แต่ละตัวคือพอร์ยเตอร์ของ int . เช่น
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 charactersint *p[4]; int a = 1 ,b =2,c =3,d=4; p[0] = &a; p[1] = &b; p[2] = &c; p[3] = &d; printf("%d, %d , %d ,%d ",*p[0],*p[1],*p[2],*p[3]); - int (*p)[4]; // มีความหมายเท่ากับการประกาศพอร์ยเตอร์ 1 ตัวเท่านั้น แต่ขนาดของมันไม่ใช่ขนาดของ int แต่มีขนาดเท่ากับ 4 int. เช่น การบวกพอร์ยเตอร์ไป 1 หน่วย มันจะทำการก้าวไปอยู่ตำแหน่งที่ 4 x ขนาดของ int ถ้าเป็นในตัวอย่างก็คือ 4 x 4 = 16. ดังนั้น ถ้า &p เริ่มต้นที่ 1000 . ดังนั้น p++ จะย้ายไปอยู่ที่ 1016 (ไม่ใช่ 1004 ). ดังนั้นเราจึงใช้การประกาศแบบนี้ สำหรับอาเรย์สองมิติ เพื่อเราสามารถทำการเข้าถึงอาเรย์แบบ row และ column
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> | |
#define ROW 3 | |
#define COL 4 | |
int sumM(int p[][COL] ); | |
int main(void) | |
{ | |
int matrix[ROW][COL] = | |
{ | |
{1, 2, 3, 4}, | |
{5, 6, 7, 8}, | |
{9, 10,11,12}, | |
}; | |
int i , j , sum = 0; | |
for(i = 0 ; i < ROW ; i++) | |
{ | |
for(j = 0 ;j < COL ; j++) | |
{ | |
sum+= matrix[i][j]; | |
} | |
} | |
printf("%d , %d\n",sum,sumM(matrix)); | |
return 0; | |
} | |
int sumM(int p[][COL] ) | |
{ | |
int i,j, sum; | |
for(i = 0 ; i < ROW ; i++) | |
{ | |
for(j=0;j< COL ;j++) | |
{ | |
sum += p[i][j]; | |
} | |
} | |
return sum; | |
} |