c-将char数组中的信息放入动态创建的数组中



  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]='';

   if (i>maxChars || i<MIN_SIZE)
      {
         printf("Your sumbition dosn't fit the size criteria.n");
         printf("Please reenter:nn");
         StrInput(str, maxChars);
      }
}

这是有问题的:

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

您正在声明数组,然后尝试对未初始化的数组使用strtok。你可能想这么做:

char Array[SIZE], *Array_ptr = 0;
StrInput(Array, SIZE);
Array_ptr = strtok(Array, " .");
#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()
            i = maxChars - 1;
            InputOver = !InputOver;//true
            break;
        }
        str[i]=(char)ch;
    }
    str[i]='';
    if (InputOver || i<MIN_SIZE){
        printf("Your sumbition dosn't fit the size criteria.n");
        printf("Please reenter:nn");
        while('n'!= ch){//clear input
            ch = getchar();
        }
        StrInput(str, maxChars);
    }
}

相关内容

  • 没有找到相关文章

最新更新