p303 使用指针给函数传递数据
2016-11-07 16:04
225 查看
不知道该怎么去学这些程序?
打了很多了变了,可是仍旧没有什么印象
自己不会但是又提不出来问题?
试试看:使用指针对字符串排序
使用数据排序的简单方法
介绍如何使用数组记号和指针
用更实际的方式练习使用指针给函数传递数据
malloc()函数需要指定要分配的内存字节数
malloc()函数返回所分配内存的字节数
calloc()
calloc():数组的元素个数,数组元素占用的字节数
realloc()
realloc()包含地址的指针,要分配的新内存的字节数
复制字符串与连接字符串的函数
-
复习第七章的程序7.14中排序字符串函数的修订版本。
源代码除了定义了main()函数之外,还定义了5个函数。
这里先列出来main()函数的实现代码和函数原型
后面将讨论其他5个函数的实现方式
字符串通过str_in()从键盘上读取。该函数的实现代码如下:
调用str_sort()函数把他们按升序存储
swap()函数的实现代码如下:
字符串数组排序完成后,就调用str_out()输出
释放已分配的所有堆内存,free_memory()
返回指针的风险
从函数中返回值
函数原型:函数头(函数返回值类型,函数名、(参数));
声明变量(数据类型 变量名 = 值)
声明指针
存储指针中的变量地址
指针前面加*号,输出变量的地址中的内容
指针前面不加*号,输出变量的地址
使用本地存储器
为了避免干扰变元指向的变量,可以考虑在函数IncomePlus()中使用本地存储器存储返回值,对这个例子做如下的小修改。
打了很多了变了,可是仍旧没有什么印象
自己不会但是又提不出来问题?
试试看:使用指针对字符串排序
使用数据排序的简单方法
介绍如何使用数组记号和指针
用更实际的方式练习使用指针给函数传递数据
#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; 返回新加薪水的地址 }
相关文章推荐
- 使用void指针给函数传递不同的数据类型
- Delphi使用C++库,向库中函数传递回调函数指针的问题解决
- 从汇编的角度理解为什么c样式函数使用时,实参尽量使用指针传递
- 4-18-1-使用指针交换数据、多线程参数传递、多线程通信、指针与数组
- 使用指针来将多维数组传递给函数
- [错误记录]关于指针传递获得数据使用错误问题....
- 第10章 指针(七) 应用1------函数间数据的双向传递
- 第10章 指针(七) 应用1------函数间数据的双向传递
- 第10章 指针(七) 应用1------函数间数据的双向传递
- 怎么样使用指针向函数传递一个多维数组
- 实例解析使用指针作为函数参数传递需要注意的问题
- C专家编程 十五 使用指针向函数传递一个多维数组
- c语言--二级指针在函数间的传递和使用
- 关于C语言通过函数传递多个数据————指针的应用
- 使用python向C语言的链接库传递数组、结构体、指针类型的数据
- SYD8801 UART使用说明【uart轮发数据代码实现】【 uart中断发数据代码实现】【RFSleep函数对串口的影响】【中断中调用串口打印函数有可能会造成死机】
- mysql---使用函数处理数据
- mysql---使用函数处理数据
- mysql---使用函数处理数据
- 如何在串口调试助手上,使用printf()函数显示十进制的数据及汉字说明(c语言版)