Is it a good idea to use varargs in a C API to set key value pairs?

前端 未结 7 2674
情书的邮戳
情书的邮戳 2021-02-20 11:47

I am writing an API that updates a LOT of different fields in a structure.

I could help the addition of future fields by making the update function variadic:

<         


        
7条回答
  •  悲哀的现实
    2021-02-20 12:28

    One problem with varargs in C is that you don't know how many arguments are passed, so you need that as another parameter:

    update(2, FIELD_NAME1, 10, FIELD_NAME2, 20);
    
    update(3, FIELD_NAME1, 10, FIELD_NAME2, 20, FIELD_NAME3, 30);
    

提交回复
热议问题