problem with function and array

This is a discussion on problem with function and array within the C Programming forums, part of the General Programming Boards category; hey guys, is it possible for a function to return an array?? i tried but some error comes.. if its ...

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    8

    problem with function and array

    hey guys,


    is it possible for a function to return an array??

    i tried but some error comes.. if its possible please tell me how. and a sample code if possible.

    and


    i wrote a code to get a pointer value as return type from my fuction.
    but it is not happening. some error C4047 int * differs in level of indirection. its showing..

    i need explanation!!!

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,681
    Of course it is... Can you please show me your try?
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  3. #3
    Registered User
    Join Date
    Jan 2013
    Posts
    8
    Code:
    #include <stdio.h>
    
    
    int directory()
    {
        char a1[3]={'a','n','d'};
        
        char *p;
        p=&a1[0];
    
    
    printf("%x",p);
        return p;
    }
    void main()
    {
        int j;
        j=directory();
    }

  4. #4
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,681
    Thank you.

    Usually we write main like this
    Code:
    int main(void)
    {
        ...
        return 0;
    }
    If you want to return an array, an array that is created locally, then what will happen when the function terminates? The locally created variables are going to be "killed". As a result the pointer of the array will be set to point to garbage. As a result, when you try to access what the pointers points to, you will receive a segmentation fault.

    What you need to do is to dynamically allocate memory via malloc (there are other ways too, but I would strongly recommend this way, if you really need to return an array) .

    Oh and of course you can return a "whole array". What you return is a reference to it, a pointer.

    You have to imagine the pointer as a smart guide, that can lead you to secret places, where the treasure is stored. Only that guide knows the location of the treasure.
    The treasure is the data of the array, the location of the treasure is the location where the memory is stored in the memory and the guide is the pointer.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  5. #5
    Registered User
    Join Date
    Jan 2013
    Posts
    8
    ohhh...

    well i dint understand much from it.i dont have much knowledge about it. jst started coding from past few months. but soon i will. and that day i will be thankful to your explanation.

    still. thanks a lot for telling it is possible. atleast my curisity is still alive. thanks for your time...

  6. #6
    Registered User
    Join Date
    Jan 2013
    Posts
    8
    actually i understood the problem. local variables get killed, so pointer has nothing but garbage to point to.

    but this malloc stuff is alien to me.. can u tell me wat it exactly do.

  7. #7
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,681
    Good that you understand that. It is a very common mistake

    Also, welcome to the forum!!

    The best way to start learning about something is to read the reference and then code it, get errors, debug it and then...get errors, debug it and again and again until you are ok

    ref of malloc

    PS - I know that feeling, when you read an explanation after you gain a little experience, you really understand much more
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  8. #8
    Registered User
    Join Date
    Nov 2012
    Posts
    1,118
    Quote Originally Posted by sahilpatel_13 View Post
    Code:
    #include <stdio.h>
    
    int directory()
    {
        char a1[3]={'a','n','d'};
     ...    
         return p;
    }
    You declared `directory' to return int but you are actually returning p, which is a char *. Also, as pointed out, the contents of the array will be garbage once you leave the function. Make the array static if you want a quick fix for this problem without using dynamic memory:

    Code:
    #include <stdio.h>
    
    char *directory()
    {
        static char a1[4]={'a','n','d','\0'};
         
        char *p;
        p=&a1[0];
     
        printf("%p\n", p);
        return p;
    }
    
    int main(void)
    {
        char *j;
        j=directory();
        // ...
        printf("contents of j: \"%s\"\n", j);
        return 0;
    }
    Also notice: char arrays typically have '\0' at the end. This is required to print its contents with printf.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. function, array, pointer problem
    By alfaceor in forum C++ Programming
    Replies: 1
    Last Post: 03-01-2012, 06:12 PM
  2. String array function problem
    By albundy in forum C Programming
    Replies: 7
    Last Post: 08-08-2011, 06:09 PM
  3. passing array to function problem
    By thestien in forum C++ Programming
    Replies: 13
    Last Post: 05-10-2008, 01:45 AM
  4. Odd problem: returning array from function
    By disorder in forum C++ Programming
    Replies: 10
    Last Post: 04-20-2008, 01:21 AM
  5. array function(a tiny problem)
    By ayesha in forum C++ Programming
    Replies: 1
    Last Post: 10-14-2001, 01:48 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21