将项添加到数组 [英] Add items to an array

查看:78
本文介绍了将项添加到数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试生成一系列序列号,以便在

应用程序中使用。我可以使用

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

解决方案

Kevin Walzer< kw@codebykevin.comwrites:


我正在尝试生成一系列序列号用于

应用程序。我可以使用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);

}



查看起始值我阅读关于C中的索引。检查结束

的情况,确保循环确定....


返回0;


}

在像Python这样的高级语言中我会做这样的事情:

serialnumber =("%s-%i-%i-%i \ n",appname,five,11,one)

serials.append(serialnumber)


但我不知道如何将这种值赋给C变量,也不确定
我确定如何用这个值填充C数组。任何建议都是值得赞赏的。



查找sprintf。


Kevin Walzer写道:


我正在尝试生成一系列序列号,以便在

应用程序中使用。我可以使用for循环将单个序列号打印到stdout

,但我无法弄清楚如何将这些数字附加到数组中。


char appname [] =" MYAPP";

int serials [20000];


在像Python这样的高级语言中我会做这样的事情:


serialnumber =("%s-%i-%i-%i \ n",appname,five,11,one)

serials.append(serialnumber)


数组serials []是一个包含整数的数组。您已使用非数字组件(如应用程序名称)创建了字符串

。你究竟想要什么?
期待这个追加()呢?你肯定没有创造任何

类型的int。你有一个字符串。为什么不是一串字符串?


Brian


2008年6月23日星期一13:43:04 -0400,Kevin Walzer写道:


#include< stdio.h>


int main(void){


int i;

int count = 20000;

char appname [] =" MYAPP";

int serials [20000];



你想要一个字符串数组,所以你需要:


char serials [2000] [22];


在这里你应该用比

串行字符串的最大大小多一个替换22。如果您事先不知道,那么您需要离开

并学习动态内存分配(从man malloc开始)。有些

会说拥有一个44kB的自动变量在任何情况下都是一个坏主意,

(他们会是对的)所以你最终应该学习如何分配内存。


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);



snprintf(serials [i],22,"%s-%i-%i-%i \ n",

appname,five,11,one);


你还应该检查snprintf的返回值是否小于22,

否则你的字符串已经被被固定大小的缓冲区截断。


}

返回0;



包括< stdlib.hand使用


退出(EXIT_SUCCESS);或退出(EXIT_FAILURE);


}



HTH


viza


I''m trying to generate an array of serial numbers for use in an
application. I can get individual serial numbers printed to stdout using
a for loop, but I cannot figure out how to append these numbers to an
array.

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, nor
am I sure how to populate the C array with this value. Any advice is
appreciated.

--
Kevin Walzer
Code by Kevin
http://www.codebykevin.com

解决方案

Kevin Walzer <kw@codebykevin.comwrites:

I''m trying to generate an array of serial numbers for use in an
application. I can get individual serial numbers printed to stdout
using a for loop, but I cannot figure out how to append these numbers
to an array.

Here is my code:

#include <stdio.h>

int main (void) {

int i;
int count = 20000;
char appname[] = "MYAPP";
int serials[20000];

Does that number look similar to anything else in the code? ....

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);
}

Look at the start value of i. Read about indexing in C. Check the end
case for the loop to be sure to be sure ....

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, nor
am I sure how to populate the C array with this value. Any advice is
appreciated.

Look up sprintf.


Kevin Walzer wrote:

I''m trying to generate an array of serial numbers for use in an
application. I can get individual serial numbers printed to stdout
using a for loop, but I cannot figure out how to append these numbers
to an array.

char appname[] = "MYAPP";
int serials[20000];

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)

The array serials[] is one containing ints. You have created strings
with non-numeric components (like the app name). What exactly would you
expect this append() to do? You certainly haven''t created an int of any
kind. You have a string. Why not an array of strings?


Brian


On Mon, 23 Jun 2008 13:43:04 -0400, Kevin Walzer wrote:

#include <stdio.h>

int main (void) {

int i;
int count = 20000;
char appname[] = "MYAPP";
int serials[20000];

You 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 your
serial string. If you don''t know that in advance, then you need to go
away and learn dynamic memory allocation (start with man malloc). Some
will 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 allocate
memory anyway.

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);

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.

}
return 0;

include <stdlib.hand use

exit( EXIT_SUCCESS ); or exit( EXIT_FAILURE );

}

HTH

viza


这篇关于将项添加到数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆