[英]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!一堆办法!
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 多年,足以编写您自己精心设计的一组函数和结构(但不是类),这样您就可以创建或多或少对您有用的东西,但其他人将永远无法使用。
Switch to C++.切换到 C++。
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.