LinuxC基础学习(知识点)_01
#include <stdio.h>
#include <stdlib.h>
int main()
{
char * p = NULL;
p = (char *)malloc(100);
strcpy(p,"hello world");
if (p != NULL){
free(p);
}
if (p != NULL)
{
free(p);
}
printf("Hello world!\n");
return 0;
}
当分配内存回收的时候,记得置空[free(p),p = NULL]
#include <stdio.h>
#include <stdlib.h>
int main (){
char * p = NULL;
strcpy(p,"hello world");
return 0;
}
char * p = (char *)malloc(100);
#include <stdio.h>
#include <stdlib.h>
int main(){
char * p = NULL;
char buf[128] = "hello";
char * temp = (char *)malloc(100);
strcpy(temp,"world");
p = buf;
p = temp;
return 0;
}
同一个指针可以操作不同地址的内存,C语言和Java语言的区别
#include <stdio.h>
#include <stdlib.h>
int change(int num){
num = 30;
return num;
}
void changep(int * num)
{
*num = 100;
}
int main (){
int num = 10;
int * p = #
*p = 20;
num = change(num);
changep(&num);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
void changep(char ** temp){
*temp = 0x300;
}
int main(){
char *temp_1 = NULL;
char ** temp_2 = NULL;
temp_1 = 0x111;
temp_2 = 0x222;
temp_1 = 0x1122;
temp_2 = &temp_1;
*temp_2 = 0x100;
printf("temp_1:%p\n",temp_1);
changep(&temp_1);
printf("temp_1:%p\n",temp_1);
}
修改一个变量的地址需要一级指针;修改一个一级指针的地址需要二级指针
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int Interface(char ** temp_1,int * temp_1_len,char ** temp_2,int * temp_2_len);
void Freestr(char ** temp_1,char ** temp_2);
int main()
{
char * one = NULL;
char * two = NULL;
int len_1 = 0;
int len_2 = 0;
int type = Interface(&one,&len_1,&two,&len_2);
if (type == 0){
printf(" application is error!\n");
return type;
}
printf("one->%s || %d\n",one,len_1);
printf("two->%s || %d\n",two,len_2);
Freestr(&one,&two);
return 0;
}
int Interface(char ** temp_1,int * temp_1_len,char ** temp_2,int * temp_2_len){
char * t_one = (char *)malloc(1024);
char * t_two = (char *)malloc(1024);
if (t_one == NULL || t_two == NULL)
return 0;
strcpy(t_one,"This is temp_1 memery is run ");
strcpy(t_two,"This is temp_two memery is tun too ");
int one_length = strlen(t_one);
int two_length = strlen(t_two);
*temp_1 = t_one;
*temp_2 = t_two;
*temp_1_len = one_length;
*temp_2_len = two_length;
return 1;
}
void Freestr(char ** temp_1,char ** temp_2){
if (*temp_1 == NULL) return ;
free(*temp_1);
*temp_1 = NULL;
if (*temp_2 == NULL) return ;
free(*temp_2);
*temp_2 = NULL;
}
char str[] = {'a','b','c','d'};
char string = "hello world";
int len = strlen(string);
int size = sizeof(string);
int i = 0;
for (i = 0;i < strlen(string);i++){
printf("%c",string[i]);
}
printf("\n");
char * p = NULL;
p = string;
for (i = 0;i < strlen(string);i++){
printf("%c",*(p+i));
}
printf("\n");
char str[] = "abcd";
str = 0x1234;
int * p = NULL;
p = 0x1234;
p = 0x1122;
char str[20] = "abcd";
char str2[] = "abcd";
char * pstr1 = "abcd";
char * pstr2 = (char *)malloc(100);
strcpy(pstr2,"abcd");
void str_copy(char * from,char * to){
/*1.==========================
for (;*from != '\0';from++,to++) *to = *from;
*to = '\0';
===========================*/
/*2.==========================
for (;*from != '\0';) *to++ = *from++;
*to = '\0';
===========================*/
/*3.==========================
while (*from != '\0'){
from++;
to++;
}
*to = '\0';
===========================*/
/*4.==========================
while ((*from = *to) != '\0' ){
from++;
to++;
}
===========================*/
/*5.==========================
while ((*to++ = *from++) != '\0');
===========================*/
/*6.==========================
while ((*to++ = *from++));
===========================*/
return ;
}
int getstrcount(char * str/*in*/,char * sub/*in*/,int * count/*out*/){
//记录出现的次数
int tempcount = 0;
//不要轻易去改变形参的值
char * temp = str;
if (str == NULL || sub == NULL || count == NULL)
return -1;
while (temp = strstr(temp,sub)){
printf("%s\n",temp);
tempcount++;
temp += strlen(sub);
if (*temp == '\0') break;
}
*count = tempcount;
return 0;
}
- 字符串两头堵模型[计算非空字符串的长度|字符串去空格]
int LeftandRightTrimSpaceInterface(char * str,char * buf){
if (str == NULL || buf == NULL)
return -1;
int start = 0;
int end = strlen(str) - 1;
while (isspace(str[start]) && str[start] != '\0') start++;
while (isspace(str[end]) && str[end] != '\0') end++;
strncpy(buf,str+start,end-start+1);
return 0;
}
int LeftandRightCountSpaceInterface(char * str,int * count){
if (str == NULL || count == NULL)
return -1;
int start = 0;
int end = strlen(str) - 1;
while (isspace(str[start]) && str[start] != '\0') start++;
while (isspace(str[end]) && str[end] != '\0') end--;
*count = end - start + 1;
return 0;
}
){
if (str == NULL)
return -1;
int start = 0;
int end = strlen(str) - 1;
for (;start < end;start++,end--){
char temp = str[start];
str[start] = str[end];
str[end] = temp;
}
return 0;
}
========================================================================*/
,char * string ){
if (str == NULL ||string = NULL || *str == '\0'){
*(string + strlen(string) + 1) = '\0';
return ;
}
*(string + strlen(str) - 1) = *str;
inreverse_2(str+1,string);
}
========================================================================*/
,char * string){
if (str == NULL || string == NULL || *str == '\0'){
return ;
}
inreverse_3(str+1,string);
printf("str = %c\n",*str);
strncat(string,str,1);
}
========================================================================*/
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
int getKeyByValue(char * str,char * key,char * value);
int getStringTrimSpace(char * str,char * value);
int main()
{
char value[20] = {0};
char * str = "name = xiaoming";
getKeyByValue(str,"name",value);
printf("value = %s\n",value);
return 0;
}
int getKeyByValue(char * str,char * key,char * value){
char * pstr = NULL;
if (str == NULL || key == NULL || value == NULL)
return -1;
pstr = str;
pstr = strstr(pstr,key);
if (pstr == NULL)
return -1;
pstr = pstr + strlen(key);
pstr = strstr(pstr,"=");
if (pstr == NULL)
return -1;
pstr = pstr + strlen("=");
int type =getStringTrimSpace(pstr,value);
if (type == -1)
return -1;
return 0;
}
int getStringTrimSpace(char * str,char * value){
if (str == NULL || value == NULL)
return -1;
int start = 0;
int end = strlen(str) - 1;
while (isspace(str[start]) && str[start] != '\0') start++;;
while (isspace(str[end]) && str[end] != '\0') end--;
strncpy(value,str+start,end-start+1);
return 0;
}
const int A = 10;
const char * str = NULL;
char * const str = NULL;
const char * const str;
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
char ** getMemery(int num);
void printMyArray(char ** pstr,int num);
void sortArray(char ** str,int num);
void freeArray(char *** str,int num);
#define NUM 5
int main()
{
char ** str = NULL;
str = getMemery(NUM);
printMyArray(str,NUM);
sortArray(str,NUM);
printMyArray(str,NUM);
freeArray(&str,NUM);
return 0;
}
char ** getMemery(int num){
int i = 0;
char ** pstr = NULL;
pstr = (char **)malloc(sizeof(char *) * num);
if (pstr == NULL)
return NULL;
for (i = 0;i < num;i++ )
{
pstr[i] = (char *)malloc(sizeof(char *) * 100);
sprintf(pstr[i],"%d%d%d",i+1,i+1,i+1);
}
return pstr;
}
void printMyArray(char ** pstr,int num){
int i = 0;
for (i = 0;i < num;i++){
printf("%s\n",pstr[i]);
}
}
void sortArray(char ** str,int num){
int i = 0,j = 0;
char * temp = NULL;
for ( i = 0;i < num;i++){
for ( j = i;j < num;j++){
if (strcmp(str[i],str[j]) < 0){
temp = str[i];
str[i] = str[j];
str[j] = temp;
}
}
}
}
void freeArray(char *** str,int num){
int i = 0;
char ** temp = NULL;
if (str == NULL)
return ;
temp = *str;
for (i = 0;i < num;i++)
{
free(temp[i]);
temp[i] = NULL;
}
free(temp);
*str = NULL;
}
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
int stringsplit(const char * str,char c,char *** pstr ,int * length);
int getstringleng(const char * str,char c,int * len,int * maxlen);
void printfArray(char ** str,int len);
int main()
{
char * str = "aaa,bbb,cccc,ddddd,abcdefg,111,2222,333333";
int len = 0;
char ** list = NULL;
stringsplit(str,',',&list,&len);
printfArray(list,len);
return 0;
}
void printfArray(char ** str,int len){
int i = 0;
for (i = 0;i < len;i++){
printf("[%s]\n",str[i]);
}
}
int stringsplit(const char * str,char c,char *** arr ,int * length){
char * newstr = (char *)malloc(strlen(str) + 2);
sprintf(newstr,"%s%c",str,c);
int len=0;
int maxlen = 0;
if (!getstringleng(newstr,c,&len,&maxlen))
return -1;
printf("len = %d\n",len);
printf("maxlen = %d\n",maxlen);
char ** pstr = NULL;
pstr = (char **)malloc(sizeof(char *) * len);
char * temp = newstr;
char * current = newstr;
int i = 0;
while (*newstr != '\0'){
current = strchr(current,c);
if (current != NULL){
if (current - temp > 0){
pstr[i] = (char *)malloc(sizeof(char) * maxlen + 1);
strncpy(pstr[i],temp,current - temp);
strncat(pstr[i],"\0",1);
temp = current = current + 1;
i++;
}else{
break;
}
}else{
break;
}
}
*arr = pstr;
*length = len;
free(newstr);
newstr = NULL;
return 0;
}
int getstringleng(const char * str,char c,int * len,int * maxlen){
if (str == NULL || len == NULL || maxlen == NULL)
return -1;
char * pstr = str;
char * temp = str;
int count = 0;
int index = 0;
while (*str != '\0'){
pstr = strchr(pstr,c);
if (pstr != NULL){
if (pstr - temp > 0){
int tp = (int)(pstr - temp);
if (tp > index)
index = tp;
count++;
temp = pstr = pstr + 1;
}else{
break;
}
}else{
break;
}
}
*len = count;
*maxlen = index;
return 0;
}