Title basicly says what wont work. Half of the below works, I pass the whole
array to a function and it modifes and returns it as expected, but when I pass
an idividual array element it display it oddly then returns the wrong value but
the correct element if that makes sense.
Output I get is: passed array element: 816 ( where is should say 8 )
modified array element: 8
I gather my problem is possibly how I am outputing the displayed array element.
I get no compile errors, its a logical error I cant faddle out but its proberly
somthing really simple
Code:
#include <stdio.h>
#define ARRAY_SIZE 5
/*function prototypes*/
void changeArray ( int, int[] );
void changeElement ( int );
/*main function - begins program execution -----------------------------------*/
int main ( void )
{
int i, j;
/*create and display original array*/
int data[ ARRAY_SIZE ] = { 1, 2, 3, 4, 5 };
printf("Original array:\n\n");
for ( i = 0; i < ARRAY_SIZE; i++ )
{
printf("%d ", data[ i ]);
}
/*pass array size and name to function*/
changeArray ( ARRAY_SIZE, data );
/*output modified array output*/
printf("\n\nModified array\n\n");
for ( j = 0; j < ARRAY_SIZE; j++ )
{
printf("%d ", data[ j ]);
}
/*output original array element*/
printf("\n\nOrginal array element data[ 3 ]:\n\n");
printf("%d", data[ 3 ]);
/*pass element to function*/
changeElement ( data[ 3 ] );
/*output modified array element*/
printf("\n\nModified array element data[ 3 [:\n\n");
printf("%d", data[ 3 ]);
getchar(); /*freeze console output window*/
return 0; /*return value from int main*/
}
/*function to modify and return array*/
void changeArray ( int size, int x[] )
{
int i;
for ( i = 0; i < size; i++ )
{
x[ i ] *= 2;
}
}
/*function ro modify and return array element*/
void changeElement ( int e )
{
printf("%d", ( e *= 2 ));
}