一、传递字符串:在函数的参数列表中,将参数声明为char指针即可实现通过字符串传递参数

    1、特征:

      1)、字符串以char指针的形式传递,一般是const指针传递;

      2)、使用字符数组声明字符串,调用字符串时直接使用数组名称即可;

      3)、使用字符数组声明字符串,调用字符串时直接在数组名前加&即可;

      4)、使用字符数组声明字符串,调用字符串时直接使用数组首元素地址即可;

      5)、使用字符指针声明字符串,调用字符串时直接使用指针即可;

    2、传递简单字符串:

 #include <stdio.h>
#include <string.h>
#include <stdlib.h> size_t stringLength(char *string){
size_t length = ;
while(*(string++)){
length++;
} return length;
} int main(int argc, char **argv)
{
char simpleArr[] = "Simple String!";
char *ptrSimpleArr = (char *)malloc(strlen("Simple String!") + );
strcpy(ptrSimpleArr, "Simple String!");
19
20 printf("使用数组名获取字符串:%s and %d\n", simpleArr, stringLength(simpleArr));
21 printf("使用数组名加&获取字符串:%s and %d\n", &simpleArr, stringLength(&simpleArr));
22 printf("使用数组首元素地址获取字符串:%s and %d\n", &simpleArr[0], stringLength(&simpleArr[0]));
23 printf("使用指针获取字符串:%s and %d\n", ptrSimpleArr, stringLength(ptrSimpleArr));
24
   return ;
}

    代码说明:

      1)、第16行代码是字符串的字符数组声明法

      2)、第20行代码是使用数组名获取字符串

      3)、第21行代码是使用数组名加&获取字符串

      4)、第22行代码是使用数组首元素地址获取字符串

      5)、第17行代码是字符串的字符指针声明法

      6)、第22行代码是使用指针获取字符串

      7)、stringLength函数实现了类似strlen函数的作用,返回制定字符串长度

    3、传递字符常量的指针:

    #include <stdio.h>
#include <string.h>
#include <stdlib.h> size_t stringLength(const char *string){
size_t length = ;
while(*(string++)){
length++;
} return length;
} int main(int argc, char **argv)
{
char simpleArr[] = "Simple String!";
char *ptrSimpleArr = (char *)malloc(strlen("Simple String!") + );
strcpy(ptrSimpleArr, "Simple String!"); printf("使用数组名获取字符串:%s and %d\n", simpleArr, stringLength(simpleArr));
printf("使用数组名加&获取字符串:%s and %d\n", &simpleArr, stringLength(&simpleArr));
printf("使用数组首元素地址获取字符串:%s and %d\n", &simpleArr[], stringLength(&simpleArr[]));
printf("使用指针获取字符串:%s and %d\n", ptrSimpleArr, stringLength(ptrSimpleArr)); return ;
}

    代码说明:

      1)、stringLength函数实现了类似strlen函数的作用,返回制定字符串长度,传入的是const的char指针,防止字符串被意外修改

  4、传递需要初始化的字符串:

  #include <stdio.h>
#include <stdlib.h>
#include <string.h> size_t stringLength(const char *string){
size_t length = ;
while(*(string++)){
length++;
} return length;
} size_t longInt(int i){
int tmp = ;
while(i / ){
i = i / ;
tmp++;
} return tmp;
} char *format(char *buffer, size_t size, const char *name, size_t quantity, size_t weight){
snprintf(buffer, size, "Item: %s Quantity: %u Weight: %u", name, quantity, weight); return buffer;
} char *formata(char *buffer, size_t size, const char *name, size_t quantity, size_t weight){
char *formatString = "Item: %s Quantity: %u Weight: %u";
//size_t formatStringLength = strlen(*formatString) - 6;
size_t formatStringLength = strlen( "Item: %s Quantity: %u Weight: %u") - ;
size_t nameLength = strlen(name);
size_t quantityN = longInt(quantity);
size_t weightN = longInt(weight);
size_t length = formatStringLength + nameLength + quantityN + weightN + ; if(buffer == NULL){
buffer = (char *)malloc(length);
size = length;
} snprintf(buffer, size, formatString, name, quantity, weight); return buffer;
} typedef struct forM{
char form1[];
char name[];
int quantity;
int weight;
int size;
} ForM; int main(int argc, char **argv)
{
ForM forM1;
char *form2 = "Item: %s Quantity: %u Weight: %u";
strcpy(forM1.form1, form2);
strcpy(forM1.name, "Axle");
forM1.quantity = ;
forM1.weight = ;
size_t size1 = stringLength(form2) + strlen(forM1.name) + longInt(forM1.quantity) + longInt(forM1.weight) - ;
printf("%s\n", format(forM1.form1, forM1.size, forM1.name, forM1.quantity, forM1.weight)); ForM forM2;
strcpy(forM2.name, "Axileguo");
forM2.quantity = ;
forM2.weight = ;
size_t size2 = stringLength(form2) + strlen(forM2.name) + longInt(forM2.quantity) + longInt(forM2.weight) - ;
printf("%s\n", format(forM2.form1, forM2.size, forM2.name, forM2.quantity, forM2.weight)); return ;
}

    代码说明:

      1)、通过引入结构体,使的编程思路更加清晰。

    写代码的一些技巧:

  /* *=+=+=+=+* *** *=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=
2 * 作者代号: *** :guochaoxxl
3 * 版权声明: *** :(魎魍魅魑)GPL3
4 * 联络信箱: *** :guochaoxxl@gmail.com
5 * 文档用途: *** :深入理解C指针
6 * 文档信息: *** :~/WORKM/StudyCode/CodeStudy/cnblogs_understanding_and_using_c_pointers/chapter5/testc11.c
7 * 修订时间: *** :2017年第41周 10月09日 星期一 上午08:58 (282天)
8 * 代码说明: *** :自行添加
9 * *+=+=+=+=* *** *+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h> char *returnALiteral1(int code){
switch(code){
case :
return "Boston Processing Center!";
break; case :
return "Denver Processing Center!";
break; case :
return "Atlanta Processing Center!";
break; case :
return "San Francisco Processing Center!";
break; default:
printf("Error!");
return "None!";
}
} char *returnALiteral2(int code){
char *str = (char *)malloc(sizeof(char) * ); switch(code){
case :
strcpy(str, "Boston Processing Center!");
break; case :
strcpy(str, "Denver Processing Center!");
break; case :
strcpy(str, "Atlanta Processing Center!");
break; case :
strcpy(str, "San Francisco Processing Center!");
break; default:
printf("Error!");
strcpy(str, "None!");
} return str;
} int main(int argc, char **argv)
{
int code;
printf("please input the code: ");
scanf("%d", &code);
printf("you input the code: %d\n", code);
printf("the result: %s\n", returnALiteral1(code)); printf("please input the code: ");
scanf("%d", &code);
printf("you input the code: %d\n", code);
printf("the result: %s\n", returnALiteral2(code)); return ;
}

    代码说明:

      1)、函数returnALiteral1的实现中,主要通过函数返回字符串常量,每一步都需要返回

      2)、函数returnALiteral2的实现中,主要通过函数返回字符指针,最后统一返回

      3)、函数returnALiteral2的实现中,切记需要先分配内存

      4)、建议使用函数returnALiteral2的实现方式

      5)、两个函数的实现不同,但是结果是相同的

33深入理解C指针之---通过字符串传递数据的更多相关文章

  1. 34深入理解C指针之---通过字符串传递函数

    一.通过字符串传递函数 1.定义:可以使用函数名(字符串)调用函数,也可以使用函数指针调用函数,将两者结合 2.特征: 1).在函数声明时使用函数指针 2).调用函数时使用函数名称(字符串) 3).可 ...

  2. 深入理解C指针之五:指针和字符串

    原文:深入理解C指针之五:指针和字符串 基础概念 字符串可以分配到内存的不同区域,通常使用指针来支持字符串操作.字符串是以ASCII字符NUL结尾的字符序列.ASCII字符NUL表示为\0.字符串通常 ...

  3. 31深入理解C指针之---指针和字符串

    一.字符串与指针 1.定义:使用字符指针表示字符串 2.特征: 1).可以直接使用字符串字面量初始化字符指针 2).声明后,赋值就只能使用字符串操作函数strcpy函数赋值 3).可以使用类似于数组的 ...

  4. C语言笔记 08_函数指针&回调函数&字符串&结构体&位域

    函数指针 函数指针是指向函数的指针变量. 通常我们说的指针变量是指向一个整型.字符型或数组等变量,而函数指针是指向函数. 函数指针可以像一般函数一样,用于调用函数.传递参数. 函数指针变量的声明: / ...

  5. 深入理解C指针之四:指针和数组

    原文:深入理解C指针之四:指针和数组 数组是C内建的基本数据结构,数组表示法和指针表示法紧密关联.一种常见的错误认识是数组和指针完全可以互换,尽管数组名字有时可以当做指针来用,但数组的名字不是指针.数 ...

  6. 深入理解C指针之一:初识指针

    原文:深入理解C指针之一:初识指针 简单来说,指针包含的就是内存地址.理解指针关键在于理解C的内存管理模式.C里面有三种内存: ①.静态全局内存(生命周期从程序开始到程序结束,全局变量作用域是全局,静 ...

  7. C 真正理解二级指针

    本文转载自CSDN博主liaoxinmeng,做数据结构时遇到指针方面的问题,想了许久,因此我觉得很有必要复习一下二级指针及其使用 正文如下: 指针是C语言的灵魂,我想对于一级指针大家应该都很熟悉,也 ...

  8. 《深入理解C指针》

    <深入理解C指针> 基本信息 原书名:Understanding and using C pointers 作者: (美)Richard Reese 译者: 陈晓亮 丛书名: 图灵程序设计 ...

  9. 深入理解C指针----学习笔记

      深入理解C指针     第1章 认识指针   理解指针的关键在于理解C程序如何管理内存,指针包含的就是内存地址.     1.1 指针和内存   C程序在编译后,以三种方式使用内存: 1. 静态. ...

随机推荐

  1. C++:100阶乘数组输出

    #include <iostream> using namespace std; int main(){ int i =1; int a[2048]={0}; while(i !=101) ...

  2. Applied Nonparametric Statistics-lec4

    Ref: https://onlinecourses.science.psu.edu/stat464/print/book/export/html/5 Two sample test 直接使用R的t- ...

  3. Python如何查看变量在内存中的地址

    在python中可以用id()函数获取对象的内存地址. 用法: object = 1 + 2 object -- 对象

  4. PTA 7-2 符号配对

    直接用栈模拟即可,数组可做,但因为这节数据结构是栈,为了期末考试还是手写一下栈的操作,值得注意的是,这道题用gets函数在PTA上会编译错误,用scanf("%[^\n]", st ...

  5. poj 2385 树上掉苹果问题 dp算法

    题意:有树1 树2 会掉苹果,奶牛去捡,只能移动w次,开始的时候在树1 问最多可以捡多少个苹果? 思路: dp[i][j]表示i分钟移动j次捡到苹果的最大值 实例分析 0,1  1,2...说明 偶数 ...

  6. cento命令之which、whereis、locate、find

    [which] 查看可执行文件的位置 语法: [root@localhost ~]# which 可执行文件名称 例如: [root@localhost ~]# which passwd /usr/b ...

  7. Centos7 安装 OwnCloud 私有云

    OwnCloud 一款文件主机服务软件,就是我们平时使用的云存储,不过这是在自己主机的服务器上建立属于自己的私有云,OwnCloud 使用AGPLv3协议发布.本项目是基于PHP和SQLite,MyS ...

  8. UML结构与解析——BUAA OO第四单元作业总结

    UML与解析架构 UML是什么 统一建模语言(英语:Unified Modeling Language,缩写 UML)是非专利的第三代建模和规约语言.UML是一种开放的方法,用于说明.可视化.构建和编 ...

  9. 使用supervisor方便调试程序

    调试过程中,有时需要修改代码,并时刻看到运行效果.如果每次终止程序又重启,会很麻烦. 可以使用supervisor,它可以监听代码文件,一旦发生改动会自动重启程序. 安装supervisor命令: n ...

  10. 两种图片延迟加载的方法总结jquery.scrollLoading.js与jquery.lazyload.js

    估计网上能查到的最多的两种图片延迟加载方法就是jquery.scrollLoading.js与jquery.lazyload.js了,其中jquery.lazyload.js的调用方法因为有网友爆出的 ...