0

  1. I'm trying to pass information from a char string that's been tokenized using " ." as the set.
  2. Turn those characters into integers using atoi()
  3. Then send the values into dynamically allocated memory

I know the theory, I know how it's supposed to work, but I can't get the right syntax to make it work!

The second part, after I declare *Malloc_Array_ptr* is where I run into trouble. So far, I've used the Malloc Pointer exactly how I'd use a regular array pointer, and I'm not getting any results on my printf test.

Can't find info that makes sense to me over google, I'm going crazy over this. I think I'm real close to figuring it out, I just need a nudge in the right direction >.<

Thank you! :)

#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);
      }
}

2 Answers 2

2

This is problematic:

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

You are declaring the array, then trying to use strtok on the uninitialized array. You probably meant to do this:

char Array[SIZE], *Array_ptr = 0;
StrInput(Array, SIZE);
Array_ptr = strtok(Array, " .");
Sign up to request clarification or add additional context in comments.

1 Comment

I worked around the balancing act, but in the end it was just a minor error and didn't change the nature of the real problem :(
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);
    }
}

1 Comment

Works exactly as hoped, I'll be studying how this works so I understand and create my own! Thank you! +all the well deserved rep I can give =D

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.