关于指针数组的

来源:百度知道 编辑:UC知道 时间:2024/05/20 05:02:09
1用指针数组
2编写函数
insert(char *name[],char *str,int *n)
name代表指针数组(其指向的字符串已经从大到小排好序),*n代表当前指针数组的实际长度。
函数的功能是:将str指向的字符串的地址插入到name中适当的位置,使得name数组中各个元素所指向的字符串是按照大小顺序排列的。插入后*n的值加1.
3main主函数的功能
指针数组的定义和初始赋值
输出指针数组所指向的字符串的内容
调用insert函数
输出更新后的指针数组所指向的字符串内容
4关于数组的操作尽量用指针
忘了说题目了 输入一个新的字符串,将所有字符串从达到小输出

输入"END"结束

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void insert ( char *name[], char *str, int *n )
{
int i;
char *newStr;
unsigned long addr, nextAddr;

newStr = (char*) malloc ( sizeof(char) * ( 1 + strlen ( str ) ) );
strcpy ( newStr, str );
for ( i = 0; i < *n && strcmp ( name[i], newStr ) > 0; i++ );
nextAddr = (unsigned long)newStr;
for ( ; i < *n; i++ )
{
addr = (unsigned long)name[i];
name[i] = (char*)nextAddr;
nextAddr = addr;
}
name[i] = (char*)nextAddr;
(*n)++;
}

void Print ( char *name[], int n )
{
int i;
for ( i = 0; i < n; i++ )
puts ( name[i] );
}

void Delete ( char *name[], int n )
{
int i;
for ( i = 0; i < n; i++ )
free ( name[i] );
}

void main()
{
const unsigned long len = 1