I'm having problems with my struct pointer (i think). the program is supposed to read in a string of numbers and convert them to int's in an array backwards. so if it reads in the string 123456789\0 the array will be 9,8,7,6,5,4,3,2,1. this way i can add and subtract them later. there are 2 problems I'm having: first when i read the string in and convert it to the array all of my arrays seem to have the same value as the last string i read in even though the other part of the struct is right, and second when i go to print the values in the struct the array values are wrong in the print function but not anywhere else. it seems to print out addresses rather then the values.
the file format is as follows:
1
1 738493 382938
the first line tells me how many operations there are.
the fist number in each line tells me what operation to do... 1 is plus and 2 is minus
the following 2 numbers are the strings that will be read and computed.
any help would, well, help. i still need to get them to add and subtract but i think if i can fix this that will be easy.
this is the code i hope you lot don't mind if i post it all it's 127 lines...
Code:
//big int test
#include <stdio.h>
#include <string.h>
#define MAXSIZE 200
struct integer* read_integer(char* stringInt);
void print(struct integer *p);
struct integer
{
int* digits;
int size;
};
int main(void)
{
struct integer number[3];
struct integer *p;
char string_int[200];
int i;
int j;
int file_lines;
int test;
FILE *fin;
fin = fopen("bigint.txt","r");
fscanf(fin, "%d ", &file_lines);
for(i = 0; i < file_lines; i++)
{
fscanf(fin, "%d ", &test);
for(j = 0; j < 2; j++)
{
fscanf(fin, "%s ", string_int);
p = read_integer(string_int);
number[j].size = p->size;
//this part seems to be over written to all of 'number' not just
//number[j] so all of my struct have the same number.digits array
number[j].digits = p->digits;
}
p = &number[0];
//this is the same code as in the print function
//the print function here works correctly but the one in the print
// function is not
printf("%d\n", p->size);
for(i = 0; i < p->size; i++)
{
printf("%d\n", p->digits[i]);
}
print(p);
}
system("pause");
return 0;
}
//Preconditions: the first parameter is string that stores
// only contains digits, doesn't start with
// 0, and is 200 or fewer characters long.
//
//Postconditions: The function will read the digits of the
// large integer character by character,
// convert them into integers and return a
// pointer to the appropriate struct integer.
struct integer* read_integer(char* stringInt)
{
struct integer* big_number;
int size;
int number[200];
int temp;
int i;
int j;
size = strlen(stringInt);
big_number = malloc(sizeof(struct integer));
big_number->digits =(int*) malloc(sizeof(int) * size);
for (i = size -1, j = 0; i >= 0 ; i--, j++)
{
number[i] = (int) (stringInt[j] - '0');
}
big_number->size = size;
big_number->digits = number;
//this is to check and see if the pointer has the right information
printf("%d\n", big_number->size);
for(i = 0; i < big_number->size; i++)
{
printf("%d\n", big_number->digits[i]);
}
return big_number;
}
//Preconditions: p is a pointer to a big integer.
//
//Postconditions: The big integer pointed to by p is
// printed out.
//
void print(struct integer *p)
{
int i;
printf("%d\n", p->size);
//this print loop prints out wrong even though the other one is right
for(i = 0 ; i < p->size; i++)
{
printf("%d\n", p->digits[i]);
}
}