Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C string type definition

Tags:

c

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

string s;

And I thought to do it like:

#define string char *

Then for example in main function I have to write only string s; and if I type different variable name then it doesn't work.

How can I improve the definition or maybe how can I use "typedef" for this job if it's not a bad practice to do it so. Or is there any better approach to make variables type of string?
I was searching but I think I couldn't find the answer.

like image 448
redpix_ Avatar asked Jan 09 '23 05:01

redpix_


2 Answers

You can't make variables of type string in C, because "string" is not a type.

A "string" is, by definition, "a contiguous sequence of characters terminated by and including the first null character". It's not a data type, it's a data format.

An array of char may contain a string. A char* may point to a string. Neither of them is a string.

If you like, you can define

typedef char *string; /* not recommended */

but that's misleading, since a variable of type char*, as I mentioned, isn't a string.

The best practice is simply to use char* directly. This makes it clear that your variable is a pointer. It's also consistent with the way the standard library is defined; for example, the strlen function is declared as:

size_t strlen(const char *s);

It's also consistent with the way most experienced C programmers write code that deals with strings.

Because of the way C treats arrays (more or less as second-class citizens), arrays, including arrays that contain strings, are usually manipulated via pointers to their elements. We can use pointer arithmetic to traverse an array. Pretending that the pointer is the array, or that it is a string, is tempting, and might seem to make the code more understandable, but in the long run it just causes confusion.

A macro approach like

#define string char*

is even worse than a typedef. Macros are expanded as sequences of tokens; the processor doesn't know about the syntax of C declarations. So given the above definition, this:

string x, y;

expands to

char* x, y;

which defines x as a char* and y as a char. If you need a name for a type, typedef is almost always better than #define.

like image 146
Keith Thompson Avatar answered Jan 18 '23 03:01

Keith Thompson


You have to learn what the Preprocessor is. As for your problem, the right solution is

typedef char *string;

and if you want to use the preprocessor remove the semi colon and the s like this

#define string char *

In the second case the preprocessor will replace each occurence of string with char *, and hence if you declare

string x, y;

it will expand to

char *x, y;

where x if a pointer to char and y is simply a char, this is misleading and should be avoided.

One more thing, when working in C it is never a good Idea to hide the fact that some variable is a pointer, so things like

typedef SomeType *SomeTypeName;

where SomeType could be any type, are generally a bad idea, you can do something I've seen that clarifies this a little, you can append a P to SomeType, like this

typedef SomeType *SomeTypeP;

but I personally prefer the * to any of these typedefs

like image 41
Iharob Al Asimi Avatar answered Jan 18 '23 04:01

Iharob Al Asimi