I'm trying to send an char array through a function convert it to an int array and send it back so I can reuse it with another function. I'm just having the worst time with this matter, please help!

Recommended Answers

All 5 Replies

I know I should send only the pointer to the string and work it as a procedure without returning any values. The thing is, I am trying to send it a char string and return it as an int string.

convertString( &charstring, &intstring );

where both parameters are arrays. It displays the following message: 'convertString' : cannot convert parameter 1 from 'char (*)[10]' to 'char *[]

And the function convertString looks like:

void convertString( char *charstring, int *intstring )
{
   for(int i = 0; i <= 9; i++ )
   {
      if(charstring[i]=='0')
         intstring[i]=0;
      if(charstring[i]=='1')
         intstring[i]=1;
      if(charstring[i]=='2')
         intstring[i]=2;
      if(charstring[i]=='3')
         intstring[i]=3;
      if(charstring[i]=='4')
         intstring[i]=4;
      if(charstring[i]=='5')
         intstring[i]=5;
      if(charstring[i]=='6')
         intstring[i]=6;
      if(charstring[i]=='7')
         intstring[i]=7;
      if(charstring[i]=='8')
         intstring[i]=8;
      if(charstring[i]=='9')
         intstring[i]=9;
       if(charstring[i]=='a'||charstring[i]=='A')
         intstring[i]=10;
      if(charstring[i]=='b'||charstring[i]=='B')
         intstring[i]=11;
      if(charstring[i]=='c'||charstring[i]=='C')
         intstring[i]=12;
      if(charstring[i]=='d'||charstring[i]=='D')
         intstring[i]=13;
      if(charstring[i]=='e'||charstring[i]=='E')
         intstring[i]=14;
      if(charstring[i]=='f'||charstring[i]=='F')
         intstring[i]=15;
   }
}

and the call to the function is

convertString( charstring, intstring );

it does not give me any errors, but when main tries to read intstring it only prints d's as if the function did not change anything.

for(int i = 0; i <= 9; i++ ) This should give you a warning unless you are using C99.
Proper way:

int i;
for (i = 0; i <= 9; i++)
for(int i = 0; i <= 9; i++ )
   {
      if(charstring[i]=='0')
         intstring[i]=0;
      if(charstring[i]=='1')
         intstring[i]=1;
      if(charstring[i]=='2')
         intstring[i]=2;
      if(charstring[i]=='3')
.
.
.
.

Think for a moment. If charstring is a '0', why does the function need to keep looking for other values all the way down?
You are missing some elses.

if (charstring[i] == '0')
    /* statement here */
else if(charstring[i] == '1')
    /* statement here */
else if(...)
     .
.
.
.

However there's a keyword in C named switch which will simplify the condition.

>it does not give me any errors, but when main tries to read intstring it only prints d's as if the function did not change anything.

Are you doing in main, something similar to:

char charstring[] = "0123456789";
    int intstring[10] = {0};
    int i;

    convertString(charstring, intstring);
    for (i = 0; i <= 9; i++)
        printf("%d\n", intstring[i]);

You dont have to have such a huge list if and else to do this. You to reduce that to just few lines of code. Looks the my version of convert function.

int convert( char *str, int *number )
{
     int i;
     
     for( i=0; str[i] != '\0'; i++ )
         if( isalpha( str[i] ) )
             number[i] = (str[i] - '0') - 7;
         else
             number[i] = (str[i] - '0');
         
     return i;
}
/* my output
my input - char str[] = "123456789ABCDEF";

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
*/

ssharish

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.