黑马程序员——C语言中指针与其他数据类型的结合使用

来源:互联网 发布:淘宝代运营sina 编辑:程序博客网 时间:2024/06/06 09:31

------<a href="http://www.itheima.com" target="blank">Java培训、Android培训、iOS培训、.Net培训</a>、期待与您交流! -------

1:指向基本数据类型变量的指针

<pre name="code" class="plain">#include <stdio.h>int main(int argc, const char *argv[]){int a = 5;int *p = &a;printf("p = %p\n", p);printf("*p = %d\n", *p);return 0;}


2:指向数值数组首元素的指针

int arr[5] = {};//数组名是地址常量,等于数组首元素地址,arr等价于&arr[0]

(1):初始化//arr等价于&arr[0],但是arr不能进行++运算!

int (*pArr) = &arr[0];//int (*pArr) = arr;

(2):赋值语句

int *pArr = NULL:

pArr = &arr[0]; //pArr = arr;

引用数组成员://pArr等价于arr

下标法:arr[i], pArr[i]

指针法:*(arr+i), *(pArr+i)

<span style="font-size:14px;">例:删除指定整数N</span>
<span style="font-size:14px;">#include <stdio.h>#include <stdlib.h>#include <time.h>#define NUM10int main(int argc, const char *argv[]){int arr[NUM] = {};int i = 0;int j = 0;int n = 0;//删除的数int t = 0;//统计删除了多少个srand(time(NULL));for (i=0; i<NUM; i++){//输入*(arr+i) = rand()%1;printf("%d ", *(arr+i));}printf("\n");scanf("%d", &n);for (i=0; i<NUM-t; i++){//遍历数组if (*(arr+i) == n){//找到nfor (j=i; j<NUM-1-t; j++){//循环前移*(arr+j) = *(arr+j+1);}t++;//计数+1i--;//防止不能删除相邻重复的成员}}for (i=0; i<NUM-t; i++){//输入printf("%d ", *(arr+i));}printf("\n");return 0;}</span>


3:指向字符数组首元素的指针

字符串的常见表示形式:

(1):通过字符数组来保存字符串

例子:char buf[] = “abcd”;//buf栈,”abcd”

(2):通过字符指针来保存字符串的首地址

例子:char *p = “abcd”;//p字符串的首地址,p栈,”abcd”数据段中的只读存储区

(3):通过指向字符数组首元素的指针

例子:char buf[] = “abcd”, char *p = buf;


4:指向函数的指针

指针函数

函数指针

5:指向结构体变量的指针

6:指向结构体数组的指针







0 0
原创粉丝点击