类似于Python的数组填充-C等效 [英] Python-like array filling - C equivalent
问题描述
我想知道像在Python中一样但在C中填充数组等效的情况.
I would like to know what would be the equivalent for filling an array like we do in Python, but in C.
Python示例代码:
Python example code:
arrayname=[0]*10
randomtextvar="test_text"
for i in range(10):
arrayname[i]=randomtextvar
for k in range(10):
print(arrayname[k]+" "+str(k)+ " position")
我还没有找到解决方案,我不太了解如何将值设置为字符串(字符)数组位置(在C中).
I haven't find an solution for this, I don't really understand how to set value to a string (char) array position (in C).
#include <stdio.h>
#include <string.h>
int main() {
int c = 0;
char arr[256];
fgets(arr, 256, stdin);
char spl[] = " ";
char *ptr = strtok(arr, spl);
while (ptr != NULL) {
ptr = strtok(NULL, spl);
// HERE I WANT TO ADD THE ptr value to a new array of strings
c++;
}
return 0;
}
推荐答案
您正在 while
循环之前执行 strtok
,并在开始.因此,您破坏该行上的第一个令牌.
You are doing strtok
before the while
loop and immediately at the start. So, you are trashing the first token on the line.
kaylum 指出了一种使用 strdup
将字符串保存到固定数组中的简单方法.
kaylum pointed out a simple way to save the strings into a fixed array using strdup
.
但是,我怀疑您想要像 python
所做的一样灵活的事情.因此,当您使用 realloc
处理许多输入行时,字符串数组可以动态动态增长.
But, I suspect you'd like something as flexible as what python
is doing. So, the array of strings can be dynamically grown as you process many input lines using realloc
.
此外,有时将最后一个数组元素设置为 NULL
[就像 argv
]有时也很不错.
Also, in addition, it's sometimes nice to have the last array element be NULL
[just like argv
].
这是一些重构的代码.我给它加了注释,以解释发生了什么事:
Here's some refactored code. I've annotated it to explain what is going on:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int
main(void)
{
char *ptr;
char *bp;
const char *spl = " \n";
char buf[256];
char **arr = NULL;
size_t arrcnt = 0;
size_t arrmax = 0;
// read in all input lines
while (1) {
// get next input line -- stop on EOF
ptr = fgets(buf,sizeof(buf),stdin);
if (ptr == NULL)
break;
// parse the current line
bp = buf;
while (1) {
// get next token on the current line
ptr = strtok(bp,spl);
bp = NULL;
// stop current line if no more tokens on the line
if (ptr == NULL)
break;
// grow the string array [periodically]
// NOTE: using arrmax cuts down on the number of realloc calls
if (arrcnt >= arrmax) {
arrmax += 100;
arr = realloc(arr,sizeof(*arr) * (arrmax + 1));
if (arr == NULL) {
perror("realloc/grow");
exit(1);
}
}
// add current string token to array
// we _must_ use strdup because when the next line is read any
// token data we had previously would get overwritten
arr[arrcnt++] = strdup(ptr);
// add null terminator just like argv -- optional
arr[arrcnt] = NULL;
}
}
// trim the array to the exact number of elements used
arr = realloc(arr,sizeof(*arr) * (arrcnt + 1));
if (arr == NULL) {
perror("realloc/trim");
exit(1);
}
// print the array
for (char **av = arr; *av != NULL; ++av)
printf("%s\n",*av);
// free the array elements
for (char **av = arr; *av != NULL; ++av)
free(*av);
// free the array
free(arr);
// reset counts and pointer
arrmax = 0;
arrcnt = 0;
arr = NULL;
return 0;
}
这篇关于类似于Python的数组填充-C等效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!