您的位置:首页 > 其它

p303 使用指针给函数传递数据

2016-11-07 16:04 225 查看
不知道该怎么去学这些程序?

打了很多了变了,可是仍旧没有什么印象

自己不会但是又提不出来问题?

试试看:使用指针对字符串排序

使用数据排序的简单方法

介绍如何使用数组记号和指针

用更实际的方式练习使用指针给函数传递数据

#define _STDC_WANT_LIB_EXT1_ 1
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>

#define BUF_LEN 100
#define COUNT 5

int main()
{
char buf[BUF_LEN];
size_t str_count = 0;
size_t capacity = COUNT;
//指定数组的大小并初始化为0(数组的元素个数、数组元素占用的字节数)
char **pS = (char**)calloc(capacity, sizeof(char*));
char** psTemp = NULL;//指向指针的指针
char* pTemp = NULL;//指向指针的
size_t str_len = 0;
bool sorted = false;

printf("Enter strings to be sorted, one per line,Press Enter to end:\n");

//Read in all the string
char *ptr = NULL;//创建一个字符指针
while (true)
{
ptr = fgets(buf, BUF_LEN, stdin);//从键盘上读取字符
if (!ptr)
{
printf("Error reading string.\n");
free(pS);
pS = NULL;
return 1;
}

if (*ptr == '\n')
break;

if (str_count == capacity)
{
capacity += capacity / 4;
//包含地址的指针,包含要分配新内存的字节数
if (!(psTemp = (char**)realloc(pS, capacity)));
return 1;

pS = psTemp;//赋值语句
}
str_len = strnlen_s(buf,BUF_LEN) + 1;//确定字符串的长度
if (!(pS[str_count] = (char*)malloc(str_len)))
return 2;
strcpy_s(pS[str_count++], str_len,buf);
}

while (!sorted)
{
sorted = true;
for (size_t i = 0; i < str_count - 1; ++i)//重复制定的次数
{
if (strcmp(pS[i], pS[i + 1]) > 0)比较数组的两元素进行排序
{
sorted = false;
pTemp = pS[i];
pS[i] = pS[i + 1];
pS[i + 1] = pTemp;

}
}
}
printf("Your input sorted in ascending sequence is:\n\n");
for (size_t i = 0; i < str_count; ++i)
{
printf("%s",pS[i]);
free(pS[i]);
pS[i] = NULL;
}
free(pS);
pS = NULL;
return 0;
}


malloc()函数需要指定要分配的内存字节数

malloc()函数返回所分配内存的字节数

calloc()

calloc():数组的元素个数,数组元素占用的字节数

realloc()

realloc()包含地址的指针,要分配的新内存的字节数

复制字符串与连接字符串的函数

-

复习第七章的程序7.14中排序字符串函数的修订版本。

源代码除了定义了main()函数之外,还定义了5个函数。

这里先列出来main()函数的实现代码和函数原型

后面将讨论其他5个函数的实现方式

#define _STDC_WANT_LIB_EXT1_ 1
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#define BUF_LEN 256
#define INIT_NSTR 2
#define NSTR_INCR 2

char* str_in();
void str_sort(const char**, size_t);
void swap(const char**,const char**);
void str_out(const char* const*, size_t);
void free_memory(char**,size_t);

int main(void)
{
size_t pS_size = INIT_NSTR;
char **pS = (char**)calloc(pS_size, sizeof(char*));
if (!pS)
{
printf("Failed to allocate memory for string pointers.\n");
exit(1);
}
char  **pTemp = NULL;

size_t str_count = 0;
char *pStr = NULL;
printf("Enter ont string per line.Press Enter to end:\n");
while ((pStr = str_in()) != NULL)
{
if (str_count == pS_size)
{
pS_size += NSTR_INCR;
if (!(pTemp = (char**)realloc(pS, pS_size * sizeof(char*))))
{
printf("Memory allocation for array of strings failed.\n");
return 2;
}
pS = pTemp;
}
pS[str_count++] = pStr;
}
str_sort(pS,str_count);
str_out(pS,str_count);
free_memory(pS,str_count);
return 0;
}


字符串通过str_in()从键盘上读取。该函数的实现代码如下:

char* str_in(void)
{
char buf[BUF_LEN];
if(!gets_s(buf,BUF_LEN))
{
printf("\nError reading string.\n");
return NULL;
}

if(buf[0] == '\0')
return NULL;

size_t str_len = strnlen_s(buf,BUF_LEN) +1;
char *pString = NULL;

if(!pString)
{
printf("Memory allocation failure\n");
return NULL;
}

strcpy_s(pString,str_len,buf);
return pString;
}


调用str_sort()函数把他们按升序存储

void  str_out(const char **p, size_t n)
{
bool sorted = false;
while(!sorted)
{
sorted = true;
for(int i =0;i<n-1;++i)
{
if(strcmp(p[i],p[i+1])>0)
{
sorted = false;
swap(&p[i],&p[i+1]);
}
}
}
}


swap()函数的实现代码如下:

void  swap( const char** p1, const char** p2)
{
const char *pT = *p1;
*p1 = *p2;
*p2 = pT;
}


字符串数组排序完成后,就调用str_out()输出

void str_out(const char* const* pStr, size_t n)
{
printf("The sorted strings are: \n");
for(size_t i=0; i<n; ++i)
printf("%s\n",pStr[i]);
}


释放已分配的所有堆内存,free_memory()

void free_memory(char **pS,size_t n)
{
for(size_t i =0; i<n;++i)
{
free(pS[i]);
pS[i] = NULL;
}
free(pS);
pS = NULL;
}


返回指针的风险

从函数中返回值

#include <stdio.h>

long *IncomePlus(long* pPay);

int main()
{
long your_pay = 30000L;
long *pold_pay = &your_pay;
long *pnew_pay = NULL;
pnew_pay = IncomePlus(pold_pay);
printf("Old pay = $%ld\n",*pold_pay);
printf(" New pay = $%ld\n", *pnew_pay);
}

long* IncomePlus(long *pPay)
{
*pPay += 10000L;//通过指针改变值
return pPay;
}


函数原型:函数头(函数返回值类型,函数名、(参数));

声明变量(数据类型 变量名 = 值)

声明指针

存储指针中的变量地址

指针前面加*号,输出变量的地址中的内容

指针前面不加*号,输出变量的地址

使用本地存储器

为了避免干扰变元指向的变量,可以考虑在函数IncomePlus()中使用本地存储器存储返回值,对这个例子做如下的小修改。

#include <stdio.h>

long *IncomePlus(long* pPay);

int main()
{
long your_pay = 30000L;
long *pold_pay = &your_pay;
long *pnew_pay = NULL;
pnew_pay = IncomePlus(pold_pay);
printf("Old pay = $%ld\n",*pold_pay);
printf(" New pay = $%ld\n", *pnew_pay);
}

//Definition of function to increment pay
long* IncomePlus(long *pPay)
{
long pay = 0;  //本地变量
pay = *pPay + 10000L;  //给工资加薪
return &pPay;  返回新加薪水的地址
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  如何看懂程序