Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

void function returns nothing – C

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;
}

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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").

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading