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

chenglong7997

知識點:

1。多維數(shù)組的單個元素的存儲和引用實際上是以線性 形式排列在內(nèi)存中的。

2.使用指針向一個函數(shù)傳遞一個多維數(shù)組。
myfunction(int my_array[][20]);

myfunction(char **my_array);
參數(shù)必須是指針數(shù)組,而且必須是指向字符串的指針數(shù)組。

char_array[rowsize *i +j]=....
將二位數(shù)組轉(zhuǎn)換成以為數(shù)組  
posted @ 2012-06-23 05:02 Snape 閱讀(180) | 評論 (0)編輯 收藏
知識點:

1.數(shù)組和指針。
 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 
 4 void test(int arr[])
 5 {
 6     printf("In Func: size of the array is %d\n", sizeof(arr) );    //4 the pointer's size
 7 }
 8 
 9 void test_char(char arr[])
10 {
11     printf("In Func: sizeof the my_char is %d\n", sizeof(arr));    //4 the pointer's size
12     printf("In Func: strlen the my_char is %d\n", strlen(arr));    //11 the string's size
13 }
14 
15 int main()
16 {
17     int array[100]={1,3,4,5};
18     char my_char[50]="hello world";
19 
20     printf("In Main: sizeof the array is %d\n", sizeof( array) );    //400
21 
22     printf("In Main: sizeof the my_char is %d\n", sizeof(my_char));    //50
23     printf("In Main: strlen the my_char is %d\n", strlen(my_char));    //11 the string's size
24 
25     test(array);
26     test_char(my_char);
27 }
C語言允許把形參聲明為數(shù)組,或者指針。編譯器知道何時形參是作為數(shù)組聲明的,但事實上在函數(shù)的內(nèi)部,編譯器始終把它當(dāng)做指向數(shù)組第一個元素的指針。
(也因為如此,這個指針的地址,與數(shù)組第一個元素的地址不相同。)
因此,但test和test_char函數(shù)中,sizeof(pointer)的大小都是4,為指針的大小。
也因此,只能傳給函數(shù),數(shù)組的大小,因為無法用sizeof推出數(shù)組的大小。

2.有一種操作只能在指針里進行,而無法在數(shù)組中進行,就是修改它的值。
數(shù)組名是不可修改的左值,他的值是不能改變的。也可看做常量指針。

3.數(shù)組和指針的可交換性總結(jié)
用a[i]這樣的形式對數(shù)組進行訪問,總是被編譯器“改寫”成*(a+i)這樣的指針訪問
指針始終就是指針。它絕不可以改寫成數(shù)組。你可以用下標(biāo)形式訪問指針,一般都是指針作為函數(shù)參數(shù)時候,而且你知道實際傳遞給函數(shù)的就是一個數(shù)組
在特定上下文中,也就是作為函數(shù)的參數(shù),一個數(shù)組的聲明,可依看做是一個指針。作為函數(shù)參數(shù)的數(shù)組,始終會被編譯器改成指向數(shù)組的第一個元素的指針。
當(dāng)把一個數(shù)組定義為函數(shù)的參數(shù)時候,可以選擇定義為數(shù)組,也可以定義為指針,不管用哪種方法,在函數(shù)內(nèi)部事實上獲得的就是一個指針
在其他所有情況,定義和聲明必須相匹配。如果定義了一個數(shù)組,在其他文件中對它進行聲明時候,也必須把他聲明為數(shù)組。指針也是一樣。

4.數(shù)組名與數(shù)組一個元素, 與作為參數(shù)時,被編譯器轉(zhuǎn)換為的指針的關(guān)系。
#include <stdio.h>
#include <stdlib.h>


void test_char(char arr[])
{
    printf("In test_char().\n");
    printf("addr of array param = %#x \n", &arr);
    printf("content of array param = %#x \n", arr);
    printf("addr of (arr[0]) = %#x \n", &arr[0]);
    printf("addr of (arr[1]) = %#x \n", &arr[1]);
    printf("++arr = %#x \n", ++arr);
}

int main()
{
    //int array[100]={1,3,4,5};
    char my_char[50]="hello world";


    printf("addr of array head %x\n", &my_char);
    printf("addr of my_char[0] %x\n", &(my_char[0]));
    printf("addr of my_char[1] %x\n", &(my_char[1]));
    //test(array);
    test_char(my_char);
}


結(jié)果:
addr of array head bf8c0dfa
addr of my_char[0] bf8c0dfa
addr of my_char[1] bf8c0dfb
In test_char().
addr of array param = 0xbf8c0de0 
content of array param = 0xbf8c0dfa 
addr of (arr[0]) = 0xbf8c0dfa 
addr of (arr[1]) = 0xbf8c0dfb 
++arr = 0xbf8c0dfb 

結(jié)果說明:
數(shù)組名的地址與數(shù)組第一個元素的地址是相同的。因此數(shù)組名不是第一個元素的指針,而是第一個元素,除了它不能直接取出第一個元素。
在test_char 函數(shù)中。參數(shù)的地址與數(shù)組第一個元素的地址不同,而參數(shù)的內(nèi)容是第一個元素的地址。說明編譯器確實用指針實現(xiàn)了數(shù)組的訪問。指針++后,指向數(shù)組下一個元素。





posted @ 2012-06-23 02:59 Snape 閱讀(170) | 評論 (0)編輯 收藏
1.指針與數(shù)組并不相同。
定義會分配內(nèi)存。聲明不會。
聲明與定義應(yīng)該相配。
posted @ 2012-06-21 06:07 Snape 閱讀(176) | 評論 (0)編輯 收藏
知識點:

1.int 變量跟只包含一個int 成員的結(jié)構(gòu)變量,在傳遞參數(shù)的方式可能完全不同。一個 int參數(shù)一般會傳遞到寄存器中,而結(jié)構(gòu)參數(shù)很可能被傳遞到堆棧中。

2.在結(jié)構(gòu)中放置數(shù)組,可以講數(shù)組當(dāng)做第一等級類型,用賦值語句拷貝整個數(shù)組。
struct s_tag {int a[100];};
struct s_tag orange, lime;

int main()
{
    int i;
    for(i=0; i<100; i++) lime.a[i]=1;
    orange=lime; //對整個結(jié)構(gòu)賦值
}

3.union
為了節(jié)省空間
為了把一個數(shù)據(jù)解釋為兩種不同的東西。

4.enum
#define定義的名字一般在編譯時期被丟棄,而枚舉名字則通常一直在調(diào)試器中可見。可以再調(diào)試代碼的時候看見他們。

5.typedef int x[10] 與 #define x int[10]區(qū)別
可以用其他類型說明符對宏類型名進行擴展,但是不能對type的飯定義的類型名,進行擴展
#define peach int
unsigned peach i;//right

typedef int banana;
unsigned banana i;//error

在連續(xù)幾個變量聲明中,用typedef定義的類型能夠保證聲明中所有變量均為同一種類型,但是#define定義的類型不能保證。

#define int_ptr int*;
int_ptr chalk, cheese;
結(jié)果:int * chalk, cheese;(他們兩個類型不同)

#typedef char* char_ptr
char_ptr ben, rool(兩個都是指向char的指針)


6. 不要為了方便對結(jié)構(gòu)使用typedef,雖然可以省去struct但是省去了提示信息。
用在數(shù)組,結(jié)構(gòu),指針,以及函數(shù)的組合類型。結(jié)構(gòu)標(biāo)簽可以添加“_tag”結(jié)尾。
posted @ 2012-06-21 05:39 Snape 閱讀(325) | 評論 (0)編輯 收藏
知識點:

1.malloc(strlen(str)) 幾乎肯定是錯誤的。應(yīng)該是 malloc(strlen(str)+1)。

2.NUL用于結(jié)束一個ACSII字符串。NULL表示什么也不指向。空指針。

3.switch中default寫成defau1t的錯誤。(能夠通過編譯)。
break會跳出最近的那層循環(huán),或者switch語句

4.ANSI C 相鄰的字符串常量會被自動合并成一個字符串的約定。
printf("a second favorite children's book"
"is 'yitiantulong' "
);
編譯時候會自動合并,除了最后字符串外,每個字符串結(jié)尾的‘\0’會被刪除。
bug:
char *resourse[]={
"big disk",
"color"   //color 之后沒有寫逗號,那么會和mouse連接在一起。
"mouse",
};

5.代碼第一次執(zhí)行時候行為,與以后執(zhí)行的不同。
initializer( char *str)
{
    static char separator='';
    printf(" %c %s \n", separator, str);
    separator=',';
}
非常簡便,比起其他的方法。

6.extern用于函數(shù)定義,表示全局可見。(屬于冗余)
extern用于標(biāo)量,表示在其他地方定義。

7.x=f() * g();
f() 與個g() 調(diào)動順序不確定。

8.空格
\newline 與 \ newline意義不同。

9.調(diào)用函數(shù)分配內(nèi)存來保存函數(shù)的返回值。可以返回字符串常量的指針,靜態(tài)數(shù)組的指針,動態(tài)內(nèi)存指針。
posted @ 2012-06-21 04:26 Snape 閱讀(368) | 評論 (0)編輯 收藏
知識點:

1.float不會被自動擴展為double。 在ANSI C中

2.宏所接受參數(shù)類型可以不同。 最好只用于命名常量和為結(jié)構(gòu)提供簡潔記法。

3.操作符左右最好用空格分開。防止古老版本的程序,會修改賦值符的位置。

4.在limits.h中有INT_MAX, LONG_MAX定義

5.類型不兼容。因為兩個指針?biāo)笇ο蟛煌6皇切揎椃煌?br />
foo (const char **p) {}
 
int main(int argc, char **argv)
{
     foo(argv);
}

6.K&R C 和 ANSI C對待無符號數(shù)原則不同
K&R C 使用無符號保留的原則。
ANSI C 使用值保留的原則。(整數(shù)如果轉(zhuǎn)換為signed不會丟失信息,就轉(zhuǎn)換為signed,否則轉(zhuǎn)換為unsigned)
經(jīng)過gcc中測試,采用的是ANSI C的原則

7.盡量不要在代碼中使用無符號數(shù),以避免增加不必要的復(fù)雜性。不要僅僅因為無符號數(shù)不存在負值(如年齡),就用它來表示數(shù)量。
只有在使用位段和二進制掩碼時候,才可以用無符號數(shù)。在表達式中使用強制轉(zhuǎn)換,是操作數(shù)均為有符號或者無符號數(shù),這樣就不必由編譯器選擇結(jié)果的類型。


posted @ 2012-06-21 04:03 Snape 閱讀(225) | 評論 (0)編輯 收藏
知識點:

1.if(3==i)
最好用這種方式,避免少寫一個=

2.time_t在/user/include/time.h中,用一個long來表示。因此最大值為0x7FFFFFFF(有符號數(shù));
或者用biggest=(long)(pow (2, sizeof(long)*8)-1 );來獲取最大值。
之后用gmtime, asctime函數(shù)獲取最大時間

結(jié)果:
Tue Jan 19 03:14:07 2038
posted @ 2012-06-21 03:39 Snape 閱讀(194) | 評論 (0)編輯 收藏
方法1:
 1 #include <iostream>
 2 using namespace std;
 3 
 4 //by snape 2012-3-25
 5 //method 1: drawback is we need calculate the one-dimensional index to access the 2D array
 6 int main()
 7 {
 8     int rowSize, colSize, totalSize;
 9     int index, i,j;
10 
11     cout<<"Enter the row and column size for your 2D array!"<<endl;
12     cin>>rowSize>>colSize;
13 
14     totalSize=rowSize*colSize;
15     int *pArray;
16     pArray=new int[totalSize];
17 
18     //file  the array with integers from 0 to totalsize
19     //file across the rows, moving down the colums
20 
21     int arrayValue=0;
22     for(i=0; i<rowSize; ++i)    //outer-loop traverse down the "rows"
23     {
24         for(j=0; j<colSize; ++j)
25         {
26             //caculate array index
27             //index=rowSize*j+i;        //both index=rowSize*j+i; and index=colSize*i+j; are ok
28             index=colSize*i+j;            //but if index=rowsize*i+j; or index=colSize*j+i; then there will be a bug.
29             pArray[index]=arrayValue;    // i like index=colSize*i+j; since the arrange of 2D is according to rows
30             cout<<"index="<<index<<endl;
31             cout<<"i(row)="<<i<<"j(col)="<<j<<"array value"<<pArray[index]<<endl;
32             ++arrayValue;
33         }
34     }
35 
36     //output the array
37     for(int k=0; k<totalSize; ++k)
38     {
39         cout<<pArray[k]<<endl;
40     }
41     cout<<"The End"<<endl;
42     delete [] pArray;
43     return 0;
44 }
45 
46 

方法2:
 1 //by snape
 2 //method 2: better than method 1, but call new twice
 3 int main()
 4 {
 5     int rowSize, colSize, totalSize;
 6     int i, j;
 7     cout<<"Enter the row and column size for your 2D array"<<endl;
 8     cin>>rowSize>>colSize;
 9 
10     totalSize=rowSize*colSize;
11 
12     int *pArray; //pointer to an integer
13     int **pPointerArray; //pointer to an integer pointer
14 
15     pArray=new int[totalSize];    //memory for totalSize integers
16     pPointerArray=new int*[rowSize];    //memory for rowSize # of int pointers
17 
18     //fill the pointer array with the pArray[i][0] address
19     for(i=0; i<rowSize; ++i)
20         pPointerArray[i]=pArray+i*colSize;    //place the address into the pointer
21 
22     //now fill the pArray by using the pPointerArray to access elements
23     int arrayValue=0;
24     for(i=0; i<rowSize; ++i)
25     {
26         for(j=0; j<colSize; ++j)
27         {
28             pPointerArray[i][j]=arrayValue;    //cool
29             cout<<"i(row)="<<i<<"j(col)="<<j<<"array value="<<pPointerArray[i][j]<<endl;
30             ++arrayValue;
31         }
32     }
33 
34         //output the array
35     for(int k=0; k<totalSize; ++k)
36     {
37         cout<<pArray[k]<<endl;
38     }
39     cout<<"The End!"<<endl;
40     delete [] pArray;
41     delete [] pPointerArray;
42     return 0;
43 }

方法3:
 1 // by snape
 2 //method 3: better than method 2. just malloc once and the memory is contiguous block. the best
 3 int **my2DAlloc(int rowSize, int colSize)
 4 {
 5     int i;
 6     int header=rowSize * sizeof(int *);
 7     int data=rowSize * colSize * sizeof(int);
 8     int **rowptr=(int **)malloc(header+data);    //malloc memory for both data and pointerArray(the header)
 9 
10     if(rowptr==NULL)
11         return NULL;
12     int *buf=(int *)(rowptr+rowSize);    //buf: the pointer to the first data
13     for(i=0; i<rowSize; ++i)    //assign the address of each row to pointerArray(the header)
14         rowptr[i]=buf+i*colSize;
15 
16     return rowptr;
17 }
18 
19 int main()
20 {
21     cout<<"Enter the row and column size for your 2D array"<<endl;
22     int rowSize, colSize;
23     cin>>rowSize>>colSize;
24     int **p=my2DAlloc(rowSize, colSize);
25 
26     //assign values
27     int i, j, arrayValue=0;
28     for(i=0; i<rowSize; ++i)
29         for(j=0; j<colSize; ++j)
30             p[i][j]=arrayValue++;
31     
32     //output values
33     for(i=0; i<rowSize; ++i)
34         for(j=0; j<colSize; ++j)
35             cout<<p[i][j]<<endl;
36 
37     free((void *)p);
38 }

方法3,我感覺最好,只調(diào)用一次malloc, 空間連續(xù),釋放內(nèi)存也比較方便。

大家有什么想法的歡迎交流
posted @ 2012-03-27 01:20 Snape 閱讀(1669) | 評論 (6)編輯 收藏

MSVC 下面有:

1 
2 void * _aligned_malloc(
3     size_t size, 
4     size_t alignment
5 );
Unix/Linux下面有:
1 int posix_memalign(void **memptr, size_t alignment, size_t size);

自己實現(xiàn):
 1 #include <iostream>
 2 #include <stdlib.h>
 3 using namespace std;
 4 
 5 //by snape 2012-3-25
 6 
 7 void *aligned_malloc(size_t required_bytes, size_t alignment)
 8 {
 9     void *p1;    //orignal block
10     void **p2;    //aligned block
11 
12     int offset=alignment-1+sizeof(void*);
13     if( (p1=(void*)malloc(required_bytes+offset))==NULL )
14         return NULL;
15     //cast void* to void** to let p1 store into heap in the form of pointer
16     p2=(void**)( ((size_t)(p1)+offset) & ~(alignment-1) );    //add offset to p1 and then back to the aligned address
17     p2[-1]=p1;
18     return p2;
19 }
20 
21 void aigned_free(void *p2)
22 {
23     void* p1=((void**)p2)[-1];    //get the p1
24     free(p1);
25 }
26 
27 
28 
29 int main()
30 {
31     int size=10, align=16;
32     int *p=(int *)aligned_malloc(size, align);
33     
34     cout<<"The start address="<<p<<endl;
35     
36     aigned_free(p);
37 }

上面實現(xiàn)很考驗對指針的掌握。原理不是很難。

Line12:分配內(nèi)存的時候,加上alignment-1個空間,就能保證能夠找到和alignment對齊的開始地址。多出來的sizeof(void*)是為了保存malloc最初返回的指針p1而分配的一個空間

Line 17: 保存原始的malloc返回的地址p1到對齊地址的前一個空間
posted @ 2012-03-27 01:19 Snape 閱讀(1914) | 評論 (0)編輯 收藏
僅列出標(biāo)題
共2頁: 1 2 

導(dǎo)航

<2025年9月>
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011

統(tǒng)計

常用鏈接

留言簿

隨筆分類

隨筆檔案

文章分類

文章檔案

my

搜索

最新評論

閱讀排行榜

評論排行榜

青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <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>
            国产精品视频99| 久久中文精品| 老司机成人在线视频| 国产欧美一区二区三区在线老狼 | 久久久噜噜噜久久| 亚洲欧美三级在线| 国产亚洲成年网址在线观看| 亚洲欧美中文另类| 亚洲一区二区三区中文字幕| 国产精品亚洲综合久久| 欧美在线精品免播放器视频| 亚洲一区二区三区在线看| 国产精品另类一区| 在线视频日韩| 亚洲午夜小视频| 欧美日韩mv| 亚洲欧美日韩精品久久| 性一交一乱一区二区洋洋av| 国产亚洲综合在线| 开心色5月久久精品| 麻豆av一区二区三区| 91久久国产综合久久91精品网站| 久久久午夜精品| 久久久精品一区| 99re热这里只有精品视频| 亚洲欧美日韩中文播放| 亚洲毛片播放| 欧美在线视频一区二区| 一本一本久久a久久精品综合妖精 一本一本久久a久久精品综合麻豆 | 国内成+人亚洲| 亚洲高清久久久| 国产精品久久久久三级| 欧美大尺度在线观看| 国产精品女人网站| 亚洲国产精品999| 狠色狠色综合久久| 亚洲神马久久| 一区二区三区高清| 麻豆精品一区二区av白丝在线| 亚洲欧美日韩在线高清直播| 欧美成人亚洲成人| 久久在线免费视频| 国产区二精品视| 中文无字幕一区二区三区| 亚洲人成在线观看| 另类成人小视频在线| 久久久人成影片一区二区三区观看| 国产精品mm| 日韩视频在线一区二区三区| 亚洲国产综合91精品麻豆| 欧美专区在线观看| 久久久久国产免费免费| 国产精品羞羞答答| 中文久久精品| 亚洲天堂免费观看| 欧美激情亚洲一区| 亚洲国产精品一区在线观看不卡| 国产在线拍偷自揄拍精品| 亚洲欧美日韩直播| 久久精品国产v日韩v亚洲| 国产精品网站在线观看| 亚洲一区二区三区免费在线观看| 亚洲图片欧洲图片av| 欧美日韩1区2区| 亚洲美女在线看| 国产精品99久久99久久久二8| 欧美精品一区二区高清在线观看| 亚洲人成在线播放| 亚洲桃花岛网站| 国产精品久久久久77777| 亚洲视频精选| 久久久999精品| 在线播放日韩欧美| 久久伊人精品天天| 亚洲国产精品久久久久秋霞不卡 | 国产精品成人在线| 午夜精品电影| 蜜月aⅴ免费一区二区三区| 亚洲日本成人| 欧美丝袜一区二区| 亚洲欧美国产一区二区三区| 久久精品网址| 亚洲经典三级| 国产精品久久久| 久久精品观看| 亚洲激情图片小说视频| 亚洲综合色丁香婷婷六月图片| 国产欧美在线| 欧美69wwwcom| 亚洲图片欧洲图片日韩av| 久久人91精品久久久久久不卡| 亚洲破处大片| 国产精品午夜国产小视频| 久久一二三国产| 一区二区三区四区五区在线| 久久久久久网址| 一区二区三区四区五区精品| 国产精品一区久久久久| 久久中文精品| 亚洲欧美综合| 亚洲激情av| 久久久99精品免费观看不卡| 99国产精品久久| 国内精品视频久久| 欧美日韩一区二区免费在线观看| 欧美一区二区三区视频在线| 91久久久久久| 久久综合免费视频影院| 亚洲一二三四区| 亚洲欧洲另类国产综合| 国产日韩欧美不卡在线| 欧美日本韩国一区二区三区| 久久久久久自在自线| 中文高清一区| 亚洲裸体视频| 欧美激情女人20p| 久久精品五月婷婷| 亚洲宅男天堂在线观看无病毒| 亚洲电影在线| 国内揄拍国内精品少妇国语| 国产精品国内视频| 欧美日韩国产一区精品一区 | 欧美国产日韩一区| 久久夜色撩人精品| 久久国产精品一区二区| 亚洲尤物在线| av成人国产| 一区二区三区免费在线观看| 亚洲韩日在线| 亚洲国产成人av| 免费久久久一本精品久久区| 久久亚洲精品网站| 久久久九九九九| 久久国产精品99国产精| 欧美一级片在线播放| 欧美亚洲三区| 久久高清国产| 久久久欧美一区二区| 久久野战av| 葵司免费一区二区三区四区五区| 久久久福利视频| 久久久精品国产一区二区三区| 久久国产高清| 久久婷婷蜜乳一本欲蜜臀| 久久久亚洲高清| 免费在线视频一区| 欧美国产视频日韩| 亚洲国产你懂的| 日韩视频在线免费| 亚洲伊人第一页| 香蕉久久夜色精品| 久久精品五月| 女同一区二区| 欧美无砖砖区免费| 国产日韩欧美制服另类| 国产亚洲一级| 亚洲国产精品一区二区www在线| 亚洲人成人一区二区在线观看| 在线视频亚洲一区| 欧美一级淫片播放口| 麻豆久久精品| 亚洲精品乱码视频| 亚洲欧美另类中文字幕| 久久精品成人| 欧美精品日韩三级| 国产欧美日本一区二区三区| 狠狠网亚洲精品| 夜夜嗨av一区二区三区网站四季av | 亚洲欧美日韩天堂| 另类图片国产| 日韩亚洲不卡在线| 欧美制服丝袜| 欧美日韩1区2区| 国产综合自拍| 亚洲图色在线| 美女主播一区| 亚洲一区二区三区中文字幕 | 一区二区精品国产| 久久九九全国免费精品观看| 欧美日韩ab片| 在线观看日韩国产| 亚洲摸下面视频| 欧美国产日韩二区| 午夜在线视频观看日韩17c| 美女国内精品自产拍在线播放| 欧美日韩一区在线播放| 精品91在线| 欧美一区二区私人影院日本| 91久久国产综合久久蜜月精品 | 午夜日韩视频| 欧美日韩精品免费观看视一区二区 | 亚洲欧美日韩精品一区二区| 欧美极品在线视频| 激情综合色丁香一区二区| 亚洲欧美电影院| 亚洲精品专区| 免费观看在线综合| 激情另类综合| 久久久国产一区二区| 亚洲一卡久久|