日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

c++指针数组(入门)

發布時間:2024/1/18 c/c++ 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 c++指针数组(入门) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

指針數組的本質是數組,只是每個數組的元素為指針

32位平臺:

char*arr1[4]; int *arr2[4]; short *arr3[4];sizeof(arr1)==168; sizeof(arr2)==168; sizeof(arr3)==168;

8.4.1 數值的指針數組

int *arr[4]; 指針數組

int *arr[4]={&num1,&num2,&num3,&num4};

? ? ? ? ? ?

int num1=10; int num2=20; int num3=30; int num4=40; //int arr[4] //int *p; int *arr[4]={&num1.&num2,&num3,&num4}; int n=sizeof(arr)/sizeof(arr[0]); int i; //遍歷輸出 for(int i=0;i<n;i++){ cout<<arr[i]<<" "; //10 20 30 40 } cout<<endl;

? ? ? ? ? ? ? ? ? ? ? ? ?

8.4.2 字符指針數組

char*arr[4]={"hehehehe","xixixixi","lalalala","hahahaha"};

arr[0]存放的是hehehehe的首元素地址,存放在棧區/全局區(看這個數組是全局還是局部),同時,"hehehehe"被存放在文字長量區。注意:文字長量區的值不可被修改

char*arr[4]={"hehehehe","xixixixi","lalalala","hahahaha"}; int n=sizeof(arr)/sizeof(arr[0]); int i; for(i=0;i<n;i++){ cout<<arr[i]<<endl; //這里輸出不需要加*號,arr[i]它保存的就是字符串首元素地址,輸出結果為 hehehehexixixixilalalala hahahaha} // cout<<*arr[i]; 輸出的就是第0個元素內容,就是 h,x,l,h;// cout<<*(arr[i]+3)<<endl; 這里輸出的是字符串第三個元素,e,i,a,a

8.4.3 二維字符數組

char*arr1[4]={"hehehehe","xixixixi","lalalala","hahahaha"}; char arr2[4][128]={"hehehehe","xixixixi","lalalala","hahahaha"};

arr1是在指針數組 存放的是每個字符串的首元素地址

arr2是二維數組? ? 存放的是每個字符串

8.5 指針的指針

int num=10; int *p=&num; int **q=&p;

如果想要保存p的地址,則需要兩個*號

n級變量可以保存n-1級指針變量的地址

8.6數組指針

int arr[5]={10,20,30,40,50}; int (*p)[5];//數組指針,注意()不能去掉,去掉就和5結合 #include<iostream> using namespace std; void test1() {int arr[5] = { 10,20,30,40,50 };int(*p)[5] = &arr;//數組指針cout << "sizeof(p)=" << sizeof(p) << endl;cout << "p=" << p << endl;cout << "p+1=" << p + 1; }int main() {test1();return 0;}

??

int arr[5]={10,20,30,40,50}; int(*P)[5]=&arr; //數組指針

*p==*&arr==arr;*和&抵消

對數組首地址取*==數組首元素地址

注意:若想取arr[2]內容,必須*(*p+2)=*(*(p+0)+2)==*(p[0]+2)==p[0][2]

8.6.2二維數組和數組指針的關系

int arr[3][4]={{1,2,3,4},{5,6,7,8},{9,10,11,12}};

arr[0] (值為1,2,3,4)
arr[1]? (5,6,7,8)
arr[2]? ?(9,10,11,12)

arr[1]=>*(arr+1) //第1行第0列的列地址 &arr[1]=>arr+1 //第1行的行地址 *arr+1=>//第0行第1列的列地址 arr[1]+2=>*(arr+1)+2=>//第1行第2列的列地址 #include<iostream> using namespace std; int main() {int arr[3][4] = { {1,2,3,4},{5,6,7,8},{9,10,11,12} };int row = sizeof(arr) / sizeof(arr[0]); //行為3int col = sizeof(arr[0]) / sizeof(arr[0][0]);//列為4int* p = &arr[0][0];int i = 0;for (int i = 0; i < row * col; i++) { //依次輸出arr[i]cout << p[i] << " ";}return 0; }

?8.8 指針與函數

8.8.1 指針變量作為函數的參數

如果想在函數內部 修改外部變量的值 需要將外部變量的地址 傳遞給函數(重要)

案列1 單向傳遞之值傳遞

函數內部 不能修改外部變量的值

#include<iostream> using namespace std; void setNum01(int data) { data=100; } void test1(){ int num=0; setNum01(num);//單向傳遞之 傳值 cout<<"num="<<num<<endl; //輸出結果為0.修改不成功 } int main(int argc,char*argv[]) { test1(); return 0; }

案例2 單向傳遞之傳地址

函數內部就可以修改外部變量的值

void setNum02(int *p)//int *p=&num; { *p=100; } void test2(){int num=0; setNum02(&num);//單向傳遞之傳地址 cout<<"num="<<num<<endl; //結果為100 }

8.8.2 一維數組作為函數的參數

函數內部想操作(讀或寫)外部數組元素,將數組名傳遞給函數

注意:一維數組 作為函數的形參 會被優化成指針變(面試中常用)

//void outIntArray(int arr[5], int n) { // 注意:一維數組作為函數的參數 會被編譯器 優化成 指針變量void outIntArray(int *arr, int n) {cout << "內部sizeof(arr)=" << sizeof(arr) << endl; for (int i = 0; i < n; i++) {cout << arr[i] << " ";} } void test02() {int arr[5] = {10,20,30,40,50};int n = sizeof(arr) / sizeof(arr[0]);cout << "外部sizeof(arr)=" << sizeof(arr) << endl; //20B//遍歷數組:outIntArray(arr,n);// 10 20 30 40 50 }int main() {test02();return 0;}

?8.8.3 二維數組作為函數的參數

函數內部 想操作 函數外部的二維數組 需要將二維數組名 傳遞給函數

二維數組 作為函數的形參 會被優化成 一維數組

//void outputIntArray(int arr[3][4], int row, int col) { //二維數組作為函數的形參 會被優化成 一維數組指針void outputIntArray(int ( * arr)[4],int row,int col){cout << "內部sizeof(arr)=" << sizeof(arr) << endl;for (int i = 0; i < row; i++) {for (int j = 0; j < col; j++) {cout << arr[i][j] << " ";}cout << endl;} } void test03() {int arr[3][4] = { 1,2,3,4,5,6,7,8,9,10,11,12 };int row = sizeof(arr) / sizeof(arr[0]);int col = sizeof(arr[0]) / sizeof(arr[0][0]);cout << "外部sizeof(arr)=" << sizeof(arr) << endl;//遍歷數組outputIntArray(arr, row, col);}int main() {test03();return 0;}

8.8.4 函數的返回值類型為 指針類型

將函數內部的合法地址 通過返回值 返回給函數外部

int* getAddr(void) {int data = 20;//data在函數內部,是局部變量,在函數被調用之后會被釋放//*p就等于在訪問一個被釋放的空間,這時候就會出現段錯,操作非法空間return &data; //不要返回普通局部變量的地址 } void test04() {int* p = NULL;p = getAddr();cout << "p=" << *p;//結果會出現段錯,輸出不了 }int main() {test04();return 0;}

修改之后為:

int* getAddr(void) {//int data = 20;//data在函數內部,是局部變量,在函數被調用之后會被釋放//*p就等于在訪問一個被釋放的空間,這時候就會出現段錯,操作非法空間static int data = 20;//添加局部靜態變量staticreturn &data; //不要返回普通局部變量的地址 } void test04() {int* p = NULL;p = getAddr();cout << "p=" << *p;//20 }int main() {test04();return 0;}

? 8.9 函數指針

?函數名 代表函數的入口地址:

函數指針:本質是一個指針變量 只是改變量 保存的是函數的入口地址

int myAdd(int x, int y) {return x + y; } void test05() {int(*p)(int x, int y) = NULL;//函數指針和函數入口地址建立關系p = myAdd;//通過p調用myAdd函數(函數+(實參))cout << p(10, 20); //30 }int main() {test05();return 0;}

8.9.2 函數指針變量注意:

函數指針變量不要+1 無意義

不要對函數指針變量取*

8.9.3? 函數指針變量 使用typedef定義

int myAdd(int x, int y) {return x + y; } void test05() {typedef int(*FUN_abs)(int x, int y);FUN_abs p = NULL;//函數指針和函數入口地址建立關系p = myAdd;//通過p調用myAdd函數(函數+(實參))cout << p(10, 20); //30 }int main() {test05();return 0;}

8.9.4 函數指針作為函數的參數

目的:讓算法功能多樣化

案例1:設計一個算法,完成加減乘除

int myAdd(int x, int y) {return x + y; } int mysub(int x, int y) {return x - y; } int mymul(int x, int y) {return x * y; } int mydiv(int x, int y) {return x / y; } int choice(int x, int y, int (*func)(int, int)) {return func(x, y); } void test07() {cout << choice(20, 30, myAdd) << endl;cout << choice(20, 30, mysub) << endl;cout << choice(20, 30, mymul) << endl;cout << choice(20, 30, mydiv) << endl; }int main() {test07();return 0;}

總結

以上是生活随笔為你收集整理的c++指针数组(入门)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。