bugra sezer
bugra sezer

Reputation: 195

How to Return Char Array?

As you can see I have this code it takes a command (char array ) and divides it into two diffrent arrays if there is "|" between them.What I want is not to print it with the function but return these two new arrays to main and then print them ?

How can I do it ?

#include <fcntl.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <unistd.h>

int main()
{
    int a;
    int status,n,i;
    char command[4000];
    write(STDOUT_FILENO,"Shell>",6);
    n=read(STDIN_FILENO,command,4000);
    void getArguments(char **,char*);
    char *getStdOutFileName(char *);
    if(n>0)
    {
        for(i=0;i<n;i++)
        {
            bugrafonk(command,&i);
            printf("%s",First);
            printf("%s",Second);
        }
    }
}

void bugrafonk(char* c,int*length)
{
    int i;
    int a;
    char First[4000];
    char Second[4000];
    for(i=0;i<length;i++)
    {
        if(c[i]=='|')
        {
            i=a;
            for(i=0;i<a;i++)
            {
                char First[i];
            }
            printf("---");
            for(i=a;a<length;i++)
            {
                char Second[i];
            }
        }
    }
    return(First,Second);                   //is this  true ?
}

There are some unnecssery declaration in the main now just avoid them I will use them later on.

Upvotes: 0

Views: 417

Answers (4)

Andreas DM
Andreas DM

Reputation: 11028

You may choose to return a pointer to char, and then pass two arrays that will be modified in the body of the function.:

char *func(char arr[], char* arr2 /* other parameters */)
{   /* process arr and arr2 */        

    // return new array one. arr2 doesn't need to be returned.
    return arr; 
}

// in main:
char command[4000];
char arr2[4000];                  // First
char *arr3 = func(command, arr2); // Second

Or just pass all arrays as arguments to the function and then return void, since they will be modified in the function.

Upvotes: 0

Weather Vane
Weather Vane

Reputation: 34583

The function implemented here splits the string by terminating the first part, and returns a pointer to the second part.

You don't need to return 2 pieces of information since you already know where the first string is. Note that this will not work if a string literal is passed since you cannot modify it, but that's not the case here.

#include <stdio.h>
#include <string.h>

char* bugrafonk(char* c);                   // function prototype

int main(void)
{
    char command[4000];
    char *split;
    scanf("%3999s", command);               // enter limited length string
    split = bugrafonk(command);             // this splits the string
    printf("First part: %s\n", command);    // print first half
    if (split != NULL)                      // if there is a second half
        printf("Second part: %s\n", split); // print second half
}

char* bugrafonk(char* c)                    // returns string pointer
{
    char *cptr = strchr(c, '|');            // find that char
    if (cptr != NULL)                       // if it was found
    {
        *cptr = '\0';                       // terminate first part here
        cptr++;                             // advance pointer to next part
    }
    return cptr;
}

Program output:

one|two
First part: one
Second part: two

Upvotes: 0

Aleksei Petrenko
Aleksei Petrenko

Reputation: 7188

The answer depends on where you store the results of your calculations. In your current implementation both First and Second arrays are allocated inside the bugrafonk function and thus will be destroyed when the function is finished.

One possible option would be to allocate memory for result arrays outside the function and pass pointers to the function.

char first[4000], second[4000];
bugrafonk(..., first, second);
// use first and second

And bugrafonk implementation:

void bugrafonk(your arguments..., char *first, char *second)
{
    ...
}

Also, I have no idea what the mysterious word bugrafonk means ;)

Upvotes: 1

tom
tom

Reputation: 1313

To retun char * to main you need to send pointers to your function which are attached to memory that you can use

For example,

....
char return1[50]
char return2[50]
....
split_function(command, return1, return2);
....

Upvotes: 1

Related Questions