0

I am confused and clueless as how can I return an Array of pointers to a String(Basically 2D array) from a function.

What I did(or about to do )in this code is , first insert words/names and then stored it in the array and also input the number 'n'. Than I passed this array and number into the function and their extracted the last 'n' names/words from the array and print that in the main.

Here is my code :

#include<stdio.h>
#include<conio.h>
#include<alloc.h>
#include<string.h>
char** fun(char *s[5],int no)
{
    char *f[5];
    int i,j=0;
    for(i=5-no;i<5;i++)
    {
        f[j]=(char*)malloc(strlen(s[i])+1);
        strcpy(f[j],s[i]);
        j++;
    }
    /*for(j=0;j<no;j++)         //I did it just to check whether 'f' stores the last 'n' names.
    printf("\n%d. %s",j+1,f[j]);*/
    return f;
}
void main()
{
    char *name[5],*str,*list[5];
    int i,n;
    clrscr();
    printf("Enther the Names of the list : " );
    for(i=0;i<5;i++)
    {
        printf("%d. ",i+1);
        str=(char*)malloc(30);
        gets(str);
        name[i]=(char*)malloc(strlen(str)+1);
        strcpy(name[i],str);
        free(str);
    }
    clrscr();
    printf("\nEntered Names are : ");
    for(i=0;i<5;i++)
    printf("\n%d. %s",i+1,name[i]);
    printf("\n Enter the no. :");
    scanf("%d",&n);
    *list=*fun(name,n); // I am little confused as to how should I wrote this ?
    for(i=0;i<n;i++)
    printf("\n%d. %s",i+1,list[i]);
    getch();
}

Suppose I gave the Input as :

1.the

2.there

3.this

4.that

5.therefore

Enter the No.: 3

Output:

 1.this

 2.<Some special characters>

 3.<Some special characters>

I would more interested in the method which uses the pointer approach . PS: I am Using Turbo C/C++ IDE as I am in a Learning phase of C .

user2805872
  • 173
  • 1
  • 1
  • 4
  • 1
    You can't return a local automatic array, it's undefined behavior. Have a look at `malloc()` (and do not cast its return value), or have your function fill in an array passed to it as its argument. –  Sep 23 '13 at 06:13
  • 1
    possible duplicate of [Declaring a C function to return an array](http://stackoverflow.com/questions/1453410/declaring-a-c-function-to-return-an-array) –  Sep 23 '13 at 06:14
  • @H2CO3 even if I do static char *f[5] , I get the same problem . Is there any alternative ? btw the link you gave I feel it's completely different because I emphasize on pointer approach to this problem. I hope I am clear enough – user2805872 Sep 23 '13 at 06:19
  • 1
    Apart from `static` and `malloc()`? No, there isn't. I can assure you that this is trivial to do, and if you are getting errors, you should google the error message to see what it means, then adjust your code accordingly until it works. –  Sep 23 '13 at 06:20
  • 1
    `main` must return `int`. – n. m. could be an AI Sep 23 '13 at 08:24

2 Answers2

0

As H2CO3 pointed out, define the fun as fun(list, name, n); where list is your output list of string. Pass the list from main(), fill it up inside fun().

0xF1
  • 6,046
  • 2
  • 27
  • 50
  • @user2805872 This question has already been over-asked on SO. I suggest you take a look at the duplicate. –  Sep 23 '13 at 06:16
0

You can not define array of pointers of char locally(which will allocate it on stack. scope of it is inside that function only) and use it globally. inside fun() you can use char f = (char)malloc(sizeof(char*[5])); (allocate memory on heap which you can use it globally) instead of char *f[5];

code should be:

#include<stdio.h>
#include<conio.h>
#include<string.h>
#include <malloc.h>
char** fun(char *s[5],int no)
{
    char **f = (char**)malloc(sizeof(char*[5]));
    int i,j=0;
    for(i=5-no;i<5;i++)
    {
        f[j]=(char*)malloc(strlen(s[i])+1);
        strcpy(f[j],s[i]);
        j++;
    }
    /*for(j=0;j<no;j++)         //I did it just to check whether 'f' stores the last 'n' names.
    printf("\n%d. %s",j+1,f[j]);*/
    return f;
}

    void main()
    {
        char *name[5],*str,**list;
        int i,n;

        printf("Enther the Names of the list : " );
        for(i=0;i<5;i++)
        {
            printf("%d. ",i+1);
            str=(char*)malloc(30);
            gets(str);
            name[i]=(char*)malloc(strlen(str)+1);
            strcpy(name[i],str);
            free(str);
        }

        printf("\nEntered Names are : ");
        for(i=0;i<5;i++)
            printf("\n%d. %s",i+1,name[i]);
        printf("\n Enter the no. :");
        scanf("%d",&n);
        list=fun(name,n); // I am little confused as to how should I wrote this ?
        for(i=0;i<n;i++)
            printf("\n%d. %s",i+1,list[i]);
        getch();
    }
Rocker
  • 25
  • 5