int *ptr_i;
ptr_i = (int *)malloc(sizeof(int));
/*沒配置到記憶體結束程式*/
if (ptr_i == NULL) {
printf("function malloc can't alllocate memory!\n");
system("PAUSE");
return 0;
}
*ptr_i = 5; /*設定i值
free(ptr_i); /*釋放 ptr_i 所指向的空間 */
ptr_i = NULL; /*制定 ptr_i 為 NULL */
system("PAUSE");
return 0;
2011年2月7日 星期一
2011年1月10日 星期一
[C]union記憶體共用
#include<stdio.h>
union data
{
char a;
int b;
}
main()
{
union data test;
test.b = 0;
printf("test.a = %c, test.b = %d\n", test.a, test.b);
test.b = 'X';
printf("test.a = %c, test.b = %d\n", test.a, test.b); /* 由於 union 共用記憶體,雖然只有 test.b 被修改 ,test.a 也會受到影響。 */
/* ps. X 的 ASCII 碼為 88 */
}
[C]常用函式表
| 函式名 | 所需之標頭檔 | 簡便使用方法 | 備註 |
| printf() | stdio.h | printf("字串", 參數... ) | 列印函式 |
| scanf() | stdio.h | scanf("字串", 參數... ) | 鍵盤輸入函式,請注意 "&" |
| pow() | math.h | pow(x, y) | x 的 y 次方,傳回值為 double |
| floor() | math.h | floor(x) | 取小於 x 的最大整數,x為double,如:floor(3.14)=3。 |
| ceil() | math.h | ceil(x) | 取大於 x 的最小整數,如:ceil(3.14)=4。 |
| sqrt() | math.h | sqrt(x) | 把 x 開根號 |
| strlen() | string.h | strlen(字串的名字) | 計算字串資料的長度。 |
| strcmp | string.h | strcmp(字串1, 字串2) | 比較兩個字串的異同。傳回值為 0 時,兩字串相同;反之,則不相同。 |
| strcpy | string.h | strcpy(字串1, 字串2) | 將字串2 拷貝一份到字串1。 原來在字串1 的資料將會覆蓋而消失。 |
2011年1月9日 星期日
2011年1月8日 星期六
[C]基本輸出型態
| %c | 以字元 方式輸出 |
| %d | 10 進位整數輸出 |
| %o | 以8進 位整數方式輸出 |
| %u | 無號整數輸出 |
| %x, %X | 將整 數以16進位方式輸出 |
| %f | 浮點 數輸出 |
| %e, %E | 使用科學記號顯示浮點數 |
| %g, %G | 浮點數輸出,取%f或%e(%f或%E),看哪個表示精簡 |
| %% | 顯示 % |
| %s | 字串輸出 |
訂閱:
文章 (Atom)