一维数组,二维数组,三维数组,数组与指针,结构体数组,通过改变指针类型改变访问数组的方式
打印數(shù)組中的每個(gè)元素,打印每個(gè)元素的地址:
#include <stdio.h>
#include <stdlib.h>
?
void main(void)
{
??? int a[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
??? for (int *p = a; p < a + 10;p++)? //指針類(lèi)型決定4個(gè)字節(jié)
??? {
??????? printf("\n%p,%d", p, *p);
??? }
??? getchar();
}
指針數(shù)組
#include <stdio.h>
#include <stdlib.h>
?
void main(void)
{
??? //輪詢(xún)數(shù)組的時(shí)候,可以用指針輪詢(xún)
??? //通過(guò)指針數(shù)組,可以管理地址
??? char *str[5] = { "calc", "notepad", "tasklist", "pause", "mspaint" };
??? for (char **pp = str; pp < str + 5;pp++)
??? {
??????? system(*pp);
??? }
??? getchar();
}
數(shù)組指針(等價(jià)于二維數(shù)組)
#include<stdio.h>
#include<stdlib.h>
int main(int argc, char *argv[])
{
??? //二維數(shù)組,等價(jià)于二級(jí)指針
??? char str[5][10] = { "calc", "notepad", "tasklist", "pause", "mspaint" };
??? printf("%p",str);
??? for (char(*p)[10] = str; p < str + 5;p++)
??? {
??????? //打印地址?? 字符串
??????? printf("\n%p,%s",p,p);
??????? system((char *)p);
??? }
??? return 0;
}
二維數(shù)組
#include<stdio.h>
#include<stdlib.h>
int main(int argc,char *argv[])
{
??? char str[5][10] = { "calc", "notepad", "tasklist", "pause", "mspaint" };
??? //指針地址一樣,但是類(lèi)型不一樣
??? //str代表行地址,&str代表整個(gè)數(shù)組的地址,*str就是第一個(gè)字符的地址
??? printf("%p,%p,%p",str,&str,*str);
?
??? system("pause");
??? return 0;
}
二維數(shù)組的開(kāi)辟方式已經(jīng)打印:
#include<stdio.h>
#include<stdlib.h>
?
int main(int argc,char *argv[])
{
??? //開(kāi)辟二維數(shù)組的空間
??? int **p;
??? p = (int **)malloc(sizeof(int)* 10);
??? int i,j;
??? for (i = 0; i < 10;i++)
??? {
??????? p[i] = (int *)malloc(sizeof(int)* 10);
??? }
??? //初始化數(shù)組
??? for (i = 0; i < 10; i++)
??? {
??????? for (j = 0; j < 10; j++)
??????? {
??????????? *(*(p + i) + j) = i * j;
??????? }
??????? putchar(10);
??? }
?
??? //通過(guò)指針的方式打印出二維數(shù)組
??? for (i = 0; i < 10;i++)
??? {
??????? for (j = 0; j < 10;j++)
??????? {
??????????? printf("%d ", *(*(p + i) + j));
??????? }
??????? putchar(10);
??? }
?
??? system("pause");
??? return 0;
}
指針的加減法:
#include <stdio.h>
#include<stdlib.h>
?
int main(int argc,char *argv[])
{
??? //c語(yǔ)言運(yùn)算規(guī)則,加法原則實(shí)際加上的是:元素的大小*加上的數(shù)
??? //減法原則實(shí)際上減去的是:元素的大小*減去的數(shù)??????????????
??? int a[3][4] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };
??? printf("%p\n",a);
??? int *p1 = &a[0][0];
??? int *p2 = p1 + 4;
??? printf("%d\n",*p2);
??? int *p3 = p2 - 3;
??? printf("\n%d", *p3);
??? printf("\n%d", p2 - p3);
?
??? system("pause");
??? return 0;
}
二維數(shù)組的打印問(wèn)題
#include <stdio.h>
#include<stdlib.h>
?
int main(int argc,char *argv[])
{
??? //c語(yǔ)言運(yùn)算規(guī)則,加法原則實(shí)際加上的是:元素的大小*加上的數(shù)
??? //減法原則實(shí)際上減去的是:元素的大小*減去的數(shù)??????????????
??? int a[3][4] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };
??? //將數(shù)組中的每個(gè)元素一個(gè)個(gè)打印出來(lái)
??? for (int *p = &a[0][0]; p < &a[0][0] + 12; p++)
??? {
??????? //下面的一句實(shí)現(xiàn)的是每過(guò)4個(gè)元素?fù)Q行一下
??????? if ((p - &a[0][0]) % 4 == 0)
??????? {
??????????? printf("\n");
??????? }
??????? printf("%5d",*p);
??? }
??? printf("\n\n\n");
??? //a是一個(gè)常量的行指針,a的類(lèi)型與px等價(jià)
??? int(*px)[4] = a;
??? for (int i = 0; i < 3; i++)
??? {
??????? for (int j = 0; j < 4;j++)
??????? {
??????????? //printf("%5d",a[i][j]);???? //通過(guò)數(shù)組名的方式實(shí)現(xiàn)打印
??????????? //printf("%5d",px[i][j]);??? //通過(guò)指針下標(biāo)的方式實(shí)現(xiàn)
??????????? //printf("%5d",*(px[i]+ j));//通過(guò)指針的方式實(shí)現(xiàn)
??????????? printf("%5d",*(*(px+i)+j));? //通過(guò)下標(biāo)的方式實(shí)現(xiàn)
??????? }
??????? printf("\n");
??? }
?
??? system("pause");
??? return 0;
}
結(jié)構(gòu)體數(shù)組
#include<stdio.h>
#include<stdlib.h>
#include<Windows.h>
?
struct pos{
??? int x;
??? int y;
};
?
struct pos pos2[8] = {
??? { 100, 200 },
??? { 100, 0 },
??? { 200,400 },
??? { 300,600 },
??? { 390,600 },
??? { 190,900 },
??? { 990,100},
??? {1390,600}
};
?
struct pos pos1[8][2] = {
??? { { 200, 0 }, { 900, 800 } },
??? { { 0, 0 }, { 800, 1300 } },
??? { { 1500, 200 }, { 600, 900 } },
??? { { 800, 700 }, { 700, 800 } },
??? { { 300, 100 }, { 600, 700 } },
??? { { 900, 800 }, { 700, 700 } },
??? { { 100, 200 }, { 800, 800 } }
};
?
void main()
{
??? HWND *win = FindWindowA("Notepad++", "Notepad++");
??? if (win == NULL)
??? {
??????? return;
??? }
??? SetWindowPos(win, NULL, 0, 0, 100, 300, 1);
?
??? for (int i = 0; i < 8; i++)
??? {
??????? //設(shè)置窗口位置大小
??????? SetWindowPos(win, NULL, pos1[i][0].x, pos1[i][0].y, pos1[i][1].x, pos1[i][1].y, 1);
??????? Sleep(3000);
??? }
??? system("pause");
}
作為通過(guò)函數(shù)改變二維數(shù)組中各各參數(shù)的值,和打印相關(guān)的知識(shí)點(diǎn)
#include <stdio.h>
#include <stdlib.h>
/************************************************************************/
/* 一維數(shù)組沒(méi)有副本機(jī)制,二維數(shù)組也沒(méi)有,數(shù)組作為參數(shù)都是傳遞地址?????? */
/************************************************************************/
int searchmax(int a[3][4])
{
??? //通過(guò)數(shù)組傳遞過(guò)來(lái)的數(shù)組,求大小的時(shí)候都是4(32位系統(tǒng)情況)
??? printf("\nsearch = %d",sizeof(a));
??? int b[3][4] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2 };
??? printf("\nsearch b = %d",sizeof(b));
???
??? //下面開(kāi)始求數(shù)組的最大值
??? int max;?? //存儲(chǔ)最大的值
??? max = a[0][0];
??? for (int i = 0; i < 3;i++)
??? {
??????? for (int j = 0; j < 4;j++)
??????? {
??????????? if (a[i][j] > max)?? //比較大小
??????????? {
??????????????? //接收最大的地址
??????????????? max = a[i][j];
??????????? }
??????? }
??? }
??? return max;
}
?
int main(int argc, char *argv[])
{
??? int a[3][4] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2 };
??? int max = searchmax(a);
?
??? printf("\n二維數(shù)組的最大值為:%d\n",max);
?
??? system("pause");
??? return 0;
}
求二維數(shù)組中元素的最小值
#include <stdio.h>
#include<stdlib.h>
int searchmin(int(*p)[4])
{
??? //假設(shè)第一個(gè)是最小的
??? //int min = p[0][0];
??? //同樣可以使用下面的方式實(shí)現(xiàn)
??? int min = *(*(p + 0) + 0);
?
??? for (int i = 0; i < 3;i++)
??? {
??????? for (int j = 0; j < 4;j++)
??????? {
??????????? //第一種方式通過(guò)數(shù)組下標(biāo)的方式實(shí)現(xiàn)
??????????? //if (p[i][j] <min)? //比較大小
??????????? //{
??????????? //? min = p[i][j];?//求出最小的數(shù)
??????????? //}
?
??????????? if (*(*(p +i) + j) < min)
??????????? {
??????????????? min = *(*(p + i) + j);
??????????? }
??????? }
??? }
??? return min;
}
?
int main(int argc,char *argv[])
{
??? int a[3][4] = { 1, 2, 3, 4, 5, 16, -7, 8, 9, 10, 1, 2 };
?
??? printf("\nmin = %d\n",searchmin(a));
?
??? system("pause");
??? return 0;
}
通過(guò)指針的方式實(shí)現(xiàn)求二維數(shù)組中的最大值
#include <stdio.h>
#include<stdlib.h>
?
/************************************************************************/
/* 二維數(shù)組退化為一個(gè)指向有4個(gè)元素的數(shù)組的指針?????????????????????????*/
/************************************************************************/
static int searchmax(int(*p)[4])
{
??? //存儲(chǔ)最大值
??? int max = p[0][0];
??? for (int i = 0; i < 3; i++)
??? {
??????? for (int j = 0; j < 4;j++)
??????? {
??????????? if (p[i][j] > max)
??????????? {
??????????????? max = p[i][j];
??????????? }
??? ??? }
??? }
??? return max;
}
?
int main(int argc,char *argv[])
{
??? int a[3][4] = { 1, 2, 3, 4, 5, 16, -7, 8, 9, 10, 1, 2 };
?
??? printf("\nmin = %d\n",searchmax(a));
?
??? system("pause");
??? return 0;
}
12.三維數(shù)組定義,并通過(guò)數(shù)組的方式打印出來(lái)
#include <stdio.h>
#include<stdlib.h>
?
void printArray(int a[3][4][5])
{
??? int i,j,k;
??? for (i = 0; i < 3;i++)
??? {
??????? for (j = 0; j < 4;j++)
??????? {
??????????? for (k = 0; k < 5;k++)
??????????? {
??????????????? printf("%4d",a[i][j][k]);
??????????? }
??????????? printf("\n");
??????? }
??????? printf("\n\n\n");
??? }
}
?
int main(int argc, char *argv[])
{
??? int a[3][4][5];
??? int num = 0;
??? //求得數(shù)組的大小為240
??? printf("%d\n",sizeof(a));
?
??? //線(xiàn)性初始化
??? for (int *p = &a[0][0][0]; p < &a[0][0][0] + 60;p++)
??? {
??????? *p = num;
??????? num++;
??? }
?
??? printArray(a);
?
??? system("pause");
??? return 0;
}
13.三維數(shù)組定義,并通過(guò)指針的方式打印出來(lái)
#include<stdio.h>
#include<stdlib.h>
?
static void printArray(int (*p)[4][5])
{
??? int i, j, k;
??? for (i = 0; i < 3;i++)
??? {
??????? for (j = 0; j < 4;j++)
??????? {
??????????? for (k = 0; k < 5;k++)
??????????? {
??????????????? //printf("%4d",p[i][j][k]);
??????????????? printf("%4d",*(*(*(p+i)+j)+k));
??????????? }
??????????? printf("\n");
??????? }
??????? printf("\n\n\n");
??? }
}
?
int main(int argc, char *argv[])
{
??? int a[3][4][5];
??? int num = 0;
??? //線(xiàn)性初始化
??? for (int *p = &a[0][0][0]; p < &a[0][0][0] + 60;p++)
??? {
??????? *p = num;
??????? num++;
??? }
???
??? printArray(a);
??? system("pause");
??? return 0;
}
14.通過(guò)指針類(lèi)型改變?cè)L問(wèn)數(shù)組訪(fǎng)問(wèn)
#include<stdio.h>
#include<stdlib.h>
?
int main(int argc,char *argv[])
{
??? //創(chuàng)建一維數(shù)組
??? int *p = (int *)malloc(sizeof(int)* 40);
??? for (int *px = p, i = 0; px < p + 40; px++,i++)
??? {
??????? //賦值
??????? *px = i;
??????? //指針循環(huán)
??????? //printf("%d,%p\n",*px,px);
??? }
?
??? int b[5][8];
??? printf("\n\n\n");
??? //指針類(lèi)型決定了訪(fǎng)問(wèn)的方式
??? int(*pp)[8] = (int(*)[8])p;
??? for (int i = 0; i < 5;i++)
??? {
??????? for (int j = 0; j < 8;j++)
??????? {
??????????? //printf("%5d",pp[i][j]);打印數(shù)據(jù)
??????????? printf("%5d",*(*(pp+i)+j)); //pp[i][j]
??????? }
??????? printf("\n");
??? }
?
??? printf("\n\n\n");
??? int(*ppp)[2][5] = (int(*)[2][5])p;
??? for (int i = 0; i < 4; i++)
??? {
??????? for (int j = 0; j < 2;j++)
??????? {
??????????? for (int k = 0; k < 5;k++)
??????????? {
??????????????? //打印元素
??????????????? //printf("%5d",ppp[i][j][k]);
??????????????? printf("%5d",*(*(*(ppp+i)+j) + k));
??????????? }
??????????? printf("\n");
??????? }
??????? printf("\n\n\n");
??? }
?
??? system("pause");
??? return 0;
}
?
總結(jié):下面左邊定義的數(shù)組和右邊的指針是等價(jià)的
| 數(shù)組 | 指針 | 數(shù)組訪(fǎng)問(wèn) | 指針訪(fǎng)問(wèn) |
| int a[i] | int *p | a[i] | *(p + i) |
| int a[i][j] | int (*p)[j] | a[i][j] | *(*(p+i)+j) |
| int a[i][j][k] | int (*p)[j][k] | a[i][j][k] | *(*(*(p+i)+j)+k) |
| int a[i][j][k][l] | int (*p)[i][j][k] | a[i][j][k][l] | *(*(*(*(p+i)+j)+k)+l) |
?
總結(jié)
以上是生活随笔為你收集整理的一维数组,二维数组,三维数组,数组与指针,结构体数组,通过改变指针类型改变访问数组的方式的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 银行卡安全码是什么
- 下一篇: malloc,colloc,reallo