C语言指针
指针使用
声明
char* x;
int* y;
赋值
x = (char*)1;
指针的数据宽度
数据宽度都是4字节
地址符
&
char x;
char* p;
p = &x;
取出x的地址存入p
*符
char x=10;
char* p;
p = &x;
char y;
y = *p;//p=10
函数指针
// test.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
char s[]={
0x55,0x8B,0xEC,0x83,0xEC,0x40,0x53,0x56,
0x57,0x8D,0x7D,0xC0,0xB9,0x10,0x00,0x00,
0x00,0xB8,0xCC,0xCC,0xCC,0xCC,0xF3,0xAB,
0x8B,0x45,0x08,0x03,0x45,0x0C,0x5F,0x5E,0x5B,0x8B,0xE5,0x5D,0xC3};
int main(int argc, char* argv[])
{
int (*plus)(int,int);
plus = (int(*)(int,int))&s;
printf("%d\n",plus(1,19));
return 0;
}
等价于
#include "stdafx.h"
int plus(int x,int y){
return x+y;
}
int main(int argc, char* argv[])
{
printf("%d\n",plus(1,19));
return 0;
}
C语言位运算
1.算数移位指令(SAL/SAR)
SAL EAX,1
SAL AX,1
SAL最高位放到CF里面,最低位补0 SAR最低位放到CF里面,高位处补符号位
2.逻辑移位指令(SHL,SHR)
SHL EAX,1
空位补0
3.循环位移(ROL,ROR)
ROR al,1
如果al=1000 0001,移完变成0000 0011
4.带进位的循环移位指令(RCL,RCR)
和上面的区别就是把CF位补到圈里
C语言编译相关
从C到exe
C -> 替换 -> 编译 -> 链接 -> 二进制代码
头文件的作用
比如说自己写了一个test.cpp,里面写了一个函数叫plus,现在需要在另一个文件test2.cpp中调用这个plus函数,则需要先在test.h中声明这个plus函数,然后在test2.cpp中
#include "test.h"
内存动态申请
malloc
malloc申请到的内存空间是在堆当中的,使用完成后一定要记得释放内存空间。 例:
ptr = (int *)malloc(sizeof(int)*128);//申请长度为128个int长度的内存空间,并把初始地址存入ptr这个指针。
if (ptr==NULL){
return 1;
}
memset(ptr,0,sizeof(int)*128); //初始化
(*ptr) = 1; //使用
free (ptr); //释放
ptr = NULL;
读一个文件并原样写入另一个文件
// 本程序打开一个notepad.exe复制一份完全相同的notepad2.exe
#include "stdafx.h"
#include<stdlib.h>
int main(int argc, char* argv[])
{
FILE *fp;
char *fw;
int size=0;
fp=fopen("C:\\Users\\crls\\Desktop\\VC6.0green\\MyProjects\\PEReader\\Debug\\notepad.exe","rb");
if (fp==NULL){
printf("Openfile error");
return NULL;
}else{
fseek(fp,0,SEEK_END);
size = ftell(fp);
printf("%d",size);
fseek(fp,0,SEEK_SET);
fw = (char *)malloc(sizeof(char)*size);
if (fw==NULL){
printf("malloc error!\n");
return NULL;
}
size_t n = fread(fw, 1, size, fp);
if(!n){
printf(" 读取数据失败! ");
free(fw);
fclose(fp);
return NULL;
}
}
FILE *newfp;
if (newfp==NULL){
printf("malloc error!\n");
return 1;
}
newfp = fopen("C:\\Users\\crls\\Desktop\\VC6.0green\\MyProjects\\PEReader\\Debug\\notepad2.exe","wb");
fwrite(fw , 1 , size , newfp);
fclose(newfp);
fclose(fp);
free(fw);
}
联合类型
如果需要定义多个类型,但是每次只会用一个类型,为了节省空间就可以使用union联合类型
union test{
int x;
char y;
};