
Problems with array elements from user input, please help
I have now nailed my code down to the following, but could someone
help me with the bits which don't work, I have enclosed them with a
comment:
#include <stdio.h>
#include <stdlib.h>
int inputs_array_one(int *pointer, int array_size);
void output_array_one(int *pointer, int array_size);
int index, a_size_one, a_size_two, array_size;
int array_one[100], array_two[100];
int *a_one_ptr = array_one;
int *a_two_ptr = array_two;
int main(void)
{
printf("\nPlease input how many elements are in array one: ");
scanf("%d", &a_size_one);
printf("\nPlease input how many elements are in array two: ");
scanf("%d", &a_size_two);
/* WHILE LOOP GOES HERE */
/* ------------------------------------------------------------
Allocate memory for the pointer
The following code does not work, when I output
the array all elements seem to have been initialised
to 0.
pointer = (int *) malloc(sizeof(int)*array_size);
if (pointer == NULL)
{ printf("Not enough memory\n");
exit(1);
}
--------------------------------------------------------------
while loop which doesn't work, it was placed just after I had got the
input
in the main function, marked by WHILE LOOP GOES HERE.
while (a_size_one | a_size_two <= 0)
{ printf("\nInput a positive integer for the size of array one
- ");
scanf("%d", &a_size_one);
printf("\nInput a positive integer for the size of array two
- ");
scanf("%d", &a_size_two);
}
-------------------------------------------------------------- */
*a_one_ptr = inputs_array_one(a_one_ptr, a_size_one);
output_array_one(a_one_ptr, a_size_one);
return 0;
Quote:
}
int inputs_array_one(int *pointer, int array_size)
{
printf("\n");
for (index=0; index < array_size; index++)
{ printf("Please input array element - ");
scanf("%d", &*pointer);
pointer++;
}
pointer = array_one;
return *pointer;
/* free memory used by pointer -> free(pointer); <- */
Quote:
}
void output_array_one(int *pointer, int array_size)
{ printf("\nThe array is:");
for (index=0; index < array_size; index++)
{ printf("\nArray element %d is equal to %d", index,
array_one[index]);
}
Quote:
}