0

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?

9
  • 2
    You haven't declared c in main Commented Dec 29, 2014 at 3:12
  • 1
    You will need to pass some sizes as well as your arrays. And the only way to pass an array to a function is as a pointer, so you're not going to get around that. Do you always have 2x2 arrays? Commented Dec 29, 2014 at 3:26
  • 4
    int l = sizeof (arr) / sizeof (arr[0]); isn't going to work in your second program's function, since it receives arr as a pointer, not an array, as @CarlNorum mentions. You can't return arrays from functions in C, either, and you shouldn't return a pointer to an array local to your function, since it'll cease to exist when your function returns. Your best bet is probably to pass both arrays into your function. Commented Dec 29, 2014 at 3:26
  • 1
    Your compiler should be presenting many warnings with this code. If you enable all warnings and solve those, you will save time. If you are not getting warnings after enabling them all, consider a new compiler. Commented Dec 29, 2014 at 3:32
  • 1
    The list of warnings and errors in this code make it far from being anywhere-near-ready to address the actual title of this question, see here for many of them. Commented Dec 29, 2014 at 3:45

1 Answer 1

1

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");
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.