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

chenglong7997

知識點:

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

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

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

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

1.數組和指針。
 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語言允許把形參聲明為數組,或者指針。編譯器知道何時形參是作為數組聲明的,但事實上在函數的內部,編譯器始終把它當做指向數組第一個元素的指針。
(也因為如此,這個指針的地址,與數組第一個元素的地址不相同。)
因此,但test和test_char函數中,sizeof(pointer)的大小都是4,為指針的大小。
也因此,只能傳給函數,數組的大小,因為無法用sizeof推出數組的大小。

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

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

4.數組名與數組一個元素, 與作為參數時,被編譯器轉換為的指針的關系。
#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);
}


結果:
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 

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





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

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

2.在結構中放置數組,可以講數組當做第一等級類型,用賦值語句拷貝整個數組。
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; //對整個結構賦值
}

3.union
為了節省空間
為了把一個數據解釋為兩種不同的東西。

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

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

typedef int banana;
unsigned banana i;//error

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

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

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


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

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

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

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

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

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

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

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

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

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

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

2.宏所接受參數類型可以不同。 最好只用于命名常量和為結構提供簡潔記法。

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

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

5.類型不兼容。因為兩個指針所指對象不同。而不是修飾符不同
foo (const char **p) {}
 
int main(int argc, char **argv)
{
     foo(argv);
}

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

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


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

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

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

結果:
Tue Jan 19 03:14:07 2038
posted @ 2012-06-21 03:39 Snape 閱讀(198) | 評論 (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,我感覺最好,只調用一次malloc, 空間連續,釋放內存也比較方便。

大家有什么想法的歡迎交流
posted @ 2012-03-27 01:20 Snape 閱讀(1683) | 評論 (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);

自己實現:
 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 }

上面實現很考驗對指針的掌握。原理不是很難。

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

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

導航

<2025年11月>
2627282930311
2345678
9101112131415
16171819202122
23242526272829
30123456

統計

常用鏈接

留言簿

隨筆分類

隨筆檔案

文章分類

文章檔案

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| 欧美日韩精品免费观看视一区二区| 欧美调教视频| 亚洲午夜一区二区| 亚洲午夜免费视频| 国产精一区二区三区| 久久精品一区蜜桃臀影院| 欧美一区日韩一区| 亚洲国产精品尤物yw在线观看 | 国产精品综合网站| 久久gogo国模啪啪人体图| 欧美一区在线视频| 亚洲欧洲日本国产| 一二三区精品| 韩日欧美一区二区三区| 亚洲盗摄视频| 国产精品爱啪在线线免费观看| 午夜影院日韩| 麻豆精品网站| 亚洲在线播放| 巨胸喷奶水www久久久免费动漫| 亚洲精品免费在线| 亚洲欧美视频一区| 亚洲精品日本| 欧美一区二区播放| 日韩亚洲欧美成人| 欧美在线免费观看视频| 99精品久久久| 久久久精品网| 亚洲在线观看视频| 母乳一区在线观看| 久久精品欧美| 欧美日韩一区二| 欧美.www| 国产一区二区三区四区五区美女| 最新国产の精品合集bt伙计| 国产精品一区二区久久国产| 亚洲狠狠丁香婷婷综合久久久| 国产欧美日韩另类视频免费观看| 亚洲春色另类小说| 国内精品国语自产拍在线观看| 亚洲精品小视频在线观看| 韩国精品在线观看| 中文国产成人精品| 亚洲九九爱视频| 久久美女艺术照精彩视频福利播放| 一区二区三区四区精品| 老司机67194精品线观看| 久久成人亚洲| 国产精品久久网| 亚洲九九精品| 亚洲精品精选| 久久影院亚洲| 免费在线观看精品| 韩国一区二区在线观看| 亚洲男同1069视频| 亚洲欧美国产一区二区三区| 欧美日韩精品在线观看| 91久久精品美女| 亚洲日本免费| 欧美岛国激情| 亚洲国产精品一区二区www| 狠狠色狠狠色综合人人| 欧美亚洲网站| 久久偷窥视频| 亚洲丶国产丶欧美一区二区三区| 久久精品av麻豆的观看方式| 久久www免费人成看片高清 | 亚洲香蕉在线观看| 美女黄毛**国产精品啪啪| 久久综合色8888| 激情成人在线视频| 久久久综合网站| 欧美国产日韩在线| 亚洲国产mv| 欧美激情麻豆| 日韩午夜免费| 午夜精品久久久久久久久| 国产伦精品一区二区三| 欧美一区二区三区在线看| 久久嫩草精品久久久精品| 1204国产成人精品视频| 免费在线成人| 日韩一区二区免费看| 亚洲欧美综合精品久久成人| 国产精品自拍三区| 久久躁狠狠躁夜夜爽| 欧美激情无毛| 亚洲综合视频一区| 国产自产在线视频一区| 麻豆久久精品| 日韩视频在线一区| 久久精品国产v日韩v亚洲 | 欧美精品免费视频| 中文欧美日韩| 免费看亚洲片| 亚洲午夜精品视频| 国产亚洲成av人在线观看导航| 久久国产加勒比精品无码| 欧美国产日本在线| 亚洲欧美影院| 亚洲电影在线看| 国产精品试看| 欧美r片在线| 午夜精品成人在线| 91久久国产综合久久| 欧美亚洲免费电影| 亚洲每日在线| 国产自产精品| 欧美日韩亚洲免费| 久久久中精品2020中文| 夜夜嗨av一区二区三区四季av| 久久综合久久88| 午夜精品久久久久久久99黑人| 91久久精品美女高潮| 国产精品一区二区三区乱码| 欧美国产激情二区三区| 久久精品午夜| 亚洲综合日本| 亚洲免费av片| 亚洲国产高清在线| 久久国产精品电影| 亚洲影视在线| 日韩亚洲在线| 亚洲人成毛片在线播放女女| 国产主播一区二区三区四区| 欧美日韩国产小视频| 久久字幕精品一区| 久久riav二区三区| 午夜在线a亚洲v天堂网2018| 99国产精品久久久久老师| 亚洲国产精品国自产拍av秋霞| 久久久97精品| 久久精品成人一区二区三区 | 国产欧美一区二区三区沐欲| 欧美日韩国产另类不卡| 欧美大尺度在线观看| 久久久www| 久久久久亚洲综合| 久久国产精品亚洲77777| 亚洲在线日韩| 午夜精品视频在线观看一区二区| 一本色道久久综合亚洲精品小说| 亚洲黄色免费网站| 亚洲第一在线| 亚洲国产精品精华液网站| 欧美韩日一区| 亚洲激情偷拍| 亚洲精品一区二区三区蜜桃久| 亚洲国产欧美不卡在线观看| 亚洲国产精品久久久久秋霞蜜臀| 欧美黄在线观看| 亚洲人体影院| 一区二区三区日韩欧美| 国产精品99久久久久久有的能看| 一区二区欧美日韩| 亚洲欧美日韩精品在线| 午夜精品久久久久久久99樱桃| 午夜精品电影| 久久资源在线| 欧美激情综合色综合啪啪| 欧美午夜不卡视频| 国产精品一区久久| 在线观看视频一区| av成人国产| 欧美在线短视频| 免费不卡在线观看| 亚洲麻豆一区| 香蕉久久久久久久av网站| 久久女同互慰一区二区三区| 牛牛影视久久网| 国产精品久久国产愉拍 | 韩国一区二区在线观看| 亚洲国产午夜| 午夜精品福利视频| 噜噜噜91成人网| 亚洲乱码精品一二三四区日韩在线 | 久久久天天操| 亚洲激情小视频| 午夜亚洲影视| 欧美久久久久中文字幕| 国产伦精品一区二区三区四区免费 | 国产欧美日本在线| 91久久精品日日躁夜夜躁欧美| 亚洲在线观看视频网站| 久久亚洲一区二区| 日韩视频一区二区三区在线播放免费观看| 亚洲影院一区| 欧美激情视频一区二区三区在线播放| 国产精品久久久久久久7电影| 影音先锋日韩精品| 香蕉亚洲视频| 亚洲精品久久久久久久久久久| 欧美一区二区三区喷汁尤物| 欧美日韩国产精品| 亚洲国产视频a| 久久激情网站| 亚洲淫片在线视频| 欧美日韩三区| 日韩亚洲一区二区|