First off, I'm sure this is because I've just started working seriously with pointers. I'm sure I'm doing something amiss.
Extensive testing has told me that the contents of the arrFor are being corrupted somehow between the allocateArray function and the display function. I *suspect* that it has something to do with passing arrFor to the displayResults function. However, I've reached the point where I can't see how to fix it anymore and need outside help. Thanks!
N.B. arrBack remains uncorrupted; that's why i suspect the function call to displayResults.
input file (array.txt):
output (to stdout):
Extensive testing has told me that the contents of the arrFor are being corrupted somehow between the allocateArray function and the display function. I *suspect* that it has something to do with passing arrFor to the displayResults function. However, I've reached the point where I can't see how to fix it anymore and need outside help. Thanks!
N.B. arrBack remains uncorrupted; that's why i suspect the function call to displayResults.
Code:
/* This program uses pointers to read into an array from a file, dynamically
allocate a second array to fit the data read exactly, and fill the new array
with the reverse of the original array entirely using pointer arithmetic. */
const int MAX_SIZE = 51;
#include <stdio.h>
#include <stdlib.h>
int getData( FILE * is, const int MAX_SIZE, int * );
void allocateArray( int *, int * numberRead );
void displayResults( FILE * os, int *, int *, int * numberRead );
int main()
{
int arrayForward[MAX_SIZE];
int * arrayForPtr;
arrayForPtr = arrayForward;
// int *arrayBackward;
FILE * fin;
fin = fopen( "array.txt", "r" );
if ( fin == NULL )
{
perror( "Error opening input file\n" );
// system( "pause" ); only for Dev C++
exit(1);
}
//get data
int numberRead = 0;
numberRead = getData( fin, MAX_SIZE, arrayForPtr );
fclose( fin );
allocateArray( arrayForPtr, &numberRead );
// displayResults( stdout, arrayForward, arrayBackward, &numberRead );
return 0;
}
int getData( FILE * is, const int MAX_SIZE, int * arrFor )
{
int nu = 0;
int used = 0;
int * arrayEnd = arrFor + (MAX_SIZE - 1);
while ( arrFor < arrayEnd && fscanf( is, "%i", arrFor ) == 1 )
{
++arrFor;
// printf("%4i\n", arrFor[nu - 1]);
used++;
}
//temporary print to see the data read
/* int u;
for ( u = 0 ; u < nu ; u++ )
{
printf("%4i\n", arrFor[u]);
} */
return used;
}
void allocateArray( int * arrFor, int * numberRead )
{
int * arrBack;
arrBack = (int *)calloc( *numberRead, sizeof(int) ); //allocate array
// arrayBackPtr = arrayBack;
int * arrayEnd = arrFor + (*numberRead); //find bounds
// int * end;
// end = &arrayEnd;
//fill new array with contents of first in reverse order
//use subscripts
/* int u = 0, count = *numberRead;
while ( u < *numberRead )
{
if ( count > 0 )
{
arrayBack[count - 1] = arrFor[u];
count--;
}
u++;
} */
//use pointer arithmetic
while ( arrFor < arrayEnd )
{
if ( arrayEnd + 1 > arrFor )
{
*(arrBack - 1) = *arrFor;
arrBack--;
}
arrFor++;
}
//possibly faulty call to displayResults
displayResults( stdout, arrFor, arrBack, numberRead );
//temporary print
/* printf("\n");
for ( int count = 0 ; count < *numberRead ; count++ )
{
printf("%4i\n", arrBack[count]);
} */
}
void displayResults( FILE * os, int * arrFor, int * arrBack, int * numberRead )
{
int * arrayEndFor = arrFor + (*numberRead); //bounds for original array
int * arrayEndBack = arrBack + (*numberRead); //bounds for reversed array
fprintf( os, "%24s\n\n%14s%17s\n", "The Arrays", "Original", "Reverse" );
int nu = 0;
/* while ( nu < *numberRead )
{
printf("%4i\n", arrFor[nu] );
nu++;
} */
while ( arrFor < arrayEndFor && arrBack < arrayEndBack )
{
fprintf( os, "%14i%17i\n", *arrFor, *arrBack );
arrFor++, arrBack++;
}
free( arrBack );
}
input file (array.txt):
Code:
62
55
83
90
97
104
107
148
156
160
186
190
209
217
226
232
240
283
291
295
output (to stdout):
Code:
The Arrays
Original Reverse
1606419960 295
32767 291
7 283
0 240
1606664232 232
32767 226
1606416440 217
32767 209
1606416384 190
32767 186
1606424274 160
32767 156
0 148
0 107
1606418464 104
32767 97
0 90
0 83
1606416424 55
32767 62
Last edited: