用过`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类型的更多相关文章

  1. 利用OD破解一个简单的C语言程序

    最近在学习汇编(看的是王爽老师的<汇编语言(第三版)>),然后想尝试使用OD(Ollydbg)软件破解一个简单的C语言程序练练手. 环境: C语言编译环境:VC++6.0 系统:在Wind ...

  2. 一个简单的C语言程序(详解)

    C Primer Plus之一个简单的C语言程序(详解) #include <stdio.h> int main(void) //一个简单的 C程序 { int num; //定义一个名为 ...

  3. 初次尝试PHP——一个简单的对数据库操作的增删改查例子

    第一次学习PHP,很多人说PHP是最好的语言,学习了一点点,还不敢说这样的话,不过确实蛮好用的. 做了一个简单的对数据库的增删改查的操作,主要是将四种操作写成了独立的函数,之后直接调用函数.以下是代码 ...

  4. 利用windows.h头文件写一个简单的C语言倒计时

    今天写一个简单的倒计时函数 代码如下: #include<stdio.h> #include<windows.h> int main() { int i; printf(&qu ...

  5. 编写一个Car类,具有String类型的属性品牌,具有功能drive; 定义其子类Aodi和Benchi,具有属性:价格、型号;具有功能:变速; 定义主类E,在其main方法中分别创建Aodi和Benchi的对象并测试对象的特 性。

    package com.hanqi.test; public class Car { //构造一个汽车的属性 private String name; //创建getter和setter方法 publ ...

  6. 为Python编写一个简单的C语言扩展模块

    最近在看pytorh方面的东西,不得不承认现在这个东西比较火,有些小好奇,下载了代码发现其中计算部分基本都是C++写的,这真是要我对这个所谓Python语音编写的框架或者说是库感觉到一丢丢的小失落,细 ...

  7. 一个简单的C语言语法检查器的实现

    我自己的实现方法的核心过程:首先用一个非终结符代表所有要检查的程序代码,然后根据文法将这个整体的符号不断展开,以拼凑成按检查的程序的顺序排列的终结符序列,能成功说明语法正确,否则有错误. 关键词:分词 ...

  8. C++ Daily 《4》----一个简单的 int to string 的方法

    经常会在项目中用到 int to string, 之前一般用C语言的 sprintf, 发现C++ 中的 ostringstream 可以轻松完成这个任务. #include <iostream ...

  9. 一个简单的C语言题背后的故事

    最近看到了一个C语言问题,是要计算出这个函数的输出: #include <stdio.h> int Test(int x,int y, int z){ printf("x,y,z ...

随机推荐

  1. 一周搞定模拟电路P3_电容_记录

    1 电容的介绍 什么是电容 它有两个电极板,和中间板所夹的介质封装而成,具有特定功能的电子器件. 电容的作用 旁路.去耦.滤波和储能的作用 2 旁路电容的作用 1)使输入电压均匀化,减少噪声对后级的影 ...

  2. Hybrid App 开发快速指南

    链接:https://blog.csdn.net/valada/article/details/81639658

  3. C#加载XML方式

    //path:xml文件路径  SECSMessage:xml文件的根元素下的第一个子集元素 <SECSLibrary> <SECSMessage> <Descripti ...

  4. 在tomcat上部署项目

    1.部署项目的第一种方法(项目直接放入 webapps 目录中) 2.部署项目的第二种方法(修改 conf/server.xml 文件 ) 3.部署项目的第三种方法(apache-tomcat-7.0 ...

  5. shell中遍历数组的几种方式

    #!/bin/bash arr=( '你好') length=${#arr} echo "长度为:$length" # for 遍历 for item in ${arr[*]} d ...

  6. SQLite - C/C++接口 API(一)

    1.sqlite3_open(const char *filename, sqlite3 **ppDb) 该例程打开一个指向 SQLite 数据库文件的连接,返回一个用于其他 SQLite 程序的数据 ...

  7. ASP.NET Core搭建多层网站架构【9.1-使用Autofac代替原生的依赖注入】

    2020/01/30, ASP.NET Core 3.1, VS2019, Autofac.Extensions.DependencyInjection 5.0.1 摘要:基于ASP.NET Core ...

  8. JS中bool值转换与比较

    前言 首先需要知道的是,js中有6个值为false,分别是: 0, '', null, undefined, NaN 和 false, 其他(包括{}, [], Infinity)为true. 可以使 ...

  9. 【代码学习】PYTHON 抛出异常

    class ShortInputException(Exception): '''你定义的异常类.''' def __init__(self, length, atleast): Exception. ...

  10. PyQt5中Web操作与多线程定时器

    1.装载外部网页页面'''用web浏览器控件QWebEngineView控件显示网页PyQt5和Web的交互技术 同时使用python和Web开发程序,混合开发 Python+JavaScript+H ...