News for hackers. Stuff that matters.
Re:How to implement strings by _kst_ on Saturday November 03, 2018 @04:29PM (#18372294)
The first sentence:

"The C programming language defines a string as char* ."

No, it doesn't. A string is by definition "a contiguous sequence of characters terminated by and including the first null character". A char* value may or may not be a pointer to a string.

(Confusing arrays and pointers is perhaps the most common mistake people make when talking about C.)

Re:How to implement strings by bergesenha on Saturday November 03, 2018 @05:31PM (#18372583)
And in C++, the type of a string literal expression is not const char* , but in fact const char (&)[], ie a reference to an array of chars. I was surprised to learn this as I always assigned a literal to a const char* , relying on pointer decay without knowing it.
Re:How to implement strings by _kst_ on Saturday November 03, 2018 @06:51PM (#18372941)
In C, the type of a string literal is

    char[n]
where n is the length of the literal plus 1.

In C++, it's "array of n const char", or

    const char[n]
It's not of a reference type. See the ISO C++11 standard, 2.4.15 [lex.string].

(In both C and C++, array expressions "decay" to pointer expressions in most contexts. But for example

    sizeof "hello"
is 6 (the size of the string), not the size of a char* pointer.
Re:How to implement strings by CodeArtisan on Saturday November 03, 2018 @07:08PM (#18372999)
A string in C is an implicit type; it's a specialization[1] of an array of characters. Another example of implicit type are Lists in Lisp (specialization of nested pairs).

[1] https://en.wikipedia.org/wiki/Specialization_(logic)

Re:How to implement strings by kazinator on Saturday November 03, 2018 @07:20PM (#18373036)
A string in C is entirely built upon convention; it exists in the words of a document only.

Common Lisp has a list class that you can specialize a CLOS method on.

Also:

  [1]> (typep nil 'list)
  T
  [2]> (typep '(a) 'list)
  T
  [3]> (typep 3 'list)
  NIL
And:

  [4]> (subtypep 'cons 'list)
  T ;
  T
  [5]> (subtypep 'null 'list)
  T ;
  T
So, kind of apples and oranges.
Re:How to implement strings by CodeArtisan on Saturday November 03, 2018 @08:47PM (#18373356)
I was referring to John McCarthy's definition of a list. IIRC, Common lisp's list type includes both proper and unproper lists. (typep (cons 1 2) 'list) evaluates to true but (length (cons 1 2)) throws a type error (not a list).
Re:How to implement strings by _kst_ on Saturday November 03, 2018 @11:24PM (#18373992)
I guess you could think of it as an "implicit type". I prefer to think of it as a data format rather than a data type. (It doesn't satisfy the meaning of "type" as C defines the word.)

char[n] (for constant n) is an array type. An object of type char[n] may or may not contain a string.