简体   繁体   中英

How can I pass a char array as a parameter to a function in C?

I have to make a program that converts a number from a base that the User entered in a decimal number. I made this code but I'm having trouble to pass the char array as a parameter on the function:

int base_decimal(char x[100], int y){
int tot = 0;
int p = 1;
int arr[100];
int i,a,j = 100;

for(i=0;i<100;i++){
    printf(" %s ", x[i]);
        switch(x[j]){
            case 'a': arr[i] = 10;
            break;
            case 'b': arr[i] = 11;
            break;
            case 'c': arr[i] = 12;
            break;
            case 'd': arr[i] = 13;
            break;
            case 'e': arr[i] = 14;
            break;
            case 'f': arr[i] = 15;
            break;
            default: arr[i] = atoi(&x[j]);
            break;
        }

    j--;
}

for(a = 0; i<100;a++){

        tot += arr[a] * pow(y,a);

}


return tot;

}

the main:

void main(void) {
  int b, x=0;
  char n[100];
  int num;

  printf("type de number: \n");

  scanf("%s", n);
  fflush(stdin);

  printf("type de base:\n");
  scanf("%i", &b);
  num = base_decimal(n, b);

  printf("The number %i in decimal is: %i ",  b, num);
}

when I run the code, the program crashes.

Can someone help me???

sorry for the bad English :)

In

printf(" %s ", x[i]);

you are trying to print string but x[i] actually is a character, so you should use

printf(" %c ", x[i]); // or x[j - 1]
//        ^      ^
//     format   and index

Please note also that I have corrected x[j] to x[i] or x[j - 1] . Otherwise you were trying to access array with index j = 100 which is out of range. Remember, the last element in array defined as array[n] is a[n - 1] , it is because the first one is not a[1] but a[0] .

This is a tidied up version of your code. The algorithm is wrong though. Some things that were wrong with your code were

This is wrong

for(a = 0; i<100;a++){

So was this, I removed it.

printf(" %s ", x[i]);

If you want to print the characters you need t

printf(" %c ", x[i]);

Have a look at the convert method that I added.

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int convert(int number,int base){
  if(number == 0 || base==10) {
    return number;
  }
  return (number % base) + 10*convert(number / base, base);
}


int base_decimal(char x[100], int y){ 
  int tot = 0;
  int arr[100];
  int i,a,j = 100;

  for(i=0;i<100;i++){
    switch(x[j]){
      case 'a': arr[i] = 10; break;
      case 'b': arr[i] = 11; break;
      case 'c': arr[i] = 12; break;
      case 'd': arr[i] = 13; break;
      case 'e': arr[i] = 14; break;
      case 'f': arr[i] = 15; break;
      default: arr[i] = atoi(&x[j]); break;
    }   
    j--;
  }

  for(a = 0; a<100;a++){
    tot += arr[a] * pow(y,a);
  }
  return tot;
}

int main(void) {
  int b = 0;
  int n = 0;
  char result[100];
  int num;
  printf("type de number: \n");
  scanf("%d", &n);
  fflush(stdin);
  printf("type de base:\n");
  scanf("%d", &b);
  //num = base_decimal(n, b);
  num = convert(n, b);

  printf("The number %i in decimal is: %i ",  b, num);
  return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM