我知道这个理论,我知道它应该如何工作,但是我无法使用正确的语法使它工作!
第二部分,在我宣布* Malloc_Array_ptr *之后,我遇到了麻烦。 到目前为止,我已经使用了Malloc指针,我使用的是常规数组指针,而且我的printf测试没有得到任何结果。
无法通过谷歌找到对我有意义的信息,我对此疯狂。我认为我真的很接近搞清楚,我只需要在正确的方向上轻推>。<
谢谢! :)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SIZE 10
#define MIN_SIZE 2
void StrInput(char str[], int maxChars);
int main(int argc, char *argv[])
{
char Array[SIZE], *Array_ptr = strtok(Array, " .");
StrInput(Array, SIZE);
int i=1, *temp = Array_ptr;
//Strok initialized in order to use NULL next sequence.
//Temp stores the pointer in it's original form, before it gets butchered by strtok
while (Array_ptr != NULL)
{
Array_ptr = strtok(NULL, " .");
i++;
}
//Above code finds the number of tokens strtok worked on, and stores it as i.
//Dynamically Creates the array which can hold exactly the amount of tokens (i)
int *Malloc_Array_ptr = (int*)malloc(i* sizeof(int)), hold;
i=0;
while (Array_ptr != NULL)
{
temp = strtok(NULL, " .");
hold = atoi(temp);
Malloc_Array_ptr[i] = hold;
i++;
}
printf("Show me the money: %s \n", Malloc_Array_ptr);
system("PAUSE");
return 0;
}
/*----------------------------------------------------------------------*/
void StrInput(char str[], int maxChars)
{
int i=0, str_lenght;
while ((str[i] = getchar()) != '\n')
i++;
str[i+1]='\0';
if (i>maxChars || i<MIN_SIZE)
{
printf("Your sumbition dosn't fit the size criteria.\n");
printf("Please reenter:\n\n");
StrInput(str, maxChars);
}
}
答案 0 :(得分:2)
这是有问题的:
char Array[SIZE], *Array_ptr = strtok(Array, " .");
您正在声明该数组,然后尝试在未初始化的数组上使用strtok。你可能打算这样做:
char Array[SIZE], *Array_ptr = 0;
StrInput(Array, SIZE);
Array_ptr = strtok(Array, " .");
答案 1 :(得分:1)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SIZE 10
#define MIN_SIZE 2
void StrInput(char str[], int maxChars);
int main(int argc, char *argv[]){
char Array[SIZE], *Array_ptr, *strwk;
StrInput(Array, SIZE);
int i=0;
strwk=strdup(Array);//strtok would change the string. so make a copy.
Array_ptr=strtok(strwk, " .");
while (Array_ptr != NULL){
++i;//countup element
Array_ptr = strtok(NULL, " .");
}
int *Malloc_Array_ptr = (int*)malloc(i* sizeof(int));
i=0;
strcpy(strwk, Array);
Array_ptr = strtok(strwk, " .");
while (Array_ptr != NULL){
Malloc_Array_ptr[i] = atoi(Array_ptr);
++i;
Array_ptr = strtok(NULL, " .");
}
free(strwk);
int j;
//check print
for(j=0;j<i;++j)
printf("%d ", Malloc_Array_ptr[j]);
printf("\n");
// printf("Show me the money: %s \n", Malloc_Array_ptr);//Malloc_Array_ptr isn't (char*).
system("PAUSE");
return 0;
}
/*----------------------------------------------------------------------*/
void StrInput(char str[], int maxChars){
int i=0, ch;//str_lenght: unused
int InputOver = 0;
printf("input numbers :");
for(i=0;(ch = getchar()) != '\n';++i){
if(i > maxChars -1){//-1: for EOS(\0)
i = maxChars - 1;
InputOver = !InputOver;//true
break;
}
str[i]=(char)ch;
}
str[i]='\0';
if (InputOver || i<MIN_SIZE){
printf("Your sumbition dosn't fit the size criteria.\n");
printf("Please reenter:\n\n");
while('\n'!= ch){//clear input
ch = getchar();
}
StrInput(str, maxChars);
}
}