- I'm trying to pass information from a char string that's been tokenized using " ." as the set.
- Turn those characters into integers using atoi()
- 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);
}
}