Technopolice
Technopolice

Reputation: 497

Returning array of arrays in C

I have the following code, which works fine

#include <stdlib.h>
void transpose();


void main(){
    transpose();

}

void transpose() {
    int arr[] = {2, 3, 4, 1};
    int l = sizeof (arr) / sizeof (arr[0]);
    int i, j, k;
    for (i = 0; i < l; i++) {
        j = (i + 1) % l;
        int copy[l];
        for (k = 0; k < l; k++)
            copy[k] = arr[k];
        int t = copy[i];
        copy[i] = copy[j];
        copy[j] = t;
        printf("{%d, %d, %d, %d}\n", copy[0], copy[1], copy[2], copy[3]);
    }
}

However what I want to do is, pass an array to transpose function and the transpose function would return the list of arrays.

So I tried the following code:

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

void print_array(int a[], int num_elements);

void main(){
    int b[16];
    int a[] = {2, 3, 4, 1};
    c= transpose(a);
    print_array(c,16);
}

int transpose(int arr) {
    //int arr[] = {2, 3, 4, 1};
    int b[16];
    int l = sizeof (arr) / sizeof (arr[0]);
    int i, j, k;
    for (i = 0; i < l; i++) {
        j = (i + 1) % l;
        int copy[l];
        for (k = 0; k < l; k++)
            copy[k] = arr[k];
        int t = copy[i];
        copy[i] = copy[j];
        copy[j] = t;
        printf("{%d, %d, %d, %d}\n", copy[0], copy[1], copy[2], copy[3]);
        b=copy;
    }
    return b;
}


void print_array(int a[], int num_elements)
{
   int i;
   for(i=0; i<num_elements; i++)
   {
     printf("%d ", a[i]);
   }
   printf("\n");
}

But some errors. I would like NOT to work with pointers, so how to tackle this?

Also I know the print_array function is defined to print single array, I will modify it to print all arrays by a for loop. Is that a correct approach?

Upvotes: 0

Views: 97

Answers (1)

BLUEPIXY
BLUEPIXY

Reputation: 40145

Perhaps, rewrite as your desire. (Logic intact)

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

void print_array(int **a, int num_elements);
int **transpose(int n, int arr[n]);

int main(){
    int a[] = {2, 3, 4, 1};
    int **c;
    int n = sizeof(a)/sizeof(*a);
    int i;

    c= transpose(n, a);
    print_array(c, n);
    //deallocate
    for(i=0;i<n;++i)
        free(c[i]);
    free(c);
    return 0;
}

int **transpose(int n, int arr[n]){
    int l = n;
    int **b = malloc(l * sizeof(*b));//sizeof(*b) : sizeof(int *)
    int i, j, k;
    for (i = 0; i < l; i++) {
        j = (i + 1) % l;
        int *copy = malloc(l * sizeof(*copy));//sizeof(int)
        for (k = 0; k < l; k++)
            copy[k] = arr[k];
        int t = copy[i];
        copy[i] = copy[j];
        copy[j] = t;
        //printf("{%d, %d, %d, %d}\n", copy[0], copy[1], copy[2], copy[3]);
        b[i] = copy;
    }
    return b;
}

void print_array(int **a, int num_elements){
    int i, j;
    for(i=0; i<num_elements; i++){
        for(j=0; j<num_elements; j++)
            printf("%d ", a[i][j]);
        printf("\n");
    }
}

Upvotes: 1

Related Questions