chenglong7997

知识点:

1。多维数组的单个元素的存储和引用实际上是以线性 形式排列在内存中的。

2.使用指针向一个函数传递一个多维数组。
myfunction(int my_array[][20]);

myfunction(char **my_array);
参数必须是指针数组,而且必须是指向字符串的指针数组。

char_array[rowsize *i +j]=....
将二位数组转换成以为数组  
posted @ 2012-06-23 05:02 Snape 阅读(154) | 评论 (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 阅读(147) | 评论 (0)编辑 收藏
1.指针与数组并不相同。
定义会分配内存。声明不会。
声明与定义应该相配。
posted @ 2012-06-21 06:07 Snape 阅读(147) | 评论 (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 阅读(263) | 评论 (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 阅读(332) | 评论 (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 阅读(191) | 评论 (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 阅读(165) | 评论 (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 阅读(1609) | 评论 (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 阅读(1866) | 评论 (0)编辑 收藏
仅列出标题
共2页: 1 2 

导航

<2012年6月>
272829303112
3456789
10111213141516
17181920212223
24252627282930
1234567

统计

常用链接

留言簿

随笔分类

随笔档案

文章分类

文章档案

my

搜索

最新评论

阅读排行榜

评论排行榜