我写下了接受用逗号分隔的群体值的代码。然后,我分割输入的字符串并将其存储到数组中。现在,我想存储一个双倍的数据,所以我可以在其上执行数学函数。但首先,我想输出它为双。我曾尝试的strtod,但它给我的如何将指针转换为C中的double?
passing argument 1 of '__strtod' makes pointer from integer without a cast [- Wint-conversion]
错误这是我的主要功能
int main(int argc, char const *argv[])
{
int p_size, s_size;
int n =0;
char *population;
char population_string[100];
printf("Enter the population size:");
scanf("%d",&p_size);
printf("Enter the sample size:");
scanf("%d",&s_size);
printf("Enter the population values separated by comma(,):");
scanf("%s",&population_string);
printf("The population are:%s\n",population_string);
population = splitPopulation(population_string,p_size);
printf("The contents are:\n");
for (int i = 0; i < p_size; i++)
{
printf("%c\n",population[i]);
printf("%f\n", strtod(population[i],NULL));
}
return 0;
}
,这是我的分裂字符串
char * splitPopulation(char *population_string, int size){
char *population_array=malloc(sizeof(char*)*size);
char *token = strtok(population_string,",");
for (int i = 0; i < size; i++)
{
population_array[i]= *token;
token= strtok(NULL,",");
}
return population_array;
}
功能
我的样品输入是:
Enter the population size:4
Enter the sample size:2
Enter the population values separated by comma(,):1,2,3,4
这解决了它。感谢您为我启发了一些关于char的指针。 – nairda29