#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "cltsockekt.h"
//打桩
//1,客户端初始化环境
int cltSocket(void **handle)
{
return 0;
}
// 2,客户端发报文
int cltSocket_senddata(void *handle,unsigned char *buf,int buflen)
{
return 0;
}
//3,客户端收报文
int cltSocket_resvdata(void *handle,unsigned char*buf,int* buflen)
{
return 0;
}
//4,客户端销毁环境
int cltSocket_destory(void *handle)
{
return 0;
}
void main()
{
printf("hello..\n");
system("pause");
}
#ifndef _CLS_SOCKET_H_
#define _CLS_SOCKET_H_
//1,客户端初始化环境
int cltSocket(void **handle);
// 2,客户端发报文
int cltSocket_senddata(void *handle,unsigned char *buf,int buflen);
//3,客户端收报文
int cltSocket_resvdata(void *handle,unsigned char*buf,int* buflen);
//4,客户端销毁环境
int cltSocket_destory(void *handle);
#endif
demo_sort
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
void printfArray(int *a,int num)
{
int i=0;
for(i=0;i<num;i++)
{
printf("\n%d",a[i]);
}
}
//数组做函数参数,会退化为指针
//2,在形参里面出现的char buf[30],Int a[10] c/c++
//编译器会把它当作指针,也不回主动的分配内存
void printfArray3(int a[10])
{
int i=0;
int num1=sizeof(a);
int num2=sizeof(*a);
int num=sizeof(a)/sizeof(*a);
for(i=0;i<num;i++)
{
printf("\n%d",a[i]);
}
}
void main3()
{
int a[10]={1,23,43,4,5,6,7,8,90,45};
int i=0,j=0,temp=0;
printf("排序之前\n");
printfArray3(a);
for(i=0;i<10;i++)
{
for(j=i+1;j<10;j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
printf("排序之后\n");
printfArray3(a);
system("pause");
}
对参数的指针类型怎么理解?
理解角度从两个角度出发,
站在c/c++ 编译器的角度,对形参,如果是指针类型,c编译器只会分配四个自己的内存
指针的数据类型到底是什么?
指针的数据类型是指它所指向的内存空间的数据类型
指针的数据类型具有依附性
结论:指针的步长,根据所指内存空间类型来定。
while(*p1='\0'){
*p2=*p1;
p2++;
p1++;
}
void main()
{
char *p=NULL;
*p=100;
system("pause");
//运行出错
//写入位置0x0000 时发生访问冲突
}
void main()
{
{
char *p=NULL;//向null空间写数据
}
{
char *p=0x77;//向不知道的内存空间写数据
*p=100;
}
{
char *p="abcde";
*p='z';//常量区不能修改
}
void main()
{
//不断地修改指针变量的值含义
char buf[20];
char*p=NULL;
strcpy(buf,"123132567890");
p=&buf[0];
p=&buf[1];
p=&buf[2];
p=&buf[3];
}
system("pause");
}
#include <stdio.h>
#include <stdlib.h>
int g_a=10;//全局常量区
void main()
{
//printf("%x\n",&g_a);
int *a=&10;//10 字面量 不能取地址,没有在堆,栈,全局 可以按照放在代码区去理解
system("pause");
}
函数1调用函数2,函数1称为主调函数,函数2称为被调函数
规则1:Main(主掉函数) 分配的内存(在堆区,栈区,全局区)都可以在被调用函数里面使用吧。
规则2:在被调用函数里面分配的内存
1,如果在被调用函数里面的临时区(栈)分配内存,主调用函数是不能使用的。
char * getstring()
{
char buf[30];
strcpy(buf,"abcde");
return buf;
}
栈是开口向下的,地址越来越小。
堆是不断malloc 内存,首地址越来越大的。
p是谁的地址,*p 就间接的修改谁的值
如果p是实参的地址呢?
//抛砖
char *p="abcdefg";
//这行代码c++ 编译器会做两个操作。
1,先给abcdefg 分配内存
2,再给指针变量分配4个字节内存 指针指向谁,就把谁的地址赋给指针 字符串的首地址
怎么理解指针做函数参数
第一个角度:站在c/c++编译器角度,对形参,如果是指针类型,c编译器只会分配4个字节内存
铁律2:
通过*p /*p++ 来改变变量的值是指针存在的最大意义。
1,两码事:指针变量和它指向的内存变量
2,条件反射:指针指向某个变量,就是把某个变量地址赋给指针
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
//指针作为函数参数
int getLen(int *p)
{
*p=40;//间接修改实参的值
}
//间接赋值的应用场景有2个
//在函数指针 *p1++=*p2++;
//指针做函数参数,通过*p形参间接修改实参的值,这才是指针存在的最大意义。。
//这才是c语言特有的现象,才是指针的精华
//
void main()
{
int a=10;
int *p=NULL;
a=20;//直接修改
p=&a;//a 的地址赋值给p
*p=30;
printf("1a,%d\n",a);//间接赋值 如果p是a的地址,那么就间接修改a 的值。
getLen(&a);//函数调用
printf("2a,%d\n",a);
system("pause");
}
//间接赋值成立的三个条件
//1,条件1:定义了一个变量(实参)定义了一个变量(形参)
//条件2:建立关联
//条件3:*p 形参去间接的修改实参的值。
//Aop 切面编程
void main02()
{
int a=10;
int *p=NULL;
a=20;//直接修改
p=&a;//a 的地址赋值给p
*p=30;
printf("1a,%d\n",a);//间接赋值 如果p是a的地址,那么就间接修改a 的值。
getLen(&a);//函数调用
printf("2a,%d\n",a);
system("pause");
}
站在c++ 编译器的角度,*p 相当于我们程序员手工(显示)利用间接赋值,去操作内存
[] 怎么理解,只不过是c++ 编译器帮我们程序员做了一个*p 的操作
char buf[100]="abcde";
char *p=NULL;
for(int i=0;i<strlen(buf);i++){
printf("%c",p[i]);
}
for(int i=0i<strlen(buf);i++){
printf("%c,",*(p+i));
}
字符串拷贝
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void copy_str2(char* from,char * to)
{
for(;*from!='\0';from++,to++)
{
*to=*from;
}
*to='\0';
}
void copy_str3(char *from,char*to)
{
while(*from!='\0')
{
*to++=*from++;//后缀++ 优先级高于*p
}
*to='\0';
}
void copy_str4(char * from,char * to)
{
while((*to++=*from++)!='\0')//利用
{
}
}
void main()
{
char from[100]={0};
char to[100]={0};
copy_str2(from,to);
system("pause");
}
查找字符串中字符 和 他的出现次数
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void main001()
{
char *p="adabcd111222";
char *p2=NULL;
//p2=strstr(p,"111");
p2=strchr(p,'2');//char --->int 隐身类型转变
if(p2==NULL)
{
printf("没有找到\n");
}else{
printf("找到%s\n",p2);
}
system("pause");
}
//找到 字符串中的 "abcd" 出现的次数
void main()
{
char *p="adcdabcd111222abcd1abcd2";
char *p2=NULL;
char *sub="abcd";
int count=0;
do{
p=strstr(p,sub);
if(p==NULL)
{
//printf("没有找到\n");
break;
}else{
count++;
p=p+strlen(sub);
//printf("找到%s\n",p2);
}
}while(*p!='\0');
printf("count:%d\n",count);
system("pause");
}
用while 的方式如下:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void main()
{
int count=0;
char * sub="abcd";
char *p="11abcd1213abcd";
while(p=strstr(p,sub))
{
count++;
p=p+strlen(sub);
if(*p=='\0')
{
break;
}
}
printf("count:%d\n",count);
system("pause");
}
字符串trim
一。。。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
int trimSpace(char *mybuf)
{
int count=0;
char *p =mybuf;
int i=0;
int j=0;
j=strlen(p)-1;
printf("%d,%d\n",j,i);
while(isspace(p[i])&&p[i]!='\0')
{
printf("+++\n");
i++;
}
while(isspace(p[j])&&p[j]!='\0')
{
printf("---\n");
j--;
}
count=j-i+1;
printf("%d,%d\n",j,i);
printf("count:%d",count);
memcpy(mybuf,mybuf+i,count);//字符串拷贝
mybuf[count]='\0';//这里一定要加上'\0'
return 0;
}
void main()
{
int ret=0;
char bu=NULL;
char buf[]=" abcd ";
// char* p=" abcd ";
ret=trimSpace(buf);
if(ret!=0)
{
printf("func trimSpace error:%d\n",ret);
}else{
printf("buf:%s\n",buf);
}
system("pause");
}
void mainmmmm()
{
int count=0;
char *p ="fda ";
int i=0;
int j=0;
j=strlen(p)-1;
while(isspace(p[i])&&p[i]!='\0')
{
// x
i++;
}
while(isspace(p[j]&&p[j]!='\0'))
{
j--;
}
count=j-i+1;
printf("count:%d",count);
system("pause");
}
二。。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
//不是很好的做法
int no_ok_trimSpace(char *mybuf)
{
int count=0;
char *p =mybuf;
int i=0;
int j=0;
j=strlen(p)-1;
printf("%d,%d\n",j,i);
while(isspace(p[i])&&p[i]!='\0')
{
printf("+++\n");
i++;
}
while(isspace(p[j])&&p[j]!='\0')
{
printf("---\n");
j--;
}
count=j-i+1;
printf("%d,%d\n",j,i);
printf("count:%d",count);
memcpy(mybuf,mybuf+i,count);//字符串拷贝
mybuf[count]='\0';//这里一定要加上'\0'
return 0;
}
//一般情况下,不要修改输入的内存块的值.
int trimSpace_ok(char *mybuf,char*outbuf)
{
int count=0;
char *p =mybuf;
int i=0;
int j=0;
j=strlen(p)-1;
printf("%d,%d\n",j,i);
while(isspace(p[i])&&p[i]!='\0')
{
printf("+++\n");
i++;
}
while(isspace(p[j])&&p[j]!='\0')
{
printf("---\n");
j--;
}
count=j-i+1;
printf("%d,%d\n",j,i);
printf("count:%d",count);
memcpy(outbuf,mybuf+i,count);//字符串拷贝
outbuf[count]='\0';//这里一定要加上'\0'
return 0;
}
void main()
{
int ret=0;
char bu=NULL;
char buf2[100];
//char buf[]=" abcd ";
char* buf=" abcde ";//对于字符串分配内存有三种方式,可以在堆区,栈,全局(常量),你要知道你内存是怎么分配的。
//这里是全局的常量区,不能修改。 所以trimSpace 方式是不ok 的。
ret=trimSpace_ok(buf,buf2);
if(ret!=0)
{
printf("func trimSpace error:%d\n",ret);
}else{
printf("buf:%s\n",buf);
}
system("pause");
}
字符串反转
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void main()
{
char c;
//char* str="abcdefg"; 常量
char str[]="abcdefg";
int len=strlen(str);
char *p1=str;
char *p2=str+len-1;
while(p1<p2)
{
c=*p1;
*p1=*p2;
*p2=c;
p1++;
p2--;
}
printf("str:%s\n",str);
system("pause");
}
1,在c 中没有字符串这种类型,是通过字符串数组(char buf[100]) 去模拟
2,字符串和字符串数组的区别 是不是带有\0
根据key 去找value
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define _CRT_SECURE_NO_WARNINGS
//一般情况下不要修改输入的内存块的值
int trimSpace_ok2(char *mybuf, char *outbuf)
{
int count = 0;
int i = 0, j = 0;
char *p = mybuf;
j = strlen(p) -1;
while (isspace(p[i]) && p[i] != '\0')
{
i++;
}
while (isspace(p[j]) && j>0)
{
j--;
}
count = j-i +1;
//
printf("count:%d\n", count);
//void * __cdecl memcpy(void *, const void *, size_t);
memcpy(outbuf, mybuf+i, count);
outbuf[count] = '\0';
return 0;
//system("pause");
}
int getKeyByValude(char *keyvaluebuf /*in*/, char *keybuf /*in*/,
char *valuebuf /*in out*/, int * valuebuflen /*in out*/)
{
int rv = 0;
char tmp[2048*10];
char *p = NULL;
//1 在大字符串里面查找有么有关键字
p = strstr(keyvaluebuf, keybuf);
if (p==NULL)
{
return 0;
}
p = p + strlen(keybuf);
//2 在查找=号
p = strstr(keyvaluebuf, "=");
if (p==NULL)
{
return 0;
}
p = p + 1;
//3 去掉左右空格
rv = trimSpace_ok2(p, tmp);
if (rv != 0)
{
printf("func trimSpace_ok2() err:%d\n", rv);
return rv;
}
strcpy(valuebuf, tmp);
*valuebuflen = strlen(tmp);
return 0;
}
void main()
{
int rv = 0;
char keyvaluebuf[] = "ORACLE_name = itcast ";
char *keybuf = "ORACLE_name";
char valuebuf[1024];
int valuebuflen = 0;
//调用函数,要先判断是否出错
rv = getKeyByValude(keyvaluebuf, keybuf, valuebuf, &valuebuflen);
if (rv != 0)
{
printf("func getKeyByValude() err:%d", rv);
return ;
}
printf("valuebuf:%s\n", valuebuf);
printf("valuebuflen:%d\n", valuebuflen);
system("pause");
}
const
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void main()
{
const int *c;//const 放在* 左边,内存空间不能被写
int * const d=0;//表示const 修饰d,d 是指针变量 //常量指针,抛砖
//指针变量和他指向的内存空间变量是不同搞的概念
}
[] 和 * 的区别
//[] *
//[]----->*
//buf[i]--->buf[0+i]-------->*(p+i)------>p[i]
//站在c++ 编译器的角度,*p相当于我们程序员手工(显示)利用间接赋值,去操作内存
//[] 怎么理解,只不过c++编译器帮我们程序员做了一个*p 的操作