知識點:
1。多維數組的單個元素的存儲和引用實際上是以線性 形式排列在內存中的。
2.使用指針向一個函數傳遞一個多維數組。
myfunction(int my_array[][20]);
myfunction(char **my_array);
參數必須是指針數組,而且必須是指向字符串的指針數組。
char_array[rowsize *i +j]=....
將二位數組轉換成以為數組
知識點:
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 函數中。
參數的地址與數組第一個元素的地址不同,而參數的內容是第一個元素的地址。說明編譯器確實用指針實現了數組的訪問。指針++后,指向數組下一個元素。
1.指針與數組并不相同。
定義會分配內存。聲明不會。
聲明與定義應該相配。
知識點:
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”結尾。
知識點:
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.
調用函數分配內存來保存函數的返回值。可以返回字符串常量的指針,靜態數組的指針,動態內存指針。
知識點:
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.盡量不要在代碼中使用無符號數,以避免增加不必要的復雜性。不要僅僅因為無符號數不存在負值(如年齡),就用它來表示數量。
只有在使用
位段和二進制掩碼時候,才可以用無符號數。在表達式中使用
強制轉換,是操作數均為有符號或者無符號數,這樣就不必由編譯器選擇結果的類型。
知識點:
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
方法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, 空間連續,釋放內存也比較方便。大家有什么想法的歡迎交流
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到對齊地址的前一個空間