您的位置:首页 > 其它

指针&数组&字符串&结构体

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指针,指向一个指针
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐