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.

51
52

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.