- #1
Firestrider
- 104
- 0
What I want to do is pass in a string to read_integer (a function which will parse the string character by character, convert the characters into ints (c - '0'), store it into a struct, and return a pointer to that struct. The string is guaranteed not to be over 200 digits.). I'm not sure how to access part of the struct because the piece that is storing the ints is a int pointer with no array size, and not sure if I'm passing in references right:
Code:
#include <stdio.h>
struct integer
{
int* digits;
int size;
};
struct integer* read_integer(char* stringInt);
int main(void)
{
FILE* read_file;
char intStr[200];
struct integer* first;
read_file = fopen("ints.txt", "r");
fscanf(read_file, "%s", &intStr);
first = read_integer(intStr);
fclose(read_file);
system("PAUSE");
return 0;
}
struct integer* read_integer(char* stringInt)
{
int index;
struct integer* new_integer;
char iterchar;
new_integer = (struct integer*)(malloc(sizeof(struct integer)));
new_integer->size = strlen(stringInt);
new_integer->digits = (int*)(malloc(new_integer->size * sizeof(int)));
for(index = 0; index < new_integer->size; index++)
{
scanf("%c", &iterchar);
new_integer->digits = (int)(iterchar - '0');
printf("%c", iterchar);
}
return new_integer;
}