0

I am new to C programming and especially to pointers. In the program I wrote, I tried to write a function that returns a pointer to specified column of array. See the code below for better understanding (or confusion :) ):

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

// function for getting pointer to specidifed column index
// 'ind' is index of requested column, 'ncol' is number of items in column
int* get_col(const int* arr, unsigned int ind, unsigned int ncol);

int main() {
    unsigned int n;

    printf("Input matrix size : ");
    scanf("%i", &n);

    int arr[n][n];

    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++)
            arr[i][j] = i * n + j;
    }

    for (int i = 0; i < n; i++) {
        printf("values in column %d: \n", i);
        int *col = get_col((int*)arr, i, n);
        for (int j = 0; j < n; j++) {
            printf("%d ", *col);
            col = col + 1;
        }
        printf("\n");
    }

    return 0;
}

int* get_col(const int* arr, unsigned int ind, unsigned int ncol) {
    int *result = malloc(sizeof(int) * ncol);

    for (int i = 0; i < ncol; i++)
        *result = *(arr + i*ncol + ind);

    return result;
}

As you see get_col function accepts pointer to array, column index and column size (n of elements in column, i.e number of rows) as arguments and trying to return a pointer to 1D array that contains values of column at requested index. The problem is that result is not correct. In case n=3 results are like below:

Input matrix size : 3
values in column 0: 
6 0 0 // supposed to be 0 3 6
values in column 1: 
7 0 0 // supposed to be 1 4 7
values in column 2: 
8 0 0 // supposed to be 2 5 8

I think that the problem lies in my understanding of pointers not the algorithm implemented. Actually, at first I didn't use pointer in my get_col function like below:

int result[ncol];

// ... do my work here to populate array

return &result;

Then as compiler complains warning: function returns address of local variable [-Wreturn-local-addr], I converted result from array to pointer in get_col function like above. What is the problem in this code? Did I use pointers in get_col function as it should be?

3
  • You already have a nice answer, but remember to free the pointer that you allocated inside the "get_col" function before the program finishes. In this case is not very problematic because the program ends and the operative system will do it, but it's a good habit to free all the memory you allocate, otherwise your programs will run full of memory leaks, and then crashes because "out of memory". it's just a tip since you say you're new to C and pointers. ;) Commented Apr 17, 2020 at 17:52
  • @RaúlHerrero thanks for the tip. Can you tell exactly where i should call free in this code? Commented Apr 17, 2020 at 17:56
  • 1
    free any pointer after you are not going to use it anymore. In this case, after the last printf, at the end of every loop of the for loop (second i++ loop, which it's used for printing the results), where you still have the pointer defined. Commented Apr 18, 2020 at 19:14

1 Answer 1

1

In the following line:

*result = *(arr + i*ncol + ind);

You're always writing to the same memory address.

Change it to one of the two following options:

*(result + i) = *(arr + i*ncol + ind);
result[i] = *(arr + i*ncol + ind);

Regarding your second problem when you used:

int result[ncol];

// ... do my work here to populate array

return &result;

You should understand that result variable in this case (static-memory allocation) is stored in the stack. So, after your function returns, the variable values doesn't exist anymore in the memory. That's why you need dynamic-memory allocation. In dynamic-memory allocation, that values stay in the memory until you call free by yourself.

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.