Determining the type of string C

I was just trying to create a string like when you could just write:

string s;

And I decided to do it like:

#define string char *

Then, for example, in the main function I should write only string s;, and if I enter another variable name, this will not work.

How can I improve the definition, or maybe how can I use a “typedef” for this work, if that’s not a bad practice. Or is there any better approach for creating string type variables?
I searched, but I could not find the answer.

+4
source share
3 answers

You cannot create variables of type string in C because "string" is not a type.

"" " , ". , .

char . A char* . .

,

typedef char *string; /* not recommended */

, char*, , .

char* . , . , ; , strlen :

size_t strlen(const char *s);

, C , .

- , C ( ), , , , . . , , , , .

,

#define string char*

, a typedef. ; C. , , :

string x, y;

char* x, y;

x char* y char. , typedef , #define.

+7

, Preprocessor. ,

typedef char *string;

, s,

#define string char *

string char *, , ,

string x, y;

char *x, y;

x, char y a char, .

, C, , , - , ,

typedef SomeType *SomeTypeName;

SomeType , , , -, , , P SomeType,

typedef SomeType *SomeTypeP;

* typedef s

+3

. string - ++. , ++ , , , .

: , , . , , using namespace std; , string ++ .

, , - char *, , .

-1

Source: https://habr.com/ru/post/1568181/


All Articles