0

  1. 我正在尝试从使用“。”标记的字符字符串传递信息。作为集合。
  2. 使用atoi()将这些字符转换为整数
  3. 然后将值发送到动态分配的内存中

我知道这个理论,我知道它应该如何工作,但我无法获得正确的语法来使它工作!

第二部分,在我声明 *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);
      }
}
4

2 回答 2

2

这是有问题的:

char Array[SIZE], *Array_ptr = strtok(Array, " .");

您正在声明数组,然后尝试在未初始化的数组上使用 strtok。您可能打算这样做:

char Array[SIZE], *Array_ptr = 0;
StrInput(Array, SIZE);
Array_ptr = strtok(Array, " .");
于 2013-04-28T18:55:25.810 回答
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);
    }
}
于 2013-04-28T22:51:21.387 回答