Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
here's my C coding problem:
how does one convert a char array to an int array?
for example, say if one has the pattern = "3 3 3 3 3 4" stored as chars in an array (with spaces)
how can i convert this to an int array like int_pattern = [3, 3, 3, 3, 3, 4]initial declarations are:
char sz_pattern[max]
int sz_pattern[max]ive already tried:
sz_pattern[2*n_belt+1] = NULL;
for (i=0; i < (2*n_belt+1); ++i)
{
sz_pattern_int[i] = atoi(sz_pattern[i]);
}result: nothing but zeroes stored in int array
and also:
for (i=0; i < max; ++i)
{
*((int *)sz_pattern_int[i]) = *((char *)sz_pattern[i])
}result: core dump.

1. "max" can't be the same thing for both the integer array and char array - the character array will be TWICE as big as the integer array (one character and one space for each integer!).2. The incrementer in the loop cannot be the same for both. Again, you are hitting every other character in the character array - thus need a different count.
***********************************
int stringoffset;
char *intermediate;for (i = 0; i < max; i++)
{
stringoffset = i * 2;intermediate = &sz_pattern_char[stringoffset];
sz_pattern_int[i] = atoi(intermediate);
}Is how I worked it.

I'm no C buff, but in Java I'd do something like
int intPattern[max/2+1];
char charPattern[max];
for (int i=0; i(less than)intPattern.length; i++)
{
if charPattern[i]!=' ';
intPattern[i]==(int)charPattern[i];
}

![]() |
MAC address delphi
|
Trap an int!
|

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |