Last active
December 14, 2015 02:29
-
-
Save wfwei/5013647 to your computer and use it in GitHub Desktop.
Basic C Grammar
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include "stdio.h" | |
| #include "string" | |
| //宏定义方法,注意括号 | |
| #define MAX(x,y) ((x)>(y) ? (x) :(y)) | |
| #define IS_EVEN(n) ((n)%2==0) | |
| char * invalidPointer(){ | |
| char a[100]; //栈内变量,函数返回后消亡 | |
| scanf("%s", a); | |
| return a; | |
| } | |
| char * validPointer(){ | |
| int strLen = 10; | |
| char * a = (char *)malloc(strLen + 1); //堆变量,一直存在,需要手动free | |
| scanf("%s", a); | |
| return a; | |
| } | |
| void sizeofTest(){ | |
| char arr[100]; | |
| char *ptr = arr; | |
| printf("sizeof arr : %d\nsizeof ptr : %d\n", sizeof(arr), sizeof(ptr)); | |
| } | |
| int main(){ | |
| int arr[2][3]; | |
| printf("%d\n", sizeof(arr)); // 2*3*sizeof(int) | |
| memset(arr, 0, sizeof(arr)); // memset(string.h) 单字节操作(char) | |
| // memset(arr, 1, sizeof(arr)); // 粗错 | |
| sizeofTest(); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment