2

Can anyone explain to me in a very simple way the meaning of these lines of code.

typedef int pipe_t[2];
pipe_t *piped; 
int L; 
L = atoi(argv[2]);
piped = (pipe_t *) malloc (L*sizeof(pipe_t));
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
dubbi
  • 21
  • 2
  • if you understand what `int pipe_t[2];` would do (declare an object that's an array of 2 ints), then adding `typedef` means the word `pipe_t` is an alternative name for the type that `pipe_t` had in the first version – M.M Aug 10 '20 at 20:59
  • If I removed the first line of code how should the following change? – dubbi Aug 10 '20 at 21:33
  • 1
    Without the typedef, the other lines using that type become more complex. For example, the second would have to be `int (*piped)[2];` – Lee Daniel Crocker Aug 10 '20 at 21:50

2 Answers2

4
  • Type pipe_t is "array of 2 ints"
  • Variable piped is a pointer to such arrays.
  • L is an integer, assigned from the command line
  • Pointer piped is assigned to point to a block of memory large enough for L arrays of the type above.
Lee Daniel Crocker
  • 12,927
  • 3
  • 29
  • 55
2

For this typedef, you can read the declaration from right to left, so in

typedef int pipe_t[2];

you have

  • [2] says it is an array of 2. Positions [0] and [1]
  • pipe_t is the variable (type) name
  • int says pipe_t[2] is an array of 2 int
  • typedef says that it is, in fact, a type — an alias for a user-defined type representing an array of 2 int.

Run this program

#include<stdio.h>

int main(int argc, char** argv)
{
    typedef int pipe_t[2];
    printf("{typedef int pipe_t[2]} sizeof(pipe)_t is %d\n",
        sizeof(pipe_t));

    pipe_t test;

    test[1] = 2;
    test[0] = 1;
    printf("pair: [%d,%d]\n", test[0], test[1]);

    // with no typedef...
    int    (*another)[2];
    another = &test;
    (*another[0]) = 3;
    (*another)[1] = 4;
    printf("{another} pair: [%d,%d]\n", (*another)[0], (*another)[1]);

    pipe_t* piped= &test;
    printf("{Using pointer} pair: [%d,%d]\n", (*piped)[0], (*piped)[1]);
    return 0;
};

And you see

{typedef int pipe_t[2]} sizeof(pipe)_t is 8
pair: [1,2]
{another} pair: [3,4]
{Using pointer} pair: [3,4]

And you see that pipe_t has a size of 8 bytes, corresponding to 2 int's in x86 mode. And you can declare test as pipe_t and use it as an array of int. And the pointer works the same way

And I added the code to be used without such typedef, so we see that using a typedef makes it a bit cleaner to read.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
arfneto
  • 1,227
  • 1
  • 6
  • 13
  • If I removed the first line of code how should the following change? – dubbi Aug 10 '20 at 21:30
  • I do not understand what you are trying to say. :( if you remove the typedef you must replace all lines that uses it. It is just an alias. – arfneto Aug 10 '20 at 22:10
  • you should declare them this way: `int (*another)[2];` and reference like `(*another)[0] = 3;` – arfneto Aug 10 '20 at 22:28
  • I added this to the code above so you can see the 2 possibilities – arfneto Aug 10 '20 at 22:38
  • See [What is the difference between `const int*`, `const int * const`, and `int const *`?](https://stackoverflow.com/q/1143262/15168) and [C isn't that hard: `void ( *( *f[] ) () ) ()`](https://stackoverflow.com/q/34548762/15168) for discussions of the Spiral Rule for interpreting declarations in C and C++. You can probably find other questions that reference it. Your opening statement that "in C you read a declaration from right to left" is a bit simplistic. See also [Clockwise/Spiral Rule](http://c-faq.com/decl/spiral.anderson.html). – Jonathan Leffler Aug 10 '20 at 23:48
  • I think it makes sense in the way I wrote. May be simplistic but it seemed ok to me given the level of the discussion and the particular declaration here. Maybe you could add something to make it more clear @JonathanLeffler – arfneto Aug 11 '20 at 01:37
  • I hope my edit both respects what you wrote and makes it more accurate. – Jonathan Leffler Aug 11 '20 at 02:02
  • Thanks @JonathanLeffler! I was just curious about any further clarification the "spiral rule" could bring on the author declaration. And I confess I did not followed the link to read more. I was not quoting somebody else, I just wrote a program and a text in my terminal here trying to help the author. But what I wrote is not a rule nor even "my rule". – arfneto Aug 11 '20 at 02:14
  • In boringly pedantic detail, your original statement claimed that _all_ C declarations can be read right to left, which is not normally considered to be correct — see the Spiral Rule links cited above. By reducing the claim to 'this typedef', you are correct. And we can leave the comments about the spiral rule in the comments for the curious to follow, without either confusing or misleading the naïve C programmer. The 'spiral' part involves pointers and pointers to functions; they're not relevant here. That, it seems to me, is a sensible benefit all around. – Jonathan Leffler Aug 11 '20 at 02:23
  • Sorry @JonathanLeffler for the boring text. I was writing to the author and depicting the particular declaration he was asking about. For someone with experience on this things it can in fact be a real pain to read. I am not a specialist or an author of rules neither. The code I wrote is also boring for sure. – arfneto Aug 11 '20 at 02:30
  • I was only claiming that my commentary was boring and pedantic, not that your answer was either. Your answer was mostly OK, but slightly overclaimed in the original first sentence. That's now fixed in a way that should not cause beginners to be confused or misled. No further commentary from either of us is necessary, I think. – Jonathan Leffler Aug 11 '20 at 02:34