-3

For unknown reasons, I need to know how to replace the standard char a[10]; with string a; (Yes, I saw it in the CS50). So, how to create your own variable type named string?

Lundin
  • 195,001
  • 40
  • 254
  • 396
Argentum
  • 41
  • 5
  • Hi Argentum, this is not a forum and if you need help you need to provide some specific code and describe what have you already tried to solve your problems. – red Aug 30 '19 at 10:51
  • @Argentum, as red mentioned above, your question needs a lot of clarification. So please re edit the question and answer these questions: **What kind of user defined type would you like to make?** and **What have you tried so far?**. Try to be as specific as you can. Otherwise you will get an general answer. – Danilo Aug 30 '19 at 10:58
  • 1
    Try `typedef char string[10];` – S.S. Anne Aug 30 '19 at 11:42
  • But do not call it `string`, because that has another meaning in the context of C, so it could confuse people. (It means a sequence of characters in which the end of the desired data is marked with a null character.) You could call it a `char10` or something meaningful to your use of the type. – Eric Postpischil Aug 30 '19 at 13:00
  • 2
    @EricPostpischil The culprit is Harvard CS50 which teaches students to typedef their character arrays in C as `string`. Needless to say, it is a harmful programming course. – Lundin Aug 30 '19 at 13:58
  • I did a rollback to to OP's last edit, which contains the context that got edited away by "helpful" editors... – Lundin Aug 30 '19 at 14:00

4 Answers4

2

Amplifying on what @Lundin said in his answer:

One of the things that makes C hard to learn -- especially for students coming from other languages -- is that C does not have a first-class "string" type. Important as they are, strings in C are cobbled together out of arrays of char, and often accessed via char * pointers. The cobbling together is performed by a loose collaboration between the compiler, the programmer, and library functions like strcpy and printf.

Although I said that "strings are cobbled together out of arrays of char, and often accessed via char * pointers", this does not mean that C's string type is char [], and it also does not mean that C's string type is char *.

If you imagine that C has a first-class string type, handled for you automatically by the language just like char, int, and double, you will be badly confused and frustrated. And if you try to give yourself a typedef called string, this will not insulate you from that confusion, will not ease your frustration, will not make your life easier in any way. It will only cloud the issue still further.

Steve Summit
  • 45,437
  • 7
  • 70
  • 103
1

It is as simple as typedef char string[10];.

But please don't do this. Hiding arrays or pointers behind a typedef is very bad practice. The code gets much harder to read and you gain nothing from it. See Is it a good idea to typedef pointers? - the same arguments apply to arrays.

It is particularly bad to name the hidden array string since that is the exact spelling used by C++ std::string.

Please note that the CS50 is a bad course since it teaches you to do this. The SO community is sick and tired of "un-teaching" bad habits to the victims of this course. Stay away from questionable Internet tutorials in general.

If you want to create some manner of custom string type, the correct and proper way is to use a struct instead.

Lundin
  • 195,001
  • 40
  • 254
  • 396
  • I think the case against typedefs is even stronger than you make it. It's debatable whether pointer typedefs are *always* a bad idea -- arguably they can be useful under certain circumstances (as the highest-rated answer at the [linked question](https://stackoverflow.com/questions/750178) so argues. But array typedefs -- yeah, those *are* just about always a bad idea. (And the less said about [`jmp_buf`](https://linux.die.net/man/3/longjmp), the better.) – Steve Summit Aug 30 '19 at 14:37
  • @SteveSummit Agreed. I think the CS-50 aimed to hide away the complexity of C string handling from beginner programmers. But it's such a fundamental thing to the language, that the course isn't doing them a favour. – Lundin Aug 30 '19 at 16:23
0

User defined types in C are structures, unions, enumerations and functions. And it seems you can also include arrays in the list.

For example

struct Point
{
    int x;
    int y;
};

or

enum Dimension { N = 100 };
Point a[N];

In this example the array type is Point[N].

In fact any derived type (including pointers) can be considered as a user-defined type. The C Standard does not define and use the tern user-defined type.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
-1

User defined types are one of the following: struct, union or enum. For example, struct:

struct worker {
  int id;
  char firstName[255];
  char lastName[255];
};

To create an instance:

struct worker w1 = { 1234, "John", "Smith" };
S.S. Anne
  • 15,171
  • 8
  • 38
  • 76
yahavi
  • 5,149
  • 4
  • 23
  • 39