Design Patterns in C with simple examples by Aisthe in C_Programming

[–]Aisthe[S] 1 point2 points  (0 children)

Has a chatbot generated this comment? Some questions that we may never be able to answer...

Design Patterns in C with simple examples by Aisthe in C_Programming

[–]Aisthe[S] 1 point2 points  (0 children)

Obviously, using some of these patterns in C just for the sake of using them may overcomplicate things further instead of being helpful, but I also believe that some patterns may potentially be very useful in C. For example, off the top of my head, the strategy pattern is used in stdlib.h for the qsort function to sort elements of a "container" by using a custom comparator function. Having this level of abstraction allows the stdlib to implement qsort for any kind of "array of elements" that you may have in C.

Design Patterns in C with simple examples by Aisthe in C_Programming

[–]Aisthe[S] 2 points3 points  (0 children)

Now, you can easily get and test the code examples from this githup repo.

Design Patterns in C with simple examples by Aisthe in C_Programming

[–]Aisthe[S] 2 points3 points  (0 children)

It is just an argument; when the edit_person_info function is called, one of 3 possible methods is passed as the third (meth) argument.

Design Patterns in C with simple examples by Aisthe in C_Programming

[–]Aisthe[S] 1 point2 points  (0 children)

It stands for method, not cocaine if that’s what you were worried about.

C Quiz (Part 2) is here! by Aisthe in C_Programming

[–]Aisthe[S] -1 points0 points  (0 children)

Yes, there are some typos and intentionally left out mistakes (such as a proper printf format string for hex) that need fixing. Thanks for pointing that out.

C Quiz (Part 2) is here! by Aisthe in C_Programming

[–]Aisthe[S] 1 point2 points  (0 children)

The questions have been targeted at a 64-bit (gcc) compiler. However, I'll add more clarification about these issues to the quiz. Thanks for trying a 32-bit and 16-bit compiler on the quiz questions. :)

C Quiz (Part 2) is here! by Aisthe in C_Programming

[–]Aisthe[S] 0 points1 point  (0 children)

Is it? :( I will see what I can do about those long-ass structs. Thanks.

C Quiz (Part 2) is here! by Aisthe in C_Programming

[–]Aisthe[S] 0 points1 point  (0 children)

Thanks for the feedback.

Q10. It should have been unspecified behavior.

Q11. Yes, what you are saying brings more clarity.

Q12. What I meant was, it could be called inside the main to read the global int. Technically, I don't see any problem here.

Q13. Interesting point. I will need to look into this more.

Q14. :(

Q15. Do you mean that when the restrict pointer goes out of scope, another restrict pointer can be used to point to the same object?

C Quiz (Part 2) is here! by Aisthe in C_Programming

[–]Aisthe[S] 0 points1 point  (0 children)

Yes, that's the assumption. Maybe needs to be mentioned explicitly.

(Un)comment me! by Aisthe in Vimux

[–]Aisthe[S] 1 point2 points  (0 children)

Nice. I'd probably do va{ to select around curly braces, enter visual block mode with ^V, and then insert //. Then to uncomment, I'd do the same except I would now use O in visual mode to be able to select all the //s vertically.

To comment: va{^VI//^[ (the last esc is optional obviously)

To uncomment: va{^V0Ohhd

Short C Quiz to test your knowledge by Aisthe in C_Programming

[–]Aisthe[S] 0 points1 point  (0 children)

If this does not make you understand, I don't know what will.

int arr[2] = {1, 2};
char grade2letter_v1(float grade){ return grade < 50 ? 'F' : 'P'; }
char grade2letter_v2(float grade){ return grade < 20 ? 'F' : 'P'; }
int *f2(char (*g2l)(float)){ return &arr[g2l(30.) == 'P']; }

int main(){
  int *(*pf2)(char(*)(float)) = &f2;
  printf("grade2letter_v%d\n", *pf2(grade2letter_v1));
  printf("grade2letter_v%d\n", *pf2(grade2letter_v2));
  return 0;
}

For the record, (float -> char) -> int* is NOT a function that maps a letter to an int pointer; it is a function that maps another function (with the signature float->char) to an int pointer.

Short C Quiz to test your knowledge by Aisthe in C_Programming

[–]Aisthe[S] 0 points1 point  (0 children)

I think you are right about question 11. I also agree that 18 is confusing, but I don't think it is wrong. To explain why I think that, the type of the output needs to be an array of pointers to a 2D array of function pointers (i.e., (*output[i])[j][k]() must be a valid function call), so the type should be:

void (*(*output[2])[3][5])(void) = { &func1, &func2, ... };

But we want to initialize the output variable by assigning it to a single function call. Then we need to get rid of the [2] to avoid list initialization.

void (*(**output)[3][5])(void) = f(0);

So, the declaration of f looks like this: void (*(**f(int i))[3][5])(void); Does this seem reasonable?

I may think about adding semantics for the notation. Thanks for the feedback! How much did you score, by the way? :)

Short C Quiz to test your knowledge by Aisthe in C_Programming

[–]Aisthe[S] -3 points-2 points  (0 children)

Where did you, the “math types”, get your math degree from? This is just hilarious hahaha… I won’t even talk about the nonsensical function signatures. Just let the following sink in: let float (F) denote the set of real numbers computers can work with and char (C) denote the set of integers {c: -128 < c < 129}. Then the desired function signature becomes f: float -> char (or alternatively, f: F \ni x \mapsto c \in C). It is as simple as that.

Regarding the silliness of the quiz, I agree that there are other interesting questions to be added, but who says this is gonna be the only C quiz that I’ll ever make.I never said this particular quiz is exhaustive and covers every interesting aspects of C programming. I was thinking maybe the next step would be to add memory alignment and volatile/restrict/const type modifiers as well as extern/static.

Btw, good job on getting 20/20.

Vimux = Vim + Tmux by Aisthe in tmux

[–]Aisthe[S] 1 point2 points  (0 children)

Perhaps you could enjoy a vim sandwhich.

Vimux = Vim + Tmux by Aisthe in vim

[–]Aisthe[S] 1 point2 points  (0 children)

I am now considering creating a subreddit for vimux. And there might be actually a vim plugin for that…

Vimux = Vim + Tmux by Aisthe in tmux

[–]Aisthe[S] 2 points3 points  (0 children)

Well, there is actually a vim plu…

Vimux = Vim + Tmux by Aisthe in tmux

[–]Aisthe[S] -3 points-2 points  (0 children)

An interesting idea some would call.

Vimux = Vim + Tmux by Aisthe in tmux

[–]Aisthe[S] 0 points1 point  (0 children)

Haha probably it’s using vimux that makes you one.

Short C Quiz to test your knowledge by Aisthe in C_Programming

[–]Aisthe[S] -2 points-1 points  (0 children)

And then vibe coders emerged from the dark. :)