简体   繁体   English

C-如何在数组中存储带有变量的字符串?

[英]C - How do you store a string with variables in it within an array?

Because I know that 因为我知道

char array[STRING_ELEMENTS + 1][MAX_STRING_LENGTH + 1];

/*just for the first element*/
array[0] == ("this number: %d, and that number: %d\n", a, b);

probably makes me look disgusting. 可能让我看起来恶心。 I would have no clue how else to do this though. 我不知道该如何做。

You can't assign a string like this in C, but you can use snprintf : 您不能在C中分配这样的字符串,但是可以使用snprintf

snprintf(array[0], MAX_STRING_LENGTH, "this number: %d, and that number: %d\n", a, b);

(don't forget to #include <stdio.h> somewhere near the top of your program.) (不要忘记在程序顶部附近的某个地方#include <stdio.h> 。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM