Thread: Getting a function to return an array

  1. #1
    Registered User
    Join Date
    Jun 2017
    Posts
    4

    Question Getting a function to return an array

    I am trying to get a function to return an array but I can not get it right; an empty array appears to be returned as nothing is displayed after "Light1"

    Code:
    #include <stdio.h>
    int * getTimings (int n)
    {
      int *lghts=malloc(3*sizeof(int));
    
      lghts[0] = n;
      if (n < 60)
        {
          lghts[1] = n + 10;
          lghts[2] = n + 20;
        }
      else
        {
          lghts[1] = n + 60;
          lghts[2] = n + 120;
        }
       
      return lghts;
    }
    
    int main ()
    {
       int *lights=malloc(3*sizeof(int));
    
      lights = getTimings(90);
        
      printf("\nLight1: %d",lights[0]
        
      return 0;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    A few notes:
    • You forgot to #include <stdlib.h> for malloc.
    • You forgot to free what you malloc
    • You used malloc in main, but that results in a memory leak because you then overwrite the pointer to the memory with the pointer returned by getTimings.
    • Although your use of malloc is correct, it would be better to write:
      Code:
      int *lghts = malloc(3 * sizeof(*lghts));
      as this way the result of sizeof would be correct even if the type of lghts was changed.
    • You seem to have made a small typo error at the end of the printf call.

    Fixing all these, although with a small tweak to the output to make it more likely to be easily readable, I compiled and ran:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int * getTimings(int n)
    {
        int *lghts = malloc(3 * sizeof(int));
    
        lghts[0] = n;
        if (n < 60)
        {
            lghts[1] = n + 10;
            lghts[2] = n + 20;
        }
        else
        {
            lghts[1] = n + 60;
            lghts[2] = n + 120;
        }
    
        return lghts;
    }
     
    int main(void)
    {
        int *lights = getTimings(90);
    
        printf("Light1: %d\n", lights[0]);
    
        free(lights);
    
        return 0;
    }
    and received the output:
    Code:
    Light1: 90
    which was expected.

    Oh, and by right you should check the return value of malloc before using it, although in this case it is unlikely to matter.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jun 2017
    Posts
    4
    Thanks, that all makes sense. When I however try and implement it into my complete code I still can not get it to work. I might be introducing a typo which I cannot see. Or could it be the online compiler that I am using, GDB online Debugger | Compiler - Code, Compile, Run, Debug online C, C++
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int getMinutes (float n)
    {
      return trunc (n);
    }
    
    int getSeconds (float n)
    {
      float integer;
      int num;
      num = round (modf (n, &integer) * 100);
      return num;
    
    }
    
    int * getTimings(int n)
    {
        int *lghts = malloc(3 * sizeof(lghts));
    
        lghts[0] = n;
        if (n < 60)
        {
            lghts[1] = n + 10;
            lghts[2] = n + 20;
        }
        else
        {
            lghts[1] = n + 60;
            lghts[2] = n + 120;
        }
    
        return lghts;
    }
    
    int
    main ()
    {
      float number;
      int min, sec, totalseconds, x;
     
      printf ("Enter a real number: ");
    
      scanf ("%f", &number);
    
      min = getMinutes (number);
    
      sec = getSeconds (number);
    
      // printf() displays the formatted output
      printf ("You entered: %.2f", number);
      printf ("\nMinutes: %d", min);
      printf ("\nSeconds: %d", sec);
    
      totalseconds = min * 60 + sec;
    
      int *lights = getTimings(totalseconds);
        
      printf ("\nTotal seconds: %d", totalseconds, "\n");
      printf("\n",lights[0]);
    
      for (x = 0; x < 3; x += 1)
        {
          printf ("\nLight %d", x + 1, ": ", lights[x]);
        }
        
      free(lights);
        
      return 0;
    }
    Output is

    Enter a real number: 1
    You entered: 1.00
    Minutes: 1
    Seconds: 0
    Total seconds: 60

    Light 1
    Light 2
    Light 3

  4. #4
    Registered User
    Join Date
    Jun 2017
    Posts
    4

    Thumbs up Resolved

    I can not use
    Code:
    printf ("\nLight %d", x + 1, ": ",lights[x]);
    Should be
    Code:
    printf ("\nLight %d %s %d", x + 1,": ",lights[x]);
    Thanks again
    Last edited by abasel; 06-28-2017 at 01:44 PM.

  5. #5
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Yeah, the normal set of arguments is printf(FormatString, argument1, argument2, ... argumentN);

    Code:
    printf("Light %d: %d\n", x + 1, lights[x]);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 01-10-2016, 01:23 AM
  2. return an array from a function
    By voldemort in forum C Programming
    Replies: 3
    Last Post: 06-30-2012, 08:21 AM
  3. Can function return an array?
    By bulletbutter in forum C++ Programming
    Replies: 21
    Last Post: 04-17-2008, 01:45 PM
  4. why can't i return Array from function?
    By chottachatri in forum C++ Programming
    Replies: 1
    Last Post: 03-20-2008, 12:55 AM
  5. return array from function
    By beginner.c in forum C Programming
    Replies: 11
    Last Post: 04-26-2007, 05:51 AM

Tags for this Thread