Home > database >  void function returns nothing - C
void function returns nothing - C

Time:11-10

I'm starting my studies in C and i came across this function return problem. I created a function that prints the name of numbers up to 9, from an input number. Entering, i have no return from the function. I can't see where the error is.

This is my code:

void for_loop(int n1, char array[]){
    for(int index = n1; index <= 9; index  ) {
        printf("%s\n", array[index]);
    }
}

int main() 
{
    int num1 = 2;

    char* numbers[10] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};

    for_loop(num1, *numbers);
    return 0;
}

CodePudding user response:

Your code is wrong:

You want this:

void for_loop(int n1, char *array[]) {
  for (int index = n1; index <= 9; index  ) {
    printf("%s\n", array[index]);
  }
}

int main()
{
  int num1 = 2;

  char* numbers[10] = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" };

  for_loop(num1, numbers);
  return 0;
}

for_loop(num1, *numbers) is equivalent to for_loop(num1, numbers[0]) which is equivalent to for_loop(num1, "zero").

  • Related