指针&数组&字符串&结构体
2018-03-02 13:47
232 查看
1.指针的定义
指针就是内存地址,指针变量就是存储地址的变量声明:数据类型: *指针变量的名字
int * p;
初始化:int *p = NULL;//空,0.0 \0 空指针
int i =10; int *p; p = &i; printf("addr: &i=%p p=%p\n",&i, p); printf("value: i=%d *p=%d\n",i, *p); 指行结果: addr: &i=0x7fff2d938e9c p=0x7fff2d938e9c value: i=10 *p=10
2.指针作为返回值
指针用作函数的返回值,表示返回一个地址注意:不要返回自动变量的地址,自动变量在函数结束后使用的内存会被释放
int *test(int n){ int *p = &n; return p; }
3.指针作为函数的参数
函数可以接受一个地址作为参数,函数可以修改实参的值,因为指向地址的内容被修改了.void swap(int *a,int *b){ int temp; temp = *a; *a = *b; *b = temp; }
4.字符串的表现形式
在C语方中,没有string类型字面值常量:”abcdef”以’\0’结束,存在于全局区,不可以改变
char arr[]:表示字符串类型的变量,后面加’\0’,存在于内存中函数栈,值可以改变
char * 指针存储地址,可以指向字面值,也可以指向char数组里面的无素
5.操作字符串的函数库
位于string.h里,在linux内核中源码实现存在于lib/string.c/** * strcpy - Copy a %NUL terminated string * @dest: Where to copy the string to * @src: Where to copy the string from */ char *strcpy(char *dest, const char *src) { char *tmp = dest; while ((*dest++ = *src++) != '\0') /* nothing */; return tmp; } /** * strcat - Append one %NUL-terminated string to another * @dest: The string to be appended to * @src: The string to append to it */ char *strcat(char *dest, const char *src) { char *tmp = dest; while (*dest) dest++; while ((*dest++ = *src++) != '\0') ; return tmp; } /** * strlen - Find the length of a string * @s: The string to be sized */ size_t strlen(const char *s) { const char *sc; for (sc = s; *sc != '\0'; ++sc) /* nothing */; return sc - s; } /** * strcmp - Compare two strings * @cs: One string * @ct: Another string */ int strcmp(const char *cs, const char *ct) { unsigned char c1, c2; while (1) { c1 = *cs++; c2 = *ct++; if (c1 != c2) return c1 < c2 ? -1 : 1; if (!c1) break; } return 0; }
6.结构体
结构体:不同类型数据的集合成员可以具有不同的类型
每个成员都有名字,通过名字来访问成员
使用一个结构体变量之前,必须先进行类型定义
typedef:给类型起别名
typedef struct
{
成员列表;
}别名;
#include <stdio.h> #include <string.h> typedef struct{ int id; char name[20]; char sex; int age; } student; int main(){ student s; s.id = 1; strcpy(s.name, "bshui"); s.sex = 'M'; s.age = 18; printf("id:%d name:%s sex:%c age:%d\n",s.id, s.name, s.sex, s.age); return 0; } 运行结果: id:1 name:bshui sex:M age:18
7.结构体作为函数的参数与返回值
如果使用结构体作为函数的参数与返回值会占用大量的内存,会消耗大量的时间一般使用结构体指针作为函数的参数和返回值,提高程序的效率
#include <stdio.h> #include <stdlib.h> #include <string.h> typedef struct{ int id; char name[20]; char sex; int age; } student; void show_info(student *s); int main(){ student *s = (student *)malloc(sizeof(student)); s->id = 1; strcpy(s->name, "bshui"); s->sex = 'M'; s->age = 18; show_info(s); return 0; } void show_info(student *s){ printf("id:%d name:%s sex:%c age:%d\n",s->id, s->name, s->sex, s->age); } 运行结果: id:1 name:bshui sex:M age:18
8.结构体的对齐与补齐
对齐:内存分配将结构体中的成员分配到内存的边界上,方便访问,每个成员都是从自身长度的整数倍开始存放补齐:整个的结构体长度必须保持为内部最长成员的整数倍
#include <stdio.h> #include <stdlib.h> typedef struct{ int i; // 4 short sh;//2 char c; //1 double d; //8 } s; typedef struct{ char a; //2 short b; //2 char d; //2 } t; int main(){ s s1; t t1; //对齐 printf("t:%ld\n", sizeof(t1)); //补齐 printf("s:%ld\n",sizeof(s1)); return 0; } 执行结果: t:6 s:16
9.联合体
可以有多个不同类型的成员组成通过成员的名字访问成员
所有成员共用起始地址相同的一段内存
大端:低位字节存储高位数据
小端:低位字节存储低位数据
#include <stdio.h> int main(){ union{ int i; char c; }u; u.i = 1; if(u.c==1) printf("little end\n"); else printf("big end\n"); return 0; }
10.指针类型区分
int i:定义一个整型变量int *q; q是一个指向整型变量的指针
int a
; a数组,包含n个整型的元素
int *q
; q数组,包含n个元素每个元素都是指向整型数据的指针
int (*q)
; q指针,指向一个数组,可以包含n个整型元素
int f(); f函数,返回值类型为int
int *f(); f函数,返回值类型指向整型数据的指针
int (*f)(); f函数指针,指向一个函数,函数返回值为int
itn **q; q指针,指向一个指针
相关文章推荐
- cf244D. Match & Catch 字符串hash (模板)或 后缀数组。。。
- <跟着结构之法一起学算法>求子数组的最大和
- 【MOOC·数据结构】求数组区间两个最大整数
- 类型之间进行隐式和显示转换&创建使用枚举&创建使用结构类型&创建使用数组&;如何处理字符串值
- 常用字符串函数 && char数组存字符串的问题
- Sorting&Searching 基于变位词的字符串数组排序 @CareerCup
- 数据结构——字符串和多维数组
- 把数组所有元素,按照“参数=参数值”的模式用“&”字符拼接成字符串
- <C#入门经典>学习笔记之结构、数组及字符串
- 20100706 学习记录:截取字符串最后一个字符 && asp textarea 多行数组分割处理
- 结构体&结构体指针&结构体指针数组
- Atitit.字节数组转字符串 base64 base16 Quoted-printable 编码原理设计 attilax 总结
- C++编程入门系列之二十七(数组、指针和字符串:指针变量的声明、地址相关运算--“*”和“&”)
- C语言根据特定的符号分割字符串 如:字符串"a,bbb,ccc"->数组[a,bbb,ccc]
- 解决从服务器获取的数组是 __NSCFConstantString以及&quot;&quot;没有空格字符串的问题
- 南京三星面试准备3--数组&基础数据结构
- <笔试><面试>编写一个排序函数,实现,既可以排序整形数组,又可以排序字符串。
- 字符串数组&字符指针理解(一)
- VC++中按特定标志字符实现字符串到字符串数组的分割(CString -> Cstring[])
- 【内功篇】指针&数组&字符串(四)