Returning pointer to array of pointers problem

This is a discussion on Returning pointer to array of pointers problem within the C Programming forums, part of the General Programming Boards category; Hello, I'm trying to return a pointer to an array of pointers to char. Basicly, I need to pass few ...

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    42

    Returning pointer to array of pointers problem

    Hello,
    I'm trying to return a pointer to an array of pointers to char. Basicly, I need to pass few strings from function to main, and for some reasons I can't find any solution to do it.

    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <stdlib.h>
    
    char *(*fun)(void)[]; 
    
    main()
    {
          char *tb[10];
               
          tb = fun();     /* this isn't working */
        
          getche();
          return 0;
    }
    
    
    char *(*fun)(void)[] {
         char *tab[10];
         int i;
         
         for (i=0; i<10; i++) 
             tab[i] = (char*) malloc(20);
    
         return tab;
    }
    Function dec/def are most likely wrong, and when I do this as in code above, I'm getting:
    Code:
    incompatible types in assignment of `char* (*)[]' to `char*[10]'
    Looking forward for some help in this case :|

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You can't return local variables. The variable goes out of scope as soon as the function ends. You'll need another level of indirection there. Then, you can allocate an array of pointers to characters, then allocate space for them all, then return the allocated array.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    42
    Quote Originally Posted by quzah
    You can't return local variables. The variable goes out of scope as soon as the function ends. You'll need another level of indirection there. Then, you can allocate an array of pointers to characters, then allocate space for them all, then return the allocated array.


    Quzah.
    Thanks! Here's solution in case somebody has similar problem:

    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <stdlib.h>
    
    char **fun(void);
    
    main()
    {
          char **tb;
          int i;
               
          tb = fun();
               
    /* printing result, which will be earlier set chars in array, a-j, each in new line */
          for (i=0; i<10; i++)
              printf("%s", *(tb+i));                
       
          getche();
          return 0;
    }
    
    char **fun(void) {
    /* allocating memory for array... */
         char **tab = (char**) calloc(10, sizeof(char*));
         int i;
         
    /* ...and for each string */
         for (i=0; i<10; i++) {
             *(tab+i) = (char*) malloc(20);
    /* setting strings values to a-j letter */
             *(*(tab+i)) = 97+i;
             *(*(tab+i)+1) = '\n';
             *(*(tab+i)+2) = '\0';
         }
         return tab;
    }
    Thanks again quzah

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,531
    > *(tab+i) = (char*) malloc(20);
    1. Read the FAQ on casting malloc (you shouldn't need to do this in C)
    2. Why not write tab[ i ] instead of all that extra verbage of * ( ) and +
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >2. Why not write tab[ i ] instead of all that extra verbage of * ( ) and +
    Because it makes your code look l33ter.
    My best code is written with the delete key.

  6. #6
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    Quote Originally Posted by Prelude
    Because it makes your code look l33ter.
    Correction. That's 1337er.
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  7. #7
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    Or 13373r.
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  8. #8
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    Or 13373|2.
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  9. #9
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    0|2 13373|2 Enough!
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  10. #10
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,531
    Maybe I should just write
    i [ tab ] = malloc(20);
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  11. #11
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    Yes, that would solve the cryptic use of "elite" (and make the code more readable).
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  12. #12
    Registered User
    Join Date
    Nov 2006
    Posts
    42
    Quote Originally Posted by Salem
    > *(tab+i) = (char*) malloc(20);
    1. Read the FAQ on casting malloc (you shouldn't need to do this in C)
    Right, guess *(tab+i) = malloc(20); will do.
    Quote Originally Posted by Salem
    2. Why not write tab[ i ] instead of all that extra verbage of * ( ) and +
    Is there anything wrong with that? Except that my code looks 1337er
    I'll check pointers tutorial tommorrow to see how can I simplify it.

    Thanks for advices.

  13. #13
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Is there anything wrong with that?
    Yeah, it's less readable and ugly.
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  14. #14
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,531
    > Is there anything wrong with that? Except that my code looks 1337er
    Only to those who don't know what they're talking about, or confuse "obscure" with "better" or "faster".
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  15. #15
    Registered User
    Join Date
    Nov 2006
    Posts
    42
    So... this I how the function should look like, or am I still missing something?

    Code:
    char **fun(void) {
         char **tab = (char**) calloc(10, sizeof(char*));
         int i;
         
         for (i=0; i<10; i++) {
             *(tab+i) = malloc(20);
             tab[i][0] = 97+i;
             tab[i][1] = '\n';
             tab[i][2] = '\0';
         }
         return tab;
    }
    Last edited by jimzy; 11-11-2006 at 05:30 AM.

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

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  2. problem returning array from function(among others)
    By Calef13 in forum C++ Programming
    Replies: 30
    Last Post: 10-30-2006, 03:26 PM
  3. Replies: 41
    Last Post: 07-04-2004, 03:23 PM
  4. sending or recieving a[][]to a function
    By arian in forum C++ Programming
    Replies: 12
    Last Post: 05-01-2004, 10:58 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM

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