Help with returning an array

This is a discussion on Help with returning an array within the C Programming forums, part of the General Programming Boards category; Hello. I need to return the array to my main function, but i can't get it working. Can some one ...

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    17

    Help with returning an array

    Hello.

    I need to return the array to my main function, but i can't get it working.
    Can some one help me.
    Code:
    char teste(char s[])
    {
        char *tok[3], *p;
        int i;
    
        p = strtok(s," ");
        for (i = 0; p && i < 3; i++) {
            tok[i] = p;
            p = strtok(NULL," ");
    	}
    	/*if (i < 3) {
            fprintf(stderr,"Parametros em falta\n");
            return -1;*/
    return tok;
    }
    int main()
    {
    
    char op[]="p 1 2";
    char *comando=teste(op);
    printf("COMANDO1: %s",comando[0]);
    }

  2. #2
    Registered User
    Join Date
    Nov 2009
    Location
    Italy
    Posts
    65
    char teste(char s[]) should be a char* since you want to return an array

  3. #3
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Quote Originally Posted by rob90 View Post
    char teste(char s[]) should be a char* since you want to return an array
    this does not fixes the main problem - you cannot return pointer to local var.

    there are 2 possibilities -
    1. pass output array as parameter and fill it in the function
    2. dynamically allocate memory for the array you are filling and do not forget to free it after you finish using it
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  4. #4
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Quote Originally Posted by vart View Post
    this does not fixes the main problem - you cannot return pointer to local var.
    But in this case the return value is assigned to a local variable in main. Wouldn't that solve the problem, like this?

    Code:
    #include <stdio.h>
    char *st();
    
    int main()
    {
            char *s = st();
            puts(s);
            puts(st());
    
            return 0;
    }
    char *st(){
            char *s = "There!";
            return s;
    }

  5. #5
    Registered User
    Join Date
    Jan 2009
    Location
    Australia
    Posts
    375
    No, in that example you are returning a pointer to a string literal. The string literal is in read only memory and will not be overwritten by anything.

    In your previous example you are returning a pointer to a local variable. This could be overwritten on other adjacent function calls and is probably undefined or implementation specific.

    As vart said, you should use malloc to create your array on the heap or you can supply the function with the variable and fill it then.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,304
    Quote Originally Posted by Subsonics
    But in this case the return value is assigned to a local variable in main. Wouldn't that solve the problem, like this?
    Your example might work, but that would be cause it deals with a string literal. If s in st was an array instead of a pointer to the first character of a string literal, then it would mean returning a pointer to a local variable. That the return value is assigned to a local variable in main is irrelevant.

    HIT_Braga: you do need extra space to hold those tokens by copying them. You cannot just store pointers to the first characters of those substrings because strtok() is destructive.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Quote Originally Posted by laserlight View Post
    Your example might work, but that would be cause it deals with a string literal.
    Ok, point taken. I was a bit surprised myself at first to be honest.

  8. #8
    Registered User
    Join Date
    Nov 2008
    Location
    INDIA
    Posts
    64
    Like this we can return the array ,

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    char* teste(char* s)
    {
            char *tok = (char *)malloc(3);
        char *p;
        int i;
    
        p=(char *)strtok(s," ");
        for (i = 0; p && i < 3; i++) {
            tok[i] = *p;
            p = (char *)strtok(NULL," ");
            }
            /*if (i < 3) {
            fprintf(stderr,"Parametros em falta\n");
            return -1;*/
        return tok;
    }
    int main()
    {
    
    char op[]="p 1 2";
    //teste(op);
    char *comando=teste(op);
    printf("COMANDO1: %c",comando[0]);
    }
    I used pointer here ,because if you use array you will get warning because Its a local variable.So we can not return that variable's address.
    Last edited by karthigayan; 04-02-2010 at 07:13 AM.

  9. #9
    Registered User
    Join Date
    Jun 2005
    Posts
    6,664
    Now the thing to remember is a "free (comando);" in main(), to avoid a memory leak.

    While often not necessary with modern operating systems, it is still good practice to control the resources used by your program. Imagine that code run in a loop .....
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Sunshine, and read this, this, and this before posting again.

  10. #10
    Registered User
    Join Date
    Mar 2010
    Posts
    17

    Question

    Hello
    Thanks for the help people.
    The code is working great but just one thing.
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    char* teste(char* s)
    {
            char *tok = (char *)malloc(3);
        char *p;
        int i;
    
        p=(char *)strtok(s," ");
        for (i = 0; p && i < 3; i++) {
            tok[i] = *p;
            p = (char *)strtok(NULL," ");
            }
            /*if (i < 3) {
            fprintf(stderr,"Parametros em falta\n");
            return -1;*/
        return tok;
    }
    int main()
    {
    
    char op[]="p 1 2";
    //teste(op);
    char *comando=teste(op);
    printf("COMANDO1: %c",comando[0]);
    }
    With this code the return is an single char. if i change the op=" cr test" it will only return "c" and i need to return "cr" and "test".
    There are many op:
    "p 1 1" - return p and 1 and 1
    "vp" - return only vp
    "cr test" - return cr and test

    So can how can i change the code for working nice ? making some changes on this:
    Code:
     char *tok = (char *)malloc(3);
    to:
    Code:
     char *tok[3]= (char *)malloc(3);

  11. #11
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    you need
    Code:
    char** tok = malloc(3* sizeof *tok);
    and later
    Code:
    tok[i] = p;
    of course you'll need to update the return type of the function
    you'll get array of 3 pointers to the original buffer...

    to support random number of parameters in the string - you'll need to implement some solution using realloc and some way to indicate number of options found - for example adding NULL pointer as last member of the array to be returned...
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  12. #12
    Registered User
    Join Date
    Mar 2010
    Posts
    17

    Angry

    Hello again.

    Can help me

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    char** teste(char* s)
    {
        char** tok = malloc(3* sizeof *tok);
        char *p;
        int i;
    
        p=(char *)strtok(s," ");
        for (i = 0; p && i < 3; i++) {
            tok[i] = p;
            p = (char *)strtok(NULL," ");
            }
            /*if (i < 3) {
            fprintf(stderr,"Parametros em falta\n");
            return -1;*/
        return tok;
    }
    int main()
    {
    
    char op[]="cr teste";
    //teste(op);
    char *comando=teste(op);
    printf("COMANDO1: %c\n",comando[0]);
    }
    Still not function as i want. I cannot make it working.

  13. #13
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Code:
    char *comando=teste(op);
    printf("COMANDO1: %c\n",comando[0]);
    you should pay attention to the compiler warnings


    Code:
    char **comando=teste(op);
    printf("COMANDO1: %s\n",comando[0]);
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  14. #14
    Registered User
    Join Date
    Mar 2010
    Posts
    17
    Thks vart i'm noob sorry.

    just a simple * dahhh

  15. #15
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Quote Originally Posted by HIT_Braga View Post
    Thks vart i'm noob sorry.

    just a simple * dahhh
    not only that - also the correct format for printing the string
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Parsing and returning array pointers?
    By thealmightyone in forum C Programming
    Replies: 26
    Last Post: 03-26-2009, 04:38 PM
  2. Returning an Array
    By mmarab in forum C Programming
    Replies: 10
    Last Post: 07-19-2007, 08:05 AM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 10:54 AM
  4. Replies: 6
    Last Post: 10-21-2003, 10:57 PM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 09:31 AM

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