View Single Post
Old 11-30-2006, 11:02 PM   #1 (permalink)
zero2
Junkie
 
zero2's Avatar
 
[c]problem with arrays

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?
zero2 is offline  
 

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76