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. 静态. ...
随机推荐
- nodejs 静态资源服务与接口代理跨域
首先需要 npm install express 和 npm install request 代码如下: const express = require('express'); const path ...
- [bzoj]1003: [ZJOI2006]物流运输
Description 物流公司要把一批货物从码头A运到码头B.由于货物量比较大,需要n天才能运完.货物运输过程中一般要转停好几个码头.物流公司通常会设计一条固定的运输路线,以便对整个运输过程实施严格 ...
- k8s的Pod控制器
pod的配置清单常见选项: apiVersion,kind,metadata,spec,status(只读) spec: containers: nodeSelector: nodeName: res ...
- 用宝塔软件在linux上自动安装php环境
1.确保是纯净系统 确保是干净的操作系统,没有安装过其它环境带的Apache/Nginx/php/MySQL,否则安装不上 2.sudo进行安装 yum install -y wget &&a ...
- 万门大学Python零基础10天进阶班视频教程
点击了解更多Python课程>>> 万门大学Python零基础10天进阶班视频教程 课程简介: 旨在通过两周的学习,让学生不仅能掌握python编程基础从而进行计算机程序的开发, 还 ...
- matplotlib学习记录 二
# 绘制10点到12点的每一分钟气温变化折线图 import random from matplotlib import pyplot as plt # 让matplotlib能够显示中文 plt.r ...
- Python学习笔记:wxPython(GUI图形用户界面)
wxPython是一套基于Python的第三方GUI插件,可用Python制作丰富的图形化界面程序. 安装:pip install wxPython 或者 网站下载安装https://pypi.org ...
- python - 接口自动化测试 - contants - 常量封装
# -*- coding:utf-8 -*- ''' @project: ApiAutoTest @author: Jimmy @file: contants.py @ide: PyCharm Com ...
- linux快速查看同局域网的其他在线主机
安装一个nmap工具,直接 nmap -sP 192.168.1.1/24 即可
- CF750E 线段树+矩阵乘矩阵加
题目描述 A string tt is called nice if a string "2017" occurs in tt as a subsequence but a str ...