尝试实现一个简单的C语言string类型
用过`C++/Java/python/matlab/JS`等语言后,发现都能很轻松的使用string类型,而C只能这样:
char str[] = "hello world";
or
const char* str = "hello world";
如果只是一个string的类型的话,可以简单的自定义一个:
typedef const char* string;
但这样的写法并不便利,且这种用法不能修改变量中的数据,于是想写一个类似C++中的string类,但写了一会儿,发现用C实现有点难...
代码:
#ifndef STRING_H_INCLUDED
#define STRING_H_INCLUDED #include <string.h>
#include <stdio.h>
#include <stdlib.h> typedef const char* const_string;
typedef char* not_const_string; typedef struct {
const_string str; unsigned int size(){return (unsigned int)strlen(str);};
void show(){printf("%s\n", str);};
not_const_string sort() {
not_const_string not_con_str = (not_const_string)malloc(strlen(str));
strcpy(not_con_str, str);
for(int i = 1; not_con_str[i] != '\0'; i++) {
char s = not_con_str[i];
int j = i - 1;
while(not_con_str[j] != NULL && not_con_str[j] > s) {
not_con_str[j + 1] = not_con_str[j];
j--;
}
not_con_str[j + 1] = s;
}
return not_con_str;
}
}string; #endif // STRING_H_INCLUDED
test:
// 测试头文件 String.h
#include "String.h" int main()
{
string str = {"baced"}; // 必须加{} str.show();
printf("%d\n",str.size());
printf("%s\n", str.sort()); return 0;
}
运行结果:
baced
5
abcde
正常来说,用老版的编译器,比如用VC6.0,或用gcc编译会出现语法错误。我在codeblocks中虽然运行成功了,但这样写法是不能称为C语言的,不过是IDE用C++兼容了这样的写法,所以才这样写。
我看了一些资料后尝试用回调函数来实现,即在结构体中保存函数指针。
结果效果并不是很好,语法很麻烦:
#ifndef STRING_H_INCLUDED
#define STRING_H_INCLUDED #include <stdio.h>
#include <stdlib.h>
#include <string.h> typedef const char* const_string;
typedef char* not_const_string; typedef struct string string; struct string {
const_string str;
void (*init) (string* );
void (*show) (string* );
unsigned int (*size) (string* );
not_const_string (*sort) (const_string );
}; /* 定义函数 */
void init(string*self);
unsigned int size(string*self);
void show(string*self);
not_const_string sort(const_string str); void init(string*self) {
self->init = init;
self->show = show;
self->size = size;
self->sort = sort;
} unsigned int size(string*self) {
return (unsigned int)strlen(self->str);
} void show(string*self) {
printf("%s\n", self->str);
} not_const_string sort(const_string str) {
not_const_string not_con_str = (not_const_string)malloc(strlen(str));
strcpy(not_con_str, str);
for(int i = 1; not_con_str[i] != '\0'; i++) {
char s = not_con_str[i];
int j = i - 1;
while(not_con_str[j] != NULL && not_con_str[j] > s) {
not_con_str[j + 1] = not_con_str[j];
j--;
}
not_con_str[j + 1] = s;
}
return not_con_str;
} #endif // STRING_H_INCLUDED
测试:
#include "String.h" int main()
{
string str = {"baced"}; // 必须加{} --这里是C++中的结构体初始化写法 C语言好像不能这样写 但无妨 可以const_string str2 = "baced";然后再传给init函数 init(&str);
str.show(&str);
printf("%d\n",str.size(&str));
printf("%s\n", str.sort(str.str)); return 0;
}
运行结果:
baced
5
abcde
可以看到用着挺麻烦的...不如其他语言的简单和直观,经常拿来用的话是不太可能的。
# 2018-02-05
实际上,回调函数在许多语言中都可看见(比如python/java/C++等),C语言的一些工程源码中也随处可见,比如windows API 中的WNDCLASS结构体的lpfnWndProc成员就是一个函数指针,还有CreateThread 创建线程函数的第三个参数也是函数指针(创建线程的几个函数中的参数都有函数指针)。
CreateThread函数的函数指针的定义是这样的:
typedef (DWORD)(*PTHREAD_START_ROUTINE)(LPVOID lpThreadParameter);
其中DWORD是函数类型,其定义为:
typedef unsigned long DWORD;
*PTHREAD_START_ROUTINE 即是函数指针,LPVOID lpThreadParameter 则是传参。
在processthreadsapi.h中可看见其函数原型:
WINBASEAPI HANDLE WINAPI CreateThread (LPSECURITY_ATTRIBUTES lpThreadAttributes, SIZE_T dwStackSize, LPTHREAD_START_ROUTINE lpStartAddress, LPVOID lpParameter, DWORD dwCreationFlags, LPDWORD lpThreadId);
参考资料:
https://stackoverflow.com/questions/17052443/c-function-inside-struct
http://bbs.csdn.net/topics/390124447
https://www.zhihu.com/question/31519846?sort=created
尝试实现一个简单的C语言string类型的更多相关文章
- 利用OD破解一个简单的C语言程序
最近在学习汇编(看的是王爽老师的<汇编语言(第三版)>),然后想尝试使用OD(Ollydbg)软件破解一个简单的C语言程序练练手. 环境: C语言编译环境:VC++6.0 系统:在Wind ...
- 一个简单的C语言程序(详解)
C Primer Plus之一个简单的C语言程序(详解) #include <stdio.h> int main(void) //一个简单的 C程序 { int num; //定义一个名为 ...
- 初次尝试PHP——一个简单的对数据库操作的增删改查例子
第一次学习PHP,很多人说PHP是最好的语言,学习了一点点,还不敢说这样的话,不过确实蛮好用的. 做了一个简单的对数据库的增删改查的操作,主要是将四种操作写成了独立的函数,之后直接调用函数.以下是代码 ...
- 利用windows.h头文件写一个简单的C语言倒计时
今天写一个简单的倒计时函数 代码如下: #include<stdio.h> #include<windows.h> int main() { int i; printf(&qu ...
- 编写一个Car类,具有String类型的属性品牌,具有功能drive; 定义其子类Aodi和Benchi,具有属性:价格、型号;具有功能:变速; 定义主类E,在其main方法中分别创建Aodi和Benchi的对象并测试对象的特 性。
package com.hanqi.test; public class Car { //构造一个汽车的属性 private String name; //创建getter和setter方法 publ ...
- 为Python编写一个简单的C语言扩展模块
最近在看pytorh方面的东西,不得不承认现在这个东西比较火,有些小好奇,下载了代码发现其中计算部分基本都是C++写的,这真是要我对这个所谓Python语音编写的框架或者说是库感觉到一丢丢的小失落,细 ...
- 一个简单的C语言语法检查器的实现
我自己的实现方法的核心过程:首先用一个非终结符代表所有要检查的程序代码,然后根据文法将这个整体的符号不断展开,以拼凑成按检查的程序的顺序排列的终结符序列,能成功说明语法正确,否则有错误. 关键词:分词 ...
- C++ Daily 《4》----一个简单的 int to string 的方法
经常会在项目中用到 int to string, 之前一般用C语言的 sprintf, 发现C++ 中的 ostringstream 可以轻松完成这个任务. #include <iostream ...
- 一个简单的C语言题背后的故事
最近看到了一个C语言问题,是要计算出这个函数的输出: #include <stdio.h> int Test(int x,int y, int z){ printf("x,y,z ...
随机推荐
- php 基础 二维数组以某个重复值累加
$arr = array( array('id' => 123, 'name' => '张三', 'amount'=>'1'), array('id' => 123, 'nam ...
- document删除元素(节点)
不需要获取父id:document.getElementById("id").parentNode.removeChild(document.getElementById(&quo ...
- AspectRatio图片的宽高比、Card 卡片组件
一.AspectRatio 组件 AspectRatio 的作用是根据设置调整子元素 child 的宽高比. AspectRatio 首先会在布局限制条件允许的范围内尽可能的扩展,widget 的高度 ...
- Qt QML Component 学习笔记
简介 Component是Qt封装好的.只暴露必要接口的QML类型,可以重复利用.一个QML组件就像一个黑盒子,它通过属性.信号.函数和外部世界交互. 一个Component既可以定义在独立的QML文 ...
- 蚁人cp数
可怜的蚁人进入量子领域后,黄蜂女被灭霸的一个响指带走,导致可怜的蚁人困在了量子领域,为了生存,他们开始建造自己家园. 蚁人为了方便在这里生存,他们建造了自己火车站.某车站有N个人上车,其中M对是情侣, ...
- docker安装-单机/多机安装
操作系统ubuntu14.04 16.04 v单机安装步骤: #安装httpsca证书 apt-get install apt-transport-https ca-certificates #添 ...
- vs2015 C语言
1.C语言输入一行未知个数数字存入数组 参考:https://www.cnblogs.com/wd1001/p/4826855.html 2.VS2015编写C语言程序的流程 参考:http://c. ...
- GO Range
Go 语言中 range 关键字用于 for 循环中迭代数组(array).切片(slice).通道(channel)或集合(map)的元素.在数组和切片中它返回元素的索引和索引对应的值,在集合中返回 ...
- Cisco AP-Flexconnect配置结果
一个部署Flexconnect AP(印度)注册到远端WLC(上海)的例子:1.连接AP的交换机接口的配置: nterface GigabitEthernet0/4switchport access ...
- MySQL数据库备份还原
本文以CentOS 7 yum安装的MariaDB-server5.5为例,说明MySQL的几种 备份还原方式 将服务器A(192.168.1.100)上的数据库备份,还原到B(192.168.1.2 ...