本文介绍了将项添加到数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我正在尝试生成一系列序列号,以便在 应用程序中使用。我可以使用 a for循环将单个序列号打印到stdout,但我无法弄清楚如何将这些数字附加到 数组。 这是我的代码: #include< stdio.h> int main(void){ int i; int count = 20000; char appname [] =" MYAPP"; int serials [20000]; for(i = 1; i< count; ++ i){ int five = i * 5; int eleven = i / 11; int one =(i-1); printf("%s-%i-%i- %i \ n",appname,five,11,one); } 返回0; } 在像Python这样的高级语言中我会做这样的事情: serialnumber =("%s-%i-%i - %i \ n",appname,five,11,one) serials.append(serialnumber) 但我不知道怎么样把这种值分配给一个C变量,也不是我的b $ b重新如何使用此值填充C数组。任何建议都是 赞赏。 - Kevin Walzer Code by Kevin http://www.codebykevin.com 解决方案 这个数字看起来与代码中的其他数字相似吗? .... 查看起始值我阅读关于C中的索引。检查结束 的情况,确保循环确定.... 查找sprintf。 你想要一个字符串数组,所以你需要: char serials [2000] [22]; 在这里你应该用比 串行字符串的最大大小多一个替换22。如果您事先不知道,那么您需要离开 并学习动态内存分配(从man malloc开始)。有些 会说拥有一个44kB的自动变量在任何情况下都是一个坏主意, (他们会是对的)所以你最终应该学习如何分配内存。 snprintf(serials [i],22,"%s-%i-%i-%i \ n", appname,five,11,one); 你还应该检查snprintf的返回值是否小于22, 否则你的字符串已经被被固定大小的缓冲区截断。 包括< stdlib.hand使用 退出(EXIT_SUCCESS);或退出(EXIT_FAILURE); HTH viza I''m trying to generate an array of serial numbers for use in anapplication. I can get individual serial numbers printed to stdout usinga for loop, but I cannot figure out how to append these numbers to anarray.Here is my code:#include <stdio.h>int main (void) {int i;int count = 20000;char appname[] = "MYAPP";int serials[20000];for (i = 1; i < count; ++i) {int five = i*5;int eleven = i/11;int one = (i-1);printf("%s-%i-%i-%i\n", appname, five, eleven, one);}return 0;}In a higher-level language such as Python I''d do something like this:serialnumber = ("%s-%i-%i-%i\n", appname, five, eleven, one)serials.append(serialnumber)but I''m not sure how to assign this kind of value to a C variable, noram I sure how to populate the C array with this value. Any advice isappreciated.--Kevin WalzerCode by Kevin http://www.codebykevin.com 解决方案Does that number look similar to anything else in the code? ....Look at the start value of i. Read about indexing in C. Check the endcase for the loop to be sure to be sure ....Look up sprintf.The array serials[] is one containing ints. You have created stringswith non-numeric components (like the app name). What exactly would youexpect this append() to do? You certainly haven''t created an int of anykind. You have a string. Why not an array of strings?BrianYou want an array of strings, so you need:char serials[2000][22];Here you should replace 22 with one more than the maximum size of yourserial string. If you don''t know that in advance, then you need to goaway and learn dynamic memory allocation (start with man malloc). Somewill say that having a 44kB automatic variable is a bad idea in any case,(and they would be right) so you should eventually learn how to allocatememory anyway.snprintf( serials[i], 22, "%s-%i-%i-%i\n",appname, five, eleven, one);You should also check that the return value of snprintf is less than 22,otherwise your string has been truncated by the fixed size buffer.include <stdlib.hand useexit( EXIT_SUCCESS ); or exit( EXIT_FAILURE );HTHviza 这篇关于将项添加到数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-23 03:50