一、传递字符串:在函数的参数列表中,将参数声明为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. [已解决] odoo12 菜单不显示,安装后多出菜单

    描述:odoo11中自定义模块写的,除了res.partner,res.users使用odoo自带的.其他的写了一个中国城市l10n_cn_city模型,一个账单模型(继承l10n_cn_city). ...

  2. Virt-install用法:

       #一般选项:指定虚拟机的名称.内存大小.VCPU个数及特性等 -n  NAME,  --name=NAME:虚拟机名称,需全局惟一: -r  MEMORY,  --ram=MEMORY:虚拟机内 ...

  3. Find the Longest Word in a String-freecodecamp算法题目

    Find the Longest Word in a String(找出最长单词) 要求 在句子中找出最长的单词,并返回它的长度 函数的返回值应该是一个数字. 思路 用.split(' ')将句子分隔 ...

  4. Protobuf有没有比JSON快5倍?用代码来击破pb性能神话

    转 http://www.sohu.com/a/136487507_505779 2017-04-26 07:58 程序设计 /58 /技术 导读:Google 的 Protocol Buffers ...

  5. Mysql中反引号和单引号的区别

    反引号,一般在ESC键的下方. 它是为了区分MYSQL的保留字与普通字符而引入的符号.举个例子:SELECT `select` FROM `test` WHERE select='字段值'在test表 ...

  6. 蓝牙stack bluez学习(1)Stack Architecture

    Bluez支持的features Core Specification 4.2 (GAP, L2CAP, RFCOMM, SDP, GATT) Classic Bluetooth (BR/EDR) B ...

  7. 私有DockerHub搭建

    docker简介 一个开源的应用容器引擎,可以用来打包程序,可以包入依赖环境,这样只需要提供docker image即可,类似于虚拟机,但是更轻量级. 几个概念: Paas,platform as a ...

  8. MySQL中文转换成拼音的函数

    CREATE DEFINER=`root`@`localhost` FUNCTION `fristPinyin`(`P_NAME` VARCHAR(255) CHARSET utf8) RETURNS ...

  9. Python 变量作用域 LEGB (上)—— Local,Global,Builtin

    Python 变量作用域的规则是 LEGB LEGB含义解释:L —— Local(function):函数内的名字空间E —— Enclosing function locals:外部嵌套函数的名字 ...

  10. LightOj:1422-Halloween Costumes

    传送门:http://www.lightoj.com/volume_showproblem.php?problem=1422 Halloween Costumes problem descriptio ...