function that returns an array

Write your question here.

1
2
3
4
5
6
7
8
9
10
11
 int * before_after_clauses(char *line){
	int num = check_slash(line);
	int start = 0;
	char slice_before[num + 1];
	char slice_after[num + 1];
	memcpy(slice_before, line + start, (num -1)*sizeof(char)); //after before 
	memcpy(slice_after, line + num, (num)*sizeof(char)); //after slash
	slice_before[num] = 0;
	slice_after[num] = 0;
        return slice_before, slice_after;
}

the above won't work because the function is not returning to an int, it is returning 2 arrays. what modification i need to make?
It is not returning two arrays. The function is supposed to return a pointer to int.
Then the return statement is returning slice_after which becomes a pointer to char.

The comma , in between slice_before and slice_after is the comma operator.

http://stackoverflow.com/questions/54142/how-does-the-comma-operator-work

1
2
3
4
5
6
7
8
9
10
11
#include <stdio.h>

const char * comma_abuse(void)
{
    return "One", "Two", "Three";
}

int main(void)
{
    printf("%s\n", comma_abuse());
}
Three


the above won't work because the function is not returning to an int, it is returning 2 arrays. what modification i need to make?

Make it so that the function accepts two arrays as parameters, named slice_before and slice_after.

Since you're working in C and those two parameters would be pointers to char, your function should also accept two sizes.

1
2
3
4
5
6
7
8
9
10
11
12
#include <stddef.h>

// ...

int before_after_clauses(/* const */ char   *line,
                         char               *slice_before,
                         size_t              slice_before_len,
                         char               *slice_after,
                         size_t              slice_after_len)
{
    // ...
}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
int * before_after_clauses(char *line){
	int num = check_slash(line);
	int start = 0;
	char slice_before[num + 1];
	char slice_after[num + 1];
	memcpy(slice_before, line + start, (num -1)*sizeof(char)); //after before 
	memcpy(slice_after, line + num, (num)*sizeof(char)); //after slash
	slice_before[num] = 0;
	slice_after[num] = 0;
        return slice_before, slice_after;
}

Err. I think either you misunderstand my question or I have no idea on what you are talking about.
That code will take *line and output two array.
for example.
line = '123\456789'
Check slash will check the position of '\' in the line and output two array.
slice_before as '123'
slice_after as '456789'

Oh I think I know what I should do but give me some pointers as well.
slice_before and slice_after's memory will be free once the function is done with. So I guess I should use malloc() to hold the memory of slice_before and slice_after. Then my function should return the 2 pointers pointing to those arrays??
Last edited on
In C++ you can't return an array, let alone return more than one variable.
However you can do this this to the header:

void before_after_clauses(char *line, &slice_before[], &slice_after[])

You will need to define variables to put for slice_before and slice_after, in your main however.

P.S. Also remove the return at the end of the code.
Last edited on
Topic archived. No new replies allowed.