简体   繁体   English

如何在 C 编程语言中创建自定义数据类型字符串?

[英]How to create custom datatype String in C Programming language?

I want to create a custom data type String.我想创建一个自定义数据类型字符串。 With that data type I want to use code like below:对于该数据类型,我想使用如下代码:

#include <stdio.h>

void main(void)
{
    String name = "MD A Barik";
    printf("%s", name);
}

How may I implement String datatype in C programming languages!如何在 C 编程语言中实现字符串数据类型!

A bunch of ways!一堆办法!

  1. Study C for many years well enough to write your own elaborate set of functions and structures (but not classes), such that you create something more or less useful for you, but that nobody else will ever be able to use.研究 C 多年,足以编写您自己精心设计的一组函数和结构(但不是类),这样您就可以创建或多或少对您有用的东西,但其他人将永远无法使用。

  2. Switch to C++.切换到 C++。

  3. Wish upon a star.向星星许愿。

Really, as comments above have noted, C does not support this and there's simply no way to pretend that it does.确实,正如上面的评论所指出的,C不支持这一点,而且根本无法假装它支持。 Efforts such as my #1 above would be a huge amount of work, and entirely counterproductive.像我上面的 #1 这样的努力将是大量的工作,而且完全适得其反。

It's a totally fair question to wish for, but you really need to take "no" for an answer here.这是一个完全公平的问题,但你真的需要在这里回答“不”。

typedef char* String;

or just use char* directly.或者直接使用char*

char* name = "MD A Barik";

You should be aware that this String behaves very differently than the std::string of c++.您应该知道此String的行为与 c++ 的std::string非常不同。 eg in c++ you can add two strings to concatenate, in C, you need to use strcat例如,在 c++ 中,您可以添加两个字符串进行连接,在 C 中,您需要使用strcat

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 C 编程语言中创建方阵? - How do I create a square matrix in the C programming language? 如何在 C 编程语言中在字符串中我需要的位置插入数字 - How to insert a number in the place I need in a string, in the C programming language C 编程语言,如何在代码中转换输入字符串并运行它[暂停] - C programming language,how to convert input string in code and run it [on hold] 如何打印出 C 编程语言的指针字符串的长度? - How to print out a pointer string's length for C programming language? 是否可以从字符串作为用户输入或从文件中以C / C ++(或任何其他语言)创建用户定义的数据类型 - Is it possible to create a user-defined datatype in a language like C/C++(or maybe any) from a string as user input or from file c语言如何停止输入 - How to stop input in programming c language 如何在C编程语言中比较两个数组? - How to compare two arrays in C programming language? 如何使用C编程语言使用预编译器初始化数组 - How to initialize a array with precompiler In C Programming Language 在C编程中定义并初始化此数据类型 - Define and initialise this datatype in C programming 如何判断标识符是否是C编程语言中的字符? - how to judge if an identifier is a character in C programming language?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM