指针

char amessage[] = "now is the time";

char *pmessage = "now is the time";

区别:

数组amessage分配一段存储空间,指向同一个存储空间,而且始终指向这段地址,而这段存储空间放“now is the time”这段字符串。

而指针pmessge指向字符串常量"now is the time"的地址,这个地址可以改变,但是如果试图修改地址中内容就没有效果。


指针数组

char *lineptr[];

使用时,*lineptr++表示lineptr指向地址++,而(*lineptr)++表示所指向的值++


二维数组和指针数组的区别

int a[10][20];

int *b[10];

分配空间:

二维数组分配10 * 20 = 200个存储单元

指针数组分配10个指针的存储空间,并且没有初始化

数组长度:

指针数组的每一行的长度可以不一样

而二维数组的长度固定。


C程序设计语言上关于指针数组的示例:

对指针数组的每一行进行排序,步骤:

读取所有输入行

对文本进行排序

按次序打印文本行


存储空间分配alloc.c

/*
 * alloc.c
 *
 *  Created on: 2015-5-17
 *      Author: minghuang
 */
#include <stdio.h>

#define ALLOCSIZE	10000

static char allocbuf[ALLOCSIZE];

static char *allocp = allocbuf;

/*
 * 分配n大小的内存
 * 不足就返回指针 NULL
 */
char *alloc(int n) {
	if (ALLOCSIZE - (allocp - allocbuf) >= n) {
		allocp += n;
		return allocp - n;
	} else {
		printf("分配的存储空间不足!!!");
		return 0;
	}
}

/*
 * 释放存储空间
 */
void afree(char *p) {
	if (p >= allocbuf && p < allocbuf + ALLOCSIZE) {
		allocp = p;
	}
}

主程序main.c

#include <stdio.h>
#include <ctype.h>
#include "alloc.h"

#define MAXLINES	5000
#define MAXLEN	1000

//char lineptr[], 表示数组存放的都是char类型
//对比理解,[]的优先级比*高,char *lineptr[]表示数组存放的都是指向char类型的指针。
char *lineptr[MAXLINES];

int readlines(char *lineptr[], int maxlines);
void writelines(char *lineptr[], int nlines);
int obtainline(char line[], int maxlen);
void strcopy(char *s, char *t);
void quicksort(char *v[], int left, int right);
void swap(char *v[], int i, int j);
int strcmp(char *s, char *t);

int main(int argc, char *argv[]) {
	int i;
	for (i = 0; i < argc; i++) {
		printf("%s\n", argv[i]);
	}
	int nlines;
	if ((nlines = readlines(lineptr, MAXLINES)) >= 0) {
		quicksort(lineptr, 0, nlines - 1);
		printf("sort:\n");
		writelines(lineptr, nlines);
		return 0;
	} else {
		printf("error : input too big to sort\n");
		return 1;
	}
}

/*
 * 比较两个字符串
 */
int strcmp(char *s, char *t) {
	while(*s == *t && *s != '\0') {
		s++;
		t++;
	}
	return *s - *t;
}

/*
 * 交换指针数组中两个元素
 */
void swap(char *v[], int i, int j) {
	char *temp;
	temp = v[i];
	v[i] = v[j];
	v[j] = temp;
}

/*
 * 将字符串t复制给s
 */
void strcopy(char *s, char *t) {
	while (*s++ = *t++)
		;
}

/*
 * 获取每一行
 */
int obtainline(char line[], int maxlen) {
	int i, c;
	i = 0;
	while (--maxlen > 0 && (c = getchar()) != EOF && c != '\n') {
		line[i++] = c;
	}
	if (c == '\n') {
		line[i++] = c;
	}
	line[i] = '\0';
	return i;
}

/*
 * 读取每一行,将每一行保存在指针数组中
 * 通过alloc分配存储空间
 */
int readlines(char *lineptr[], int maxlines) {
	char line[MAXLEN];
	int len, nlines;
	nlines = 0;
	char *p;
	while ((len = obtainline(line, MAXLEN)) > 0) {
		if (nlines >= maxlines || (p = alloc(len)) == 0) {
			return -1;
		} else {
			printf("obtainline\n");
			line[len - 1] = '\0';
			strcopy(p, line);
			lineptr[nlines++] = p;
		}
	}
	return nlines;
}

void writelines(char *lineptr[], int nlines) {
	printf("writelines nlines = %d\n", nlines);
	while (nlines-- > 0) {
		printf("%s\n", *lineptr++);
	}
}

/*
* 快速排序:对于一个给定的数组,从中选择一个元素,以该元素为界将其余的元素划分为两个子集
*,一个子集中的所有元素小于这个数,另一个子集中的元素都大于或者等于这个数,当某一个子集的个数小于2,
*就不需要再次排序
*相应的指针数组,类似
*/
void quicksort(char *v[], int left, int right) {
	if (left >= right) {
		return;
	}
	int last = left;
    swap(v, left, (left + right) / 2);
    int i;
    for(i = left + 1; i <= right; i++) {
    	if(strcmp(v[left], v[i]) > 0) {
    		swap(v, ++last, i);
    	}
    }
    swap(v, left, last);
    quicksort(v, left, last - 1);
    quicksort(v, last + 1, right);
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值