c语言用指针fp查找,重温C语言 -- 指针

1. C语言没有字符串,末尾带NUL字符的字符数组是对字符串的模拟;

2. C语言没有bool类型,可以引入stdbool.h使用布尔值。

#define true 1

3. 函数指针PROTOTYPE:

RETURN_TYPE  ( * FP_NAME )(PARAMS_TYPE)

函数指针可以实现方法的动态分派(?)

4.1 使用malloc创建数组

int *pv = (int * ) malloc( 5 * sizeof(int));

4.2 使用realloc调整size

char **newBuffer = realloc(buffer, max_length * 2);

4.3 指针表示数组: *(ptr_v + index)  // 等价于vector[index], 但无法完全取代。

5.  null NULL NUL

NUL:ASCII字符,即\0

NULL:  #define NULL ((void *) 0 )

null: 通过null指针常量来支持的一种抽象,可能非0。细节取决于实现。

6. wchar_t : 16bit / 32bit的宽字符串

7. 字面量字符串默认分配在字面量池中。

clang 、gcc中使用 -fwritable-strings 选项可以关闭字符串池。 微软vs中/GF选项会打开字符串池。

8.结构体:

8.1 定义

struct _node{

char * value;

struct _node * next;  // struct 关键字不可少

};

8.2 简化使用(直接使用别名)

typedef struct _node Node;

8.3 访问成员

直接使用结构体:使用 . 访问成员

Node p = getNode(); p.value;

使用指针:必须使用箭头

Node *q = &p; q->value;

解引用则可以使用. (*q).value;

#include

#include

struct _node{

char * value;

struct _node * next;

};

typedef struct _node Node;

Node newNode(char* content){

Node node;

node.value = content;

node.next = NULL;

return node;

}

int main(){

struct _node p;

p.value = "9999";

p.value = "3333";

printf("%s\n",p.value);

Node q;

q.value = "asdf";

printf("%s\n",q.value);

Node node = newNode("hello world");

Node * node_ref = &node;

printf("%s\n",node_ref -> value);

printf("%s\n",(*node_ref).value);

}

9. 指针安全问题

Address space layout randomization(ASLR)

内存位置被预测到而转移控制

see more: http://www.ibm.com/developerworks/cn/linux/1402_liumei_rilattack/

Data execution prevent (DEP)

执行不可执行区域的代码

10. 指针错误解引用

int num;

int *p

*p = #

11.别名: 指向了同一个地址的两个指针 alias

强别名:指针类型不允许类型转换的别名 strict alias

12. 回调:一个线程的事件导致了另一个线程的函数调用

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值