- 我正在尝试从使用“。”标记的字符字符串传递信息。作为集合。
- 使用atoi()将这些字符转换为整数
- 然后将值发送到动态分配的内存中
我知道这个理论,我知道它应该如何工作,但我无法获得正确的语法来使它工作!
第二部分,在我声明 *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);
      }
}