C语言语法笔记 – 高级用法 指针数组 指针的指针 二维数组指针 结构体指针 链表
本文由arthinking发表于315 天前 ⁄ itzhai.com原创文章 ⁄ C语言 ⁄ 评论数 3 ⁄ 被围观 1,775 views+
指针数组:
在一个数组中,如果它的元素全部都是指针类型的数据,那么这个数组称为指针数组。

定义:类型名 *数组名[数组长度];

char *suit[3] = {"first","second","third"};

指向指针的指针:

如果一个变量存放的又是另一个指针变量的地址,则称这个指针变量为指向指针数据的指针变量,又称多级指针,简称为指向指针的指针。

定义:类型标识符 * * 指针变量名;

利用指针变量访问另一个变量就是“间接访问”,在一个指针变量中存放一个目标变量的地址,就是“单级间址”。

对于数组suit,由于数组名本身就表示地址,所以可以直接创建二级指针:

char **p;
p = suit;
#include<stdio.h>
void main(){
int a[5] = {1,3,5,7,9};
int *num[5],i;
int **p;
for(i=0;i<5;i++){
num[i] = &a[i];
}
p = num;
for(i=0;i<5;i++){
printf("%d",**p);
p++;
}
printf("\n");
}
指向二维数组的指针:

二维数组的地址:

a=a[0][0]=a[0] a+1=a[1] a[0]+1=a[0][1]

a是行指针,*a是列指针,**a表示a[0][0]的值,*a表示a[0]的地址。 a[1]+2 等价于 *(a+1)+2

在行指针前面加上一个*就转换为了列指针,若a和a+1是行指针,则*a和*(a+1)是列指针。

指向数组元素的指针变量

#include<stdio.h>
void main(){
int a[3][4] = {1,2,3,4,5,6,7,8,9,10,11,12}
int *p;
for(p = a[0]; p<a[0]+12; p++){
if((p-a[0])%4 == 0)
printf("\n");
printf("%4d",*p);
}
}
指向由m个元素构成的一维数组的指针变量

这种指针使得p+1不是指向a[0][1],而是指向a[1],p的增值以一位数组的长度为单位,这种指针称为行指针。

数据类型 (*指针变量名)[N];

int a[4][3], (*p)[3];

返回指针的函数
函数类型 * 函数名([形式参数类型声明表])
{
函数体
}
指向函数的指针

指向函数的指针的一般定义形式:

数据类型 (*指针变量名)(参数类型列表)

调用方式:

(*指针变量名)(实际参数列表)

int (*FunctionPointer)(int a);
FunctionPointer = func;   //func为函数名
(*FunctionPointer)(100);
带参数的main函数
void main(int argc, char *argv[]){
函数体
}

argc表示命令行参数个数,argv表示参数数组

指向结构体的指针
struct student *p;
struct student stu;
p = &stu;
//获取子元素的三种方法:
stu.name;
(*p).name;
p->name;  //指针的方法
指向结构体数组的指针

指向结构体数组的指针实际上与前面定义的指向二维数组的指针类似,可以理解为二位地址数组的行指针。

动态内存分配:

void *malloc(unsigned int size);

newptr = malloc(sizeof(struct node));

void free(void *p)

链表结构:
#include<stdio.h>
#define NULL 0
#define LEN sizeof(struct student)  /*定义节点的长度*/
#define NODE struct student
struct student
{
char no[5];
float score;
struct student *next;
};
struct student *create(void);
void printlist(struct student *head);
NODE * insert(NODE *head, NODE *new, int i);
NODE * dellist(NODE *head,char no[]);
void main(){
struct student *a;
struct student test1={"abc",1.0,NULL};
struct student *test2;
a = create();
printf("insert new node\n");
test2 = &test1;
a = insert(a,test2,2);
printlist(a);
printf("delete node\n");
a = dellist(a,"2");
printlist(a);
getch();
}
/*创建一个具有头结点的单链表,返回单链表的头指针*/
struct student *create(void){
struct student *head = NULL, *new1, *tail;
int count = 0;
for(;;)
{
new1 = (struct student *)malloc(LEN);  /*申请一个新结点的空间*/
printf("Input the number of student No.%d(5bytes): ",count + 1);
scanf("%5s",new1->no);
if(strcmp(new1->no, "*") == 0)   /*这里不用加取址符号,因为no就表示数组的首地址*/
{
free(new1);   /*释放最后申请的结点空间*/
break;  /*结束for语句*/
}
printf("Input the score of the student No.%d: ",count + 1);
scanf("%f",&new1->score);
count++;
/*将新结点插入到链表尾,并设置新的尾指针*/
if(count == 1){
head = new1;   /*是第一个结点,置头指针*/
} else
tail->next = new1;  /*不是第一个结点,将新结点插入到链表尾*/
tail = new1;    /*设置新的尾结点*/
}
/*置新结点的指针域为空*/
new1->next = NULL;
return(head);
}
/*输出链表*/
void printlist(struct student *head){
struct student *p;
p = head;
if(head == NULL) {
printf("List is empty!!!\n");
} else {
while(p!=NULL){
printf("%5s %4.1f\n", p->no,p->score);
p = p->next;
}
}
}
/*插入链表结点*/
NODE * insert(NODE *head, NODE *new, int i){
NODE *pointer;
/*将新结点插入到链表中*/
if(head == NULL){
head = new; new->next = NULL;
} else {
if(i == 0){
new -> next = head;
head = new;
} else {
pointer = head;
/*查找单链表的第i个结点(pointer指向它)*/
for(;pointer != NULL && i > 1; pointer = pointer->next,i--);
if(pointer == NULL)
printf("Out of the range,can't insert new node!\n");
else {  /*一般情况下pointer指向第i个结点*/
new -> next = pointer->next;
pointer->next = new;
}
}
}
return(head);
}
/*删除链表*/
NODE * dellist(NODE *head,char no[]){
NODE *front;    /*front表示要删除结点的前一个结点*/
NODE *cursor;   /*cursor表示当前要删除的结点*/
if(head == NULL) {  /*空链表*/
printf("\nList is empty\n");
return(head);
}
if(strcmp(head->no,no == 0)){  /*要删除的结点是表头结点*/
front = head;
head = head->next;
free(front);
} else {  /*非表头结点*/
front = head;
cursor = head->next;
/*通过循环移动到要删除的结点的位置*/
while(cursor != NULL && strcmp(cursor->no,no) != 0) {
front = cursor;
cursor = cursor ->next;
}
if(cursor != NULL){   /*找到需要删除的结点进行删除操作*/
front->next = cursor->next;
free(front);
} else {
printf("%5s has not been found!",*no);
}
}
return(head);
}
除了文章中有特别说明,均为IT宅原创文章,转载请以链接形式注明出处。

本文链接:http://www.itzhai.com/c-language-syntax-notes-advanced-usage-of-two-dimensional-array-of-pointers-to-a-pointer-list-pointer-array-pointer-structure.html

关键字: C语言, 指针, 链表
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。