說明

常用 C Commands 記錄

通用

清空

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
void main()
{
// 清空字串
char sourceStr[20]
memset(sourceStr,0,20);

// 清空 structure
struct Member member;
memset(&member, 0x00, sizeof(struct Member));

// 指標類的 struct 尚未 malloc 時不能清空
struct Member *memberPoint;

// 指標類的 struct malloc 後,就可以清空
// 記憶體分配空間後就可以進行清空
struct Member *memberPoint1 = malloc(sizeof(struct Member));
memset(memberPoint1, 0x00, sizeof(struct Member));

}

轉換

1
2
3
4
5
6
7
8
9
10
11
void main()
{
// string -> int
char sourceStr[3] = "30";
int result = (int)strtol(&sourceStr, (char**)NULL, 10);
printf("%d\n",result);

char *sourceStr = "30";
int result = (int)strtol(sourceStr, (char**)NULL, 10);
printf("%d\n",result);
}

字串

計算長度

1
2
3
4
5
6
void main()
{
char *str = "lentgh";
// length is '6'
printf("length is '%d'\n",strlen(str));
}

寫字串進 array

1
2
3
4
5
6
7
8
9
10
11
void main()
{
char sourceStr[20] = {0};
char *targetStr = "target string";
// 寫字串並帶 format
snprintf(sourceStr,20,"x%sx",targetStr);
// 最好加上結束字元
sourceStr[strlen(sourceStr)] = 0x00;
// source string is 'xtarget stringx'
printf("source string is '%s'\n",sourceStr);
}

對比字串

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
void main()
{
char *sourceStr = "string source";
char *targetStr = "string target";
// 完整比對
if (strlen(sourceStr) == strlen(targetStr)
&& (0 == strncmp(sourceStr, targetStr, strlen(sourceStr)))){ // 或 memcmp(sourceStr, targetStr, strlen(sourceStr)) 都可以
printf("exactly equal\n");
} else {
// √
printf("possibly unequal lengths or comparison failed\n");
}

// include
if (strstr(sourceStr,"source")){
// √
printf("the sourceStr exist source term\n");
} else {
printf("not found\n");
}
}

在 String Array 中比對是否存在

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void main(){
char *stringArray[] = {
"Current",
"Energy",
"Power",
"Voltage",
NULL,
};
char **stringItem;
char *sourceKey = "Power";
for(stringItem = (char **)stringArray ; *stringItem ; ++stringItem){
if (strlen(sourceKey) == strlen(*stringItem) && 0 == strncmp(sourceKey, *stringItem,strlen(sourceKey))){
// the stringArray exist 'Power'
printf("the stringArray exist '%s'\n",*stringItem);
break;
}
}
}

在 String Array 中加入值在尾端

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
void main(){
// 因為要 resize 所以一定要用 malloc ,如果是 char* array[] 這種是沒辦法 realloc 的
char** stringArray = malloc(5 * sizeof(char*));
if (stringArray){
stringArray[0] = "Current";
stringArray[1] = "Energy";
stringArray[2] = "Power";
stringArray[3] = "Voltage";
}

int count =0;
char **stringItem;
for(stringItem = (char **)stringArray ; *stringItem ; ++stringItem){
count++;
}
stringArray = realloc(stringArray,sizeof(char*)*(count+2));
stringArray[count++] = "Test";
stringArray[count++] = NULL;
for(stringItem = (char **)stringArray ; *stringItem ; ++stringItem){
// array string is 'Current'
// array string is 'Energy'
// array string is 'Power'
// array string is 'Voltage'
// array string is 'Test'
printf("array string is '%s'\n",*stringItem);
}
}

復製字串

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void main()
{
char sourceStr[20] = {0};
char *targetStr = "string target";
// new string is 'string tar'
printf("new string is '%s'\n",strncpy(sourceStr,targetStr,10));

// 要復製的目標是指標時,可以改用 strndup
char *sourceStr;
char *targetStr = "string target";
sourceStr = strndup(targetStr,10);
// new string is 'string tar'
printf("new string is '%s'\n",sourceStr);
// 用完後記得 free 掉
free(sourceStr);
}

接字串在字尾

1
2
3
4
5
6
7
void main()
{
char sourceStr[20] = "string source";
char *targetStr = "string target";
// new string is 'string sourcestring tar'
printf("new string is '%s'\n",strncat(sourceStr,targetStr,10));
}

切割字串

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void main()
{
char source[8] = "a.b.c.d";
char *pToken;
pToken = strtok(&source, ".");
while(pToken != NULL)
{
// the token is 'a'
// the token is 'b'
// the token is 'c'
// the token is 'd'
printf("the token is '%s'\n",pToken);
pToken = strtok(NULL, ".");
}
}

Struct

初使化

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
struct _member{
char name[20];
int age;
};
typedef struct _member member;
void main()
{
// 非指標類
member m1;
memset(&m1, 0x00, sizeof(member));
m1.age = 18;

// 指標
member* m2 = malloc(sizeof(member));
memset(m2, 0x00, sizeof(member));
m2->age = 28;
}

復製 Struct

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
struct _member{
char name[20];
int age;
};
typedef struct _member member;
void main()
{
// 非指標類
member m1;
memset(&m1, 0x00, sizeof(member));
m1.age = 18;

// 指標
member* m2 = malloc(sizeof(member));
memset(m2, 0x00, sizeof(member));
m2->age = 28;

// Copy
memcpy(m2,&m1,sizeof(member));
// m2 age is '18'
printf("m2 age is '%d'\n",m2->age);
}

對比 Struct

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
struct _member{
char name[20];
int age;
};
typedef struct _member member;
void main()
{
// 非指標類
member m1;
memset(&m1, 0x00, sizeof(member));
m1.age = 18;

// 指標
member* m2 = malloc(sizeof(member));
memset(m2, 0x00, sizeof(member));
m2->age = 28;

// Compare
if (0 == memcpy(m2,&m1,sizeof(member))){
printf("exactly equal\n");
} else {
//√ unequal different count is '10'
printf("unequal different count is '%d'\n",memcpy(m2,&m1,sizeof(member)));
}
}