-3

C is an old language. For instance, did you know that there would be no problem to use the . operator instead of the -> operator with pointers and that the -> was maintained for legacy reasons? see here.

Studying several C code, one can notice that most of the time structs are passed around using pointers instead of by value. Arrays are also passed by pointer.

Would be possible to change the syntax to pass structures by default as pointers? For instance, consider the following example:

struct Point {
    float x;
    float y;
};

struct Line {
    Point! p0; // ! means that we want a value, not a pointer
    Point! p1;
};

struct Line2 {
    Point p0; // would be the same as Point*
    Point p1;

}

void foobar(Line line) { // would mean Line* line
    ...
}

void foobaz(Line! line) { // would mean Line line
    ...
}

Let's not consider readability, for instance, some argue that -> makes explicit that is a pointer. That's not the point here.

Here is the question: what possible problems or corner cases could arise from using such syntax? For example: 'using such syntax, it would be impossible to do this or that'

Java actually works similar to this proposal: everything is a pointer under the hood. The difference is that Java has garbage collector and objects are always allocated in the heap (the object itself, not the 'pointer') while in C structs can be allocated in the stack (although most of the time I see allocated in the heap).

If one wonders why I am asking this, it is because I am creating a programming language for low level development and I want to optimize the syntax for the common case. Although I like the C language syntax itself, I've got frustated by having to use headers (update function signature in two places, for instance), lack of support for function overload (I don't like to write int_list_add, float_list_add) and similar.


Thank you all who already replied, but I am not trying to modify C. This is a research for my language that I don't have pretension to be used by others.

My language is for low-level development trying as much as possible to use high level stuff like OO stuff, high level functions, closures and so on. It won't be garbaged collector, using a memory model similar to C (similar, not equal).

The language will have pointers just as C: you can get the address of some variable, deference a pointer.

If it is so important to ask the question that I asked, here go the syntax of my language which, by the way, it is totally different from C, a proof that I am not trying to create a C clone.

record Point:
    x : float
    y : float

record Line:
    p0 : Point!
    p1 : Point!

record Line2:
    p0 : Point
    p1 : Point


function foobar : void
    @line : Line

function foobaz : void
    @line : Line!

As I've said, I want to optime the syntax of my language for the common case. So if I am going to pass by pointer almost all the time, I don't want to be using a * again, and again and again. The only problem (and this has nothing to do with my programming skills, but is philosofical issue) is that I can't see problems that could arise from not specifying explicitly that is a pointer.

hdhzero
  • 31
  • 6
  • 2
    If you are frustrated with no function overloading you should use C++ instead. – mustachioed Feb 08 '18 at 21:40
  • Your idea is wrong. It distinguish between the structure and pointer which has to dereferenced. You can use the dot if you dereference tho pointer yourself (*p). Field – 0___________ Feb 08 '18 at 21:43
  • On your `Point` and `Line` example: what if `Point` is a `typedef`? – Jongware Feb 08 '18 at 21:44
  • I don't get it. What would be the difference between `Line *line` and `Line! line` other than syntax? – Pablo Feb 08 '18 at 21:49
  • @MustacheMoses, C++ has the same "problem" of having to define headers and the syntax, regarding pointers and struct, is the almost the same as C. – hdhzero Feb 08 '18 at 21:49
  • @PeterJ_01, reads the link in 'see here' that I posted in my question – hdhzero Feb 08 '18 at 21:50
  • 1
    *For instance, did you know that there would be no problem to use the . operator instead of the -> operator with pointers* -- "no problem" is a presumptive characterization. It would have been syntactically feasible, yes, but that's not the same thing. *and that the -> was maintained for legacy reasons?* -- **that's** a mischaracterization. The `->` operator was introduced for convenience, as K&R will tell you (the 1978 1st edition calls it "new"). That they chose `->` instead of re-using `.` is a perfectly reasonable design decision. – John Bollinger Feb 08 '18 at 22:04
  • 2
    C's been around for almost 50 years, and such a change would break a *lot* of legacy code. Whether it's useful on its own merits or not (I'm dubious, myself), it would never happen for that reason alone. – John Bode Feb 08 '18 at 22:05
  • 1
    I think you need to take a look at Go and at Rust. – Richard Chambers Feb 08 '18 at 22:09
  • Oddly enough, [Java seems to be coming at this issue from the other direction](http://openjdk.java.net/jeps/169). It seems unlikely that it will end up adopting syntax like that proposed by the OP, but I'm a bit amused that adherents of a language that has never provided for other than indirect access to values of aggregate type have conceived a desire for direct access, such as C has always offered. – John Bollinger Feb 08 '18 at 22:17
  • @RichardChambers I agree. The OP would like the features in those languages. IMO. – mustachioed Feb 08 '18 at 22:18
  • Would the `struct Point` type contain `x` and `y` as pointers? Why not if `struct Line2` contains `p0` and `p1` as pointers? Introducing inconsistencies like that would be unfortunate. Making `struct Point` contain two 8-byte pointers to two 4-byte values instead of just containing two 4-byte values would be unconscionable to C programmers. I'm very much in the "if you want a different/better C, go and use one — or write your own". There are many useful changes that could be made to C; this doesn't look like one of them. I see no advantage to it, and it probably breaks backwards compatibility. – Jonathan Leffler Feb 08 '18 at 22:47
  • And if you're writing your own language, you don't have to be shackled by C at all — unless you want to be. But the language you're writing isn't C. – Jonathan Leffler Feb 08 '18 at 22:49
  • @JonathanLeffler. No, Point would store x and y as values because they are primitive types. Please, all of you guys and girls. I am not proposing a modification to C. This is about my language. I just posted the question with C syntax because many people know the syntax and semantics of it. But no one besides me knows the syntax and semantics of my language. What would be the point in posting an example in a language that no one knows? – hdhzero Feb 08 '18 at 23:26
  • @JohnBollinger, what I tried to say regarding the -> operator is that the compiler, in modern C, would be smart enough to figure out that an automatic dereference should take place. And yes, it would break compatibility of code if my "proposal" should be accepted. The problem is: it is not a proposal, it is not about C. It is about my language that has pointers that work similar to C's pointers, ok? – hdhzero Feb 08 '18 at 23:29
  • 2
    What's the point of asking a question about hypothetical semantics? You can do as you wish; there are *LOTS* of languages you can look at for exemplars. The primitive vs non-primitive type distinction in Java is painful to my eyes — but I'm not a competent Java programmer, not least because of that pain. It smacks of weirdness, all the way down the line. The question is no longer of interest to me. I may or may not remove some of my diatribes. – Jonathan Leffler Feb 08 '18 at 23:29
  • @Pablo, that's exactly the point: syntax. Most of the time I see functions in C code, structures are passed by pointer. I am just wondering if would be possible to pass by pointer by default so I don't have to use a * everytime. Seems that I need to stress this out: it is for my language, not for C. I am not proposing a modification to C's syntax – hdhzero Feb 08 '18 at 23:38
  • @JonathanLeffler, I know I can do as I wish, but to a limit. I had tried some syntax that after some development proved to bring more headaches than benefits. That's why I came to SO to ask more experienced people than me what could go wrong with the syntax I proposed. – hdhzero Feb 08 '18 at 23:45
  • 5
    The problem is your question title — _"Would it be possible in C to pass by pointer by default?"_ Titles are awfully important; they set the context for everything else. You mean _"Would it be possible in a language that isn't C to pass by pointer by default?"_. The answers to both versions of the title are trivial, but also different — "No" for the current title, "Yes, of course; why on earth would you think otherwise?" for the revised title, which seems to be the question you really want to ask. (Of course, attaching the [tag:c] tag to the question would be irrelevant tagging.) – Jonathan Leffler Feb 08 '18 at 23:55
  • 2
    @hdhzero If it's your own language, then you set the rules. In your title you asked about C, not about your own language. I commented on the C aspect of the question. – Pablo Feb 09 '18 at 01:32

1 Answers1

6

You can change the syntax and the semantics (actually; much more important than just syntax) of C, but you should not call that language C anymore.

C is a standard language; read first the wikipage on C11 and the n1570 draft (or buy, from ISO or your national standard body, the paper equivalent)

I am not a lawyer, and I don't know if and how ISO is legally protecting the standard. (However, be sure that if you call your stuff C, people would surely laugth at you).

For example: 'using such syntax, it would be impossible to do this or that'

Nothing would be impossible (both your language and the C language are Turing-complete). Some things might become more inconvenient.

BTW, you don't describe precisely enough what you are thinking about (examples are not describing programming languages, and you don't explain semantics aspects). So we can't comment on it.

Read several programming languages specifications, including not only n1570 but also n3337 (close to C++11 specification) and of course R5RS (a short, well written, specification of some Scheme dialect)

At last, be sure to implement your programming language (and name it differently than C). I recommend to implement it as some free software (e.g. on github) and to bootstrap your compiler.

So try to specify your language as well as C, or C++, or Scheme are specified. Then implement. You could need decades of work, and you certainly need several years.

Read at first the Dragon Book, SICP & Lisp In Small Pieces. Read also about Denotational Semantics

Consider implementing your language with the help of libraries like GCCJIT or LLVM, or by compiling it to C. Then you'll have a "proof-of-concept" implementation. Have fun doing it.

By implementing your language, you'll discover its semantics traps. (I love doing that).

Look also into other languages, like Ada, Go, Ocaml, Rust, Modula, Cyclone, Scheme, C++ (perhaps you are reinventing its references, or its smart pointers), Common Lisp ....

Read Scott's Programming Languages Pragmatics book.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
  • But the semantics get updated every few years. At what point "should one not call it C" anymore? – Jongware Feb 08 '18 at 21:40
  • I have no idea.... BTW, updates of the C standard also affect its *syntax* (not just its semantics). Look for examples at the differences between C11 & C99 – Basile Starynkevitch Feb 08 '18 at 21:41
  • (Apart from that: Note that the question is hypothetical, and asks what problems could arise from such a question. As it is, you don't answer that ;) – Jongware Feb 08 '18 at 21:42
  • @use2564301 when C starts to have classes and object oriented features. – machine_1 Feb 08 '18 at 21:46
  • Sorry, @BasileStarynkevitch, but why do you mention ISO? I am not trying to propose a modification to C. This is for my language. I've just used C syntax and semantics as an example because my language will be used for low-level development and C is the most one used today – hdhzero Feb 08 '18 at 21:47
  • ISO examples used at least as excellent examples of language specifications. – Basile Starynkevitch Feb 08 '18 at 21:49
  • 1
    @BasileStarynkevitch, that's not the point here. The point is what problems could arise from using the proposed syntax. – hdhzero Feb 08 '18 at 21:52
  • 1
    I cannot answer to that, because you did not explained enough the *semantics* of your syntax. Don't focus on syntax, that is simple. Work on the *semantics* – Basile Starynkevitch Feb 08 '18 at 21:53
  • @BasileStarynkevitch, the question **is** about syntax. You want to know the semantics of pointers in my language? It is exactly the same as in C. I just don't want to have to keep putting a * everytime and want to know if this would be possible. – hdhzero Feb 08 '18 at 23:35
  • Try to implement it. – Basile Starynkevitch Feb 09 '18 at 06:36