33深入理解C指针之---通过字符串传递数据
一、传递字符串:在函数的参数列表中,将参数声明为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指针之---通过字符串传递数据的更多相关文章
- 34深入理解C指针之---通过字符串传递函数
一.通过字符串传递函数 1.定义:可以使用函数名(字符串)调用函数,也可以使用函数指针调用函数,将两者结合 2.特征: 1).在函数声明时使用函数指针 2).调用函数时使用函数名称(字符串) 3).可 ...
- 深入理解C指针之五:指针和字符串
原文:深入理解C指针之五:指针和字符串 基础概念 字符串可以分配到内存的不同区域,通常使用指针来支持字符串操作.字符串是以ASCII字符NUL结尾的字符序列.ASCII字符NUL表示为\0.字符串通常 ...
- 31深入理解C指针之---指针和字符串
一.字符串与指针 1.定义:使用字符指针表示字符串 2.特征: 1).可以直接使用字符串字面量初始化字符指针 2).声明后,赋值就只能使用字符串操作函数strcpy函数赋值 3).可以使用类似于数组的 ...
- C语言笔记 08_函数指针&回调函数&字符串&结构体&位域
函数指针 函数指针是指向函数的指针变量. 通常我们说的指针变量是指向一个整型.字符型或数组等变量,而函数指针是指向函数. 函数指针可以像一般函数一样,用于调用函数.传递参数. 函数指针变量的声明: / ...
- 深入理解C指针之四:指针和数组
原文:深入理解C指针之四:指针和数组 数组是C内建的基本数据结构,数组表示法和指针表示法紧密关联.一种常见的错误认识是数组和指针完全可以互换,尽管数组名字有时可以当做指针来用,但数组的名字不是指针.数 ...
- 深入理解C指针之一:初识指针
原文:深入理解C指针之一:初识指针 简单来说,指针包含的就是内存地址.理解指针关键在于理解C的内存管理模式.C里面有三种内存: ①.静态全局内存(生命周期从程序开始到程序结束,全局变量作用域是全局,静 ...
- C 真正理解二级指针
本文转载自CSDN博主liaoxinmeng,做数据结构时遇到指针方面的问题,想了许久,因此我觉得很有必要复习一下二级指针及其使用 正文如下: 指针是C语言的灵魂,我想对于一级指针大家应该都很熟悉,也 ...
- 《深入理解C指针》
<深入理解C指针> 基本信息 原书名:Understanding and using C pointers 作者: (美)Richard Reese 译者: 陈晓亮 丛书名: 图灵程序设计 ...
- 深入理解C指针----学习笔记
深入理解C指针 第1章 认识指针 理解指针的关键在于理解C程序如何管理内存,指针包含的就是内存地址. 1.1 指针和内存 C程序在编译后,以三种方式使用内存: 1. 静态. ...
随机推荐
- [vijos]P1642 班长的任务
背景 十八居士的毕业典礼(1) 描述 福州时代中学2009届十班同学毕业了,于是班长PRT开始筹办毕业晚会,但是由于条件有限,可能每个同学不能都去,但每个人都有一个权值,PRT希望来的同学们的权值总和 ...
- C++ Primer读书笔记(一)第一篇:C++概述,第一章:开始
1. 主要内容 介绍程序语言的核心思想和C++的基本概念. 印象比较深刻的就是分而治之(divide and conque)的分解思想. 2. 知识广场 1) C++ 文件后缀 cc, cpp,,cx ...
- javascript实现原生ajax的几种方法介绍
自从javascript有了各种框架之后,比如jquery,使用ajax已经变的相当简单了.但有时候为了追求简洁,可能项目中不需要加载jquery这种庞大的js插件.但又要使用到ajax这种功能该如何 ...
- exp分析
1 from pwn import* 2 3 local =1 4 debug = 1 5 6 if local: 7 p = process('./pwn1') 8 else: 9 p = remo ...
- 【HIHOCODER 1420】 Bigint Multiplication
描述 Given 2 nonnegative integers a and b, calculate a × b. 输入 One line with 2 integers a and b separa ...
- POJ:1328-Radar Installation
Radar Installation Time Limit: 1000MS Memory Limit: 10000K Description Assume the coasting is an inf ...
- Linux学习-检验软件正确性
md5sum / sha1sum / sha256sum 目前有多种机制可以计算文件的指纹码,我们选择使用较为广泛的 MD5, SHA1 或 SHA256 加密机 制来处理,我们拿NTP 软件来检查看 ...
- Linux学习-软件磁盘阵列 (Software RAID)
什么是 RAID 磁盘阵列全名是『 Redundant Arrays of Inexpensive Disks, RAID 』,英翻中的意思是:容错式廉价磁盘阵列.RAID 可以透过一个技术(软件或硬 ...
- POJ2594拐点弯的二分
开始读题没理解题意,以为就是覆盖,可是怎么交都不对... 我就气愤了,结果去百度了一下发现奶奶的这题的机器人是可以隔点瞭望的,例如1->2->3.2->4.5->2 这个图 ...
- loj2014 「SCOI2016」萌萌哒
神tm st表+并查集 #include <iostream> #include <cstdio> #include <cmath> using namespace ...