青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

glxhyt

  C++博客 :: 首頁(yè) :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
  15 隨筆 :: 0 文章 :: 4 評(píng)論 :: 0 Trackbacks
    入職一年了,這一年自己學(xué)到許多,但是忘記也很多,于是決定定下心來整理以前學(xué)到的,并且繼續(xù)學(xué)習(xí)
    
    二維數(shù)組和二級(jí)指針,這真是頭疼的問題,困擾了我好幾次,
   
    先轉(zhuǎn)一下wanpengcoder的二維數(shù)組和二級(jí)指針

前兩天寫個(gè)程序,傳參數(shù)的時(shí)候想傳個(gè)二維數(shù)組進(jìn)去,結(jié)果悲劇了,函數(shù)寫成Fun (int **p){},原來沒有這么寫過,

以為這么寫也是對(duì)的,結(jié)果錯(cuò)了,查了些資料,做個(gè)總結(jié)。

Fun (int **p){}這里面的int **p //這里的p不是二維數(shù)組的指針,而是指向指針的指針,即二級(jí)指針。

正確的二維數(shù)組的指針應(yīng)該是:Int a[2][2];Int (*p)[2];//定義時(shí)無論數(shù)組維數(shù),只可忽略第一維

例如:int a[2][2]={0,1,2,3};

int **p=(int**)a;//強(qiáng)制將二維數(shù)組指針轉(zhuǎn)為指向指針的指針

則此時(shí)p[0]=0;p[1]=1;p[2]=2;p[3]=3;

而p[0][0]=*(*(p+0)+0)=**p;

p[0][1]=*(*(p+0)+1);

對(duì)于p[0][0]:由于*p=0; ====> **p=*(0);引用地址為零的內(nèi)存,必然是錯(cuò)誤的。

對(duì)于p[0][1]=*(*p+1)====>*(4),引用了非法內(nèi)存同樣,

對(duì)于p[1][0]=*(1),p[1][1]=*(5),均引用了非法內(nèi)存所以說,二位數(shù)組并不能簡(jiǎn)單的轉(zhuǎn)換成指向指針的指針。

二維數(shù)組其實(shí)只是一個(gè)指針,而二級(jí)指針是指向指針的指針,所以二者并不等價(jià)。如上例所示:int a[2][2];

a是指向整個(gè)數(shù)組的首地址,并不是int **;所以不要指望向函數(shù)fun里面?zhèn)鲗?shí)參 p=a;

 

感謝sld666666,我覺得那個(gè)應(yīng)該是和下面的情況類似把,中間有個(gè)強(qiáng)制轉(zhuǎn)換的過程:

 

#include <iostream>

 

void fun(char ** p)

{

char (*p1)[10] = (char(*)[10])p;

std::cout<<p1[0][0]<<std::endl;

}

 

int main(int argc, char* argv[])

{

char data[][10] = {"abc","def"};

fun((char **)data);

return 0;

}

----------------------------------------------------------------華麗的分割線---------------------------------------------------------------------------------------------------------------------------

 

<c程序設(shè)計(jì)語(yǔ)言>中的關(guān)于這個(gè)的解釋:

Newcomers to C are sometimes confused about the difference between a two-dimensional array and an array of pointers, such as name in the example above. Given the definitions

int a[10][20];

int *b[10];

then a[3][4] and b[3][4] are both syntactically legal references to a single int. But a is a true two-dimensional array: 200 int-sized locations have been set aside, and the conventional rectangular subscript calculation 20 * row +col is used to find the element a[row,col]. For b, however, the definition only allocates 10 pointers and does not initialize them; initialization must be done explicitly, either statically or with code. Assuming that each element of b does point to a twenty-element array, then there will be 200 ints set aside, plus ten cells for the pointers. The important advantage of the pointer array is that the rows of the array may be of different lengths. That is, each element of b need not point to a twenty-element vector; some may point to two elements, some to fifty, and some to none at all.

Although we have phrased this discussion in terms of integers, by far the most frequent use of arrays of pointers is to store character strings of diverse lengths, as in the function month_name. Compare the declaration and picture for an array of pointers:

char *name[] = { "Illegal month", "Jan", "Feb", "Mar" };?

with those for a two-dimensional array:

char aname[][15] = { "Illegal month", "Jan", "Feb", "Mar" };


//我的理解是,當(dāng)是指針數(shù)組的時(shí)候,可以直接傳,如果是普通的二維數(shù)組的話應(yīng)該就進(jìn)行上面的轉(zhuǎn)換。

    一下是自己遇到問題:

    問題1:

 1#include "stdafx.h"
 2
 3#include <iostream>
 4using namespace std;
 5
 6
 7typedef struct tagNode_st
 8{
 9 char m_acData[10];
10 int m_iNo;
11}
Node_st;
12
13Node_st Root;
14
15int Fun(Node_st ** pst)
16{
17//Error
18#if 0
19 Node_st astNodeA[2= {{"xiaowang"1}{"xiaoming"2}};
20#else
21 static Node_st astNodeA[2= {{"xiaowang1"1}{"xiaoming1"1}}
22#endif
23 //static Node_st astNodeB[2] = {{"xiaowang2", 2}, {"xiaoming2", 2}}; 
24//static Node_st astNodeC[2] = {{"xiaowang3", 3}, {"xiaoming3", 3}}; 
25*pst = astNodeA;
26
27 return 0;
28}

29
30int _tmain(int argc, _TCHAR* argv[])
31{
32 Node_st st[2][2];
33
34 //TypeA
35 Fun((Node_st**)st);
36   //1.error
37 cout<<st[0][0].m_acData<<endl;
38 cout<<st[0][0].m_iNo<<endl<<endl;
39
40 //2.error
41 cout<<(*st)->m_acData<<endl;
42 cout<<(*st)->m_iNo<<endl<<endl;
43
44 //3.right
45 cout<<(*(Node_st**)st)->m_acData<<endl;
46 cout<<(*(Node_st**)st)->m_iNo<<endl<<endl;
47
48 //Typde B
49 Node_st *pstTemp[2= {&st[0][0], &st[1][0]};
50 Fun(&pstTemp[0]);
51 //Right
52 cout<<(pstTemp[0])->m_acData<<endl;
53 cout<<(pstTemp[0])->m_iNo<<endl<<endl;
54
55 //Error
56 cout<<(st[0][0]).m_acData<<endl;
57 cout<<(st[0][0]).m_iNo<<endl<<endl;
58
59 //Right
60 cout<<(*(Node_st**)st)->m_acData<<endl;
61 cout<<(*(Node_st**)st)->m_iNo<<endl<<endl;
62
63 //Typde C
64
65 Node_st *pstTemp2[2= {NULL, NULL};
66 Fun(&pstTemp2[0]);
67 //Right
68 cout<<(pstTemp2[0])->m_acData<<endl;
69 cout<<(pstTemp2[0])->m_iNo<<endl<<endl;
70
71 return 0;
72}

73

最終通過上面藍(lán)色部分找到了到了答案,簡(jiǎn)單的說就是

二維數(shù)組其實(shí)只是一個(gè)指針,而

二級(jí)指針是指向指針的指針,所以二者并不等價(jià)。

 

但是可以強(qiáng)轉(zhuǎn)

如:

1 int iaArray[2][2= {1124};
2 
3 int **= (int**)iaArray;
4 
5 for (int i = 0; i < 4++ i)
6 {
7  cout<<"i:"<<i<<" "<<q[i]<<endl;
8 }

9

 和:
1 int iaArrayTemp[5] = {1, 2, 3, 4, 5};
2 int **p = (int**)&iaArrayTemp;
3 p++;
4 cout<<*p<<endl;

這樣就是正確的。

問題2:
下面的問題:很有意思

1#include <iostream>
2using namespace std;
3
4int main()
5{
6 int iaArray[5] = {1, 2, 3, 4, 5};
7
8#if 0
9
10 int *p = (int*)(&iaArray+1)-1;
11 cout<<*p<<endl;
12
13 int *q = (int*)(&iaArray+1);
14 cout<<*(q-1)<<endl;
15
16 int **qq = (int**)(&iaArray+1);
17 cout<<*(qq-1)<<endl;
18
19#else
20 int iaAry[2][2] = {1, 2, 3, 4};
21
22 int *p = (int*)(&iaAry+1)-1;
23 cout<<*p<<endl;
24
25 int *q = (int*)(&iaAry+1);
26 cout<<*(q-1)<<endl;
27
28 int **qq = (int**)(&iaAry+1);
29 cout<<*(qq-1)<<endl;
30#endif
31 return 0;
32}

上面的結(jié)果都是5,下面的結(jié)果都是4
主要說明的是:
不管是二維數(shù)組,還是一維數(shù)組
數(shù)組的首地址取地址+1,增加整個(gè)數(shù)組的長(zhǎng)度;
如上面的例子:

    3:注意函數(shù)傳遞,指針,引用

    在指針引用&*,**的時(shí)候是改變的指針,這個(gè)一般主要是里面涉及到內(nèi)存分配,
    或者獲取的是靜態(tài)區(qū)域,或者是全局的區(qū)域,傳遞的時(shí)候一般都是傳,空指針。

     傳遞*,&,是改變的數(shù)組的值。一般都是傳遞的是非空的,一般要再函數(shù)中增加
      assert(NULL != p);
    
     4.const ,Enum,static const ...待續(xù)

     快0:00,笑一笑,睡覺了
posted on 2011-11-21 23:55 郭龍 閱讀(5627) 評(píng)論(0)  編輯 收藏 引用

只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。
網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理


青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            国产精品日韩久久久| 久久精品国产久精国产爱| 欧美成人午夜激情视频| 欧美国产日韩xxxxx| 一区二区三区久久网| 亚洲愉拍自拍另类高清精品| 亚洲一区二区在线| 经典三级久久| 亚洲精选国产| 国模大胆一区二区三区| 亚洲美女区一区| 影音先锋国产精品| 在线亚洲免费| 最新中文字幕亚洲| 先锋影音久久久| 日韩一区二区免费看| 午夜欧美精品| 亚洲一区二区三区免费视频| 欧美99久久| 久久男人av资源网站| 欧美香蕉大胸在线视频观看| 牛牛精品成人免费视频| 国产伦精品一区二区三区高清版 | 玖玖国产精品视频| 亚洲免费在线视频一区 二区| 麻豆精品传媒视频| 久久久国产一区二区| 欧美三级午夜理伦三级中视频| 免费成人毛片| 国产日本欧洲亚洲| 99精品视频免费| 亚洲国产成人在线| 久久精品国产一区二区三区免费看| 一本一本久久a久久精品综合麻豆 一本一本久久a久久精品牛牛影视 | 午夜在线a亚洲v天堂网2018| 欧美精品激情在线| 欧美成人网在线| 国内精品国语自产拍在线观看| 一本色道久久综合狠狠躁篇怎么玩 | 99精品国产一区二区青青牛奶| 久久精品国产77777蜜臀| 亚洲永久免费视频| 欧美色综合天天久久综合精品| 亚洲欧洲日夜超级视频| 亚洲国产女人aaa毛片在线| 欧美一级视频精品观看| 欧美在线视频一区二区三区| 国产精品视频网| 亚洲少妇诱惑| 午夜日韩在线观看| 国产精品区一区二区三| 亚洲一级黄色片| 欧美在线视频二区| 国产日韩亚洲欧美精品| 亚洲欧美日韩高清| 欧美一区影院| 国产在线精品二区| 久久成人免费电影| 免费在线成人| 亚洲精品欧美专区| 欧美区在线观看| 一二美女精品欧洲| 午夜宅男久久久| 麻豆精品视频在线观看视频| 免费亚洲网站| 日韩午夜激情av| 国产精品久久久久久一区二区三区| 亚洲欧美国产精品va在线观看 | 欧美日韩亚洲一区三区| 亚洲小视频在线| 久久精品夜色噜噜亚洲a∨| 国产综合色一区二区三区| 欧美在线视频免费播放| 亚洲成色www久久网站| 在线亚洲国产精品网站| 亚洲欧美日韩成人| 欧美.日韩.国产.一区.二区| 亚洲美女视频在线观看| 欧美日韩一区二区三区四区五区| 亚洲一区二区三区乱码aⅴ蜜桃女| 久久精品系列| 亚洲欧洲精品一区二区| 欧美午夜女人视频在线| 久久爱另类一区二区小说| 亚洲人成在线播放网站岛国| 欧美在线日韩精品| 亚洲精品亚洲人成人网| 国产欧美1区2区3区| 久久中文在线| 亚洲永久免费| 欧美成人tv| 亚洲欧美清纯在线制服| 亚洲国产女人aaa毛片在线| 欧美三级精品| 久久综合九九| 午夜精品福利电影| 亚洲开发第一视频在线播放| 久久人人爽人人爽| 一区二区三区导航| 亚洲第一免费播放区| 国产精品一卡| 欧美日本中文字幕| 久久精品一区二区三区不卡牛牛 | 亚洲精品乱码久久久久久日本蜜臀 | 欧美一区二区三区免费在线看| 在线免费一区三区| 国产精品一二一区| 欧美视频精品一区| 麻豆国产精品777777在线| 欧美亚洲免费电影| 一本色道久久综合狠狠躁篇怎么玩| 久久久久一区| 欧美亚洲网站| 亚洲色在线视频| 亚洲欧洲日本专区| 在线国产日韩| 黄色欧美日韩| 国产日韩欧美视频在线| 国产精品观看| 欧美精品亚洲精品| 麻豆成人在线观看| 久久综合色影院| 久久精品午夜| 午夜国产精品视频| 亚洲一区一卡| 一区二区三区视频在线播放| 日韩小视频在线观看专区| 亚洲精品日产精品乱码不卡| 亚洲韩日在线| 亚洲黄一区二区| 欧美激情四色| 欧美黑人在线观看| 欧美激情精品| 亚洲成人在线视频播放| 夜夜爽av福利精品导航| 亚洲免费大片| 亚洲久久一区二区| 91久久国产综合久久91精品网站| 在线免费观看视频一区| 黄色在线一区| 精品成人久久| 伊人婷婷欧美激情| 亚洲福利视频专区| 亚洲国产成人tv| 亚洲日韩欧美视频| 亚洲精品一区二区在线| 亚洲欧洲综合另类在线| 亚洲精品欧洲| 亚洲一区二区视频在线观看| 亚洲一区日本| 欧美在线免费观看视频| 蜜臀av一级做a爰片久久| 欧美成人精品一区二区三区| 欧美成人亚洲成人| 亚洲美女精品久久| 亚洲桃色在线一区| 欧美专区福利在线| 欧美成人69av| 欧美香蕉大胸在线视频观看| 国产精品看片资源| 国内一区二区在线视频观看 | 先锋影音国产一区| 久久婷婷色综合| 欧美成人网在线| 国产精品家教| 国产综合色产| 亚洲精品久久在线| 性色av香蕉一区二区| 欧美freesex8一10精品| 99视频精品全国免费| 欧美专区亚洲专区| 你懂的国产精品| 欧美亚男人的天堂| 黄色另类av| 亚洲午夜精品久久| 久久精品道一区二区三区| 欧美国产国产综合| 亚洲女爱视频在线| 免费视频一区二区三区在线观看| 国产精品99一区二区| 玉米视频成人免费看| 亚洲天堂黄色| 免费观看一级特黄欧美大片| 99re热精品| 久久国产综合精品| 国产精品毛片va一区二区三区| 在线成人性视频| 亚洲一区亚洲二区| 欧美国产日韩视频| 欧美一激情一区二区三区| 欧美日韩久久不卡| 亚洲第一福利视频| 午夜精品亚洲一区二区三区嫩草| 欧美aaa级| 欧美在线亚洲在线| 国产精品v欧美精品v日本精品动漫 | 亚洲电影网站| 欧美中文在线字幕| 欧美性猛片xxxx免费看久爱| 亚洲国产精品第一区二区三区|