I'm having a problem storing a value from an array of type char into an array of type int.
Code:
/**
* Program Name: --
*
*/
#include <stdio.h>
#define MAX 10
/* Function Prototype */
void extractInfo(char[],int, int[]);
int main()
{
char cInputOld[10] = { 'a', '1', 'r', 'o', '0', 'm', 'y', '1', 'i', 'b' };
int iBinary[13];
int iSize;
iSize = MAX;
extractInfo(cInputOld, iSize, iBinary);//Function call
return 0;
}
/* Function */
void extractInfo(char cInputOld[], int iSize, int iBinary[])
{
int i, j;
printf("Given the array of characters:\n");
for( i = 0; i < iSize; i++)
{
printf("cInputOld[%d] = %c", i, cInputOld[i]);
printf("\n");
}
j = 0;
printf("Array of binary digits:\n");
for ( i = 0; i < iSize; i++)
{
if (cInputOld[i] == '0'|| cInputOld[i] == '1')//store char 0 or char 1
{
for( j= 0; j < iSize; j++)
{
iBinary[j] = cInputOld[i];//assign cInputOld value to iBinary
}
}
}
for( j = 0; j < 13; j++)
{
printf("%3d", iBinary[j]);//print iBinary array
}
return;
}
Output:
Code:
Given the array of characters:
cInputOld[0] = a
cInputOld[1] = 1
cInputOld[2] = r
cInputOld[3] = o
cInputOld[4] = 0
cInputOld[5] = m
cInputOld[6] = y
cInputOld[7] = 1
cInputOld[8] = i
cInputOld[9] = b
Array of binary digits:
49 49 49 49 49 49 49 49 49 49 0 0 0
Instead Array of binary digits should have or at least the first 3 indices should be:
1 0 1
What am I doing wrong?