将逗号分隔的字符串转换为数组
Converting a comma separated string to array
我一直在尝试转换整数、浮点数和字符数组中的字符串。虽然我可以让它适用于整数和浮点数,但字符存在一些问题。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char *s1;
int k, no=5;
char* variable = "R1,R2,R3,R4,R5";
void* value;
s1 = calloc(no,sizeof(char)*81);
for (k=0; k<no; k++) s1[k] = strdup(mchar);
ListChar(variable, s1, no, ",");
memcpy(value, s1, no*sizeof(char)*81);
free(s1);
int i;
for (i = 0; i < no; i++)
printf("%s", value[i]);
printf("\n");
return 0;
}
在我的头文件中
#define mchar "A...(81times)"
实施:
int ListChar(char *buf, char *list, int maxloop, char* delim)
{
int n = 0;
char *s,*t;
s= strdup(buf);
t= strtok(s,delim);
while ( t && (n<maxloop))
{
if (list!=NULL) list[n] =strdup(t);
n++;
t=strtok(NULL,delim);
}
free(s);
return(n);
}
在 calloc 内存分配期间,当我观看 s1
它的 0xsomeadress ""
for循环后s1
变为0xsomeadress "Garbage value 81 times"
当 s1
分配给 list
时,它仍然读取相同的垃圾值。
而当 list [n] = strdup(t)
list[0]
读取第一块垃圾值时 -21 '1 ṗ'
.
t
被正确分隔。我什至尝试初始化 char *s1[81] = {"something"}
并在 j
上循环但它不起作用,同样的问题,我需要在最后释放 s1
因为这个函数运行了很多次。我为整数和浮点数做了 list[n]=atoi(t)
它工作正常。谁能给我一些建议?
似乎对字符串的工作原理存在根本性的误解。您的 s1
显然需要是 char **
而 strdup
的用法不正确。如果 s1
是 char *
类型,那么 s1[k]
是 char
类型。但是 strdup
returns a char *
,所以 s1[k] = strdup ...
显然是一个错误,您的编译器应该警告您。也许你想要这样的东西:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void * xmalloc(size_t s);
void
ListChar(const char *buf, char **list, int maxloop, int delim)
{
char set[] = {delim, 0};
for( int n = 0; n < maxloop; n += 1 ){
size_t len = strcspn(buf, set);
list[n] = xmalloc(len + 1);
memcpy(list[n], buf, len);
buf += len + 1;
}
}
int
main(int argc, char **argv)
{
int delim = ',';
(void)argc; /* Suppress compiler warning */
while( *++argv ){
char **s1;
int k, num = 1;
char *input = *argv;
for( const char *p = input; *p; p += 1 ){
if( *p == delim ){
num += 1;
}
}
s1 = xmalloc(num * sizeof *s1);
ListChar(input, s1, num, delim);
for( int i = 0; i < num; i += 1 ){
printf("%s\n", s1[i]);
}
free(s1);
}
return 0;
}
void *
xmalloc(size_t s)
{
void *rv = malloc(s);
if( rv == NULL ){
perror("malloc");
exit(EXIT_FAILURE);
}
return rv;
}
注意上面的代码对每个字符串扫描了两次,并不理想。与其扫描字符串以查找定界符的数量然后解析字符串,不如一次完成这两项操作。但为了演示如何分解字符串,这似乎是不必要的复杂性。 (虽然实际上更简单,IMO)
我一直在尝试转换整数、浮点数和字符数组中的字符串。虽然我可以让它适用于整数和浮点数,但字符存在一些问题。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char *s1;
int k, no=5;
char* variable = "R1,R2,R3,R4,R5";
void* value;
s1 = calloc(no,sizeof(char)*81);
for (k=0; k<no; k++) s1[k] = strdup(mchar);
ListChar(variable, s1, no, ",");
memcpy(value, s1, no*sizeof(char)*81);
free(s1);
int i;
for (i = 0; i < no; i++)
printf("%s", value[i]);
printf("\n");
return 0;
}
在我的头文件中
#define mchar "A...(81times)"
实施:
int ListChar(char *buf, char *list, int maxloop, char* delim)
{
int n = 0;
char *s,*t;
s= strdup(buf);
t= strtok(s,delim);
while ( t && (n<maxloop))
{
if (list!=NULL) list[n] =strdup(t);
n++;
t=strtok(NULL,delim);
}
free(s);
return(n);
}
在 calloc 内存分配期间,当我观看 s1
它的 0xsomeadress ""
for循环后s1
变为0xsomeadress "Garbage value 81 times"
当 s1
分配给 list
时,它仍然读取相同的垃圾值。
而当 list [n] = strdup(t)
list[0]
读取第一块垃圾值时 -21 '1 ṗ'
.
t
被正确分隔。我什至尝试初始化 char *s1[81] = {"something"}
并在 j
上循环但它不起作用,同样的问题,我需要在最后释放 s1
因为这个函数运行了很多次。我为整数和浮点数做了 list[n]=atoi(t)
它工作正常。谁能给我一些建议?
似乎对字符串的工作原理存在根本性的误解。您的 s1
显然需要是 char **
而 strdup
的用法不正确。如果 s1
是 char *
类型,那么 s1[k]
是 char
类型。但是 strdup
returns a char *
,所以 s1[k] = strdup ...
显然是一个错误,您的编译器应该警告您。也许你想要这样的东西:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void * xmalloc(size_t s);
void
ListChar(const char *buf, char **list, int maxloop, int delim)
{
char set[] = {delim, 0};
for( int n = 0; n < maxloop; n += 1 ){
size_t len = strcspn(buf, set);
list[n] = xmalloc(len + 1);
memcpy(list[n], buf, len);
buf += len + 1;
}
}
int
main(int argc, char **argv)
{
int delim = ',';
(void)argc; /* Suppress compiler warning */
while( *++argv ){
char **s1;
int k, num = 1;
char *input = *argv;
for( const char *p = input; *p; p += 1 ){
if( *p == delim ){
num += 1;
}
}
s1 = xmalloc(num * sizeof *s1);
ListChar(input, s1, num, delim);
for( int i = 0; i < num; i += 1 ){
printf("%s\n", s1[i]);
}
free(s1);
}
return 0;
}
void *
xmalloc(size_t s)
{
void *rv = malloc(s);
if( rv == NULL ){
perror("malloc");
exit(EXIT_FAILURE);
}
return rv;
}
注意上面的代码对每个字符串扫描了两次,并不理想。与其扫描字符串以查找定界符的数量然后解析字符串,不如一次完成这两项操作。但为了演示如何分解字符串,这似乎是不必要的复杂性。 (虽然实际上更简单,IMO)