Thread: passing array of struct to function

  1. #1
    Registered User
    Join Date
    Aug 2017
    Posts
    23

    Question passing array of struct to function

    Hi,
    I'm trying to make array of struct and I don't get where is my mistake.
    can you help me please?

    This is the code:

    Code:
    typedef struct{
        int i_index;
        int j_index;
        int value;
    }index_s;
    
    int index_sum(int** arr, int rows, int cols, index_s** ans_arr){
        int i;
        int j;
        int ans = 0;
        int arr_i = 0;
    
        for(i=0; i<rows; i++){
            for(j=0; j<cols; j++){
                if ((i+j) == arr[i][j]){
                    ans++;
                }
            }
        }
        
        *ans_arr = (index_s*)malloc(ans*sizeof(index_s));
    
        for(i=0; i<rows; i++){
            for(j=0; j<cols; j++){
                if((i+j) == arr[i][j]){
                    ans_arr[arr_i]->i_index = i;
                    ans_arr[arr_i]->j_index = j;
                    ans_arr[arr_i]->value = arr[i][j];
                    arr_i++;
                }
            }
        }
    
        return ans;
    }
    
    void third_main(){
        int** arr;
        index_s* ans_arr;
        int rows, cols;
        int num_of_sums;
        
    
        printf("Enter number of rows: ");
        scanf("%d",&rows);
        printf("Enter number of cols: ");
        scanf("%d",&cols);
        
        arr = alloc_matrix(rows, cols);
    
        num_of_sums = index_sum(arr, rows, cols, &ans_arr);
        printf("i=%d\t j=%d\t value=%d\n", ans_arr->i_index, ans_arr->j_index, ans_arr->value);
    }
    Thank you

  2. #2
    Registered User
    Join Date
    Jun 2015
    Posts
    1,643
    Your code is missing necessary includes:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    "third_main" should presumably be called main and it should return an int.

    You're missing the "alloc_matrix" function.

    You should remove (index_s*) from in front of malloc. If your compiler complains, then you are compiling as C++ instead of C.

    And you probably want to access it like this:
    Code:
    (*ans_arr)[arr_i].i_index
    Last edited by algorism; 08-24-2017 at 07:48 AM.

  3. #3
    Registered User
    Join Date
    Aug 2017
    Posts
    23
    Thanks!

    I know that I am missing includes... This is just part of the code...
    The "alloc_matrix" is working fine, this is the code of it:

    Code:
    int** alloc_matrix(int rows, int cols){
        int** c;
        int i;
        int j;
    
        c = (int**)calloc(rows,sizeof(int*));
        if(!c)
            return NULL;
        for(i=0; i<rows; i++){
            c[i] = (int*)calloc(cols,sizeof(int));
            if(!c[i]){
                free_matrix(c,i);
                return NULL;
            }
            for(j=0; j<cols; j++){
                printf("arr[%d][%d]= ",i,j);
                scanf("%d",&c[i][j]);
            }
        }
        return c;
    }
    Why I need to remove (index_s*) from the malloc? I don't need to tell what type I want?

    But you helped me because the problem was the access to the struct and now all works well.
    thank you

  4. #4
    Registered User
    Join Date
    Jun 2015
    Posts
    1,643
    Quote Originally Posted by DoK View Post
    Why I need to remove (index_s*) from the malloc? I don't need to tell what type I want?
    Yes. Exactly. You don't need to tell it what type you want. C automatically converts void* to whatever type you need. If your compiler complains then you are compiling as C++ instead of C, which is a mistake. Try removing it.

    Your alloc_matrix seems overly-complicated. Normally we wouldn't malloc (or calloc) each row separately. That's a waste of time and space. The normal practice is to allocate the data as a single block and then set the row pointers.
    Code:
    int** alloc_matrix(int rows, int cols) {
        // allocate row pointers
        int **a = malloc(rows * sizeof *a);
        if (!a) return NULL;
    
        // allocate a block of memory to hold the data
        a[0] = calloc(rows * cols, sizeof **a);
        if (!a[0]) { free(a); return NULL; }
    
        // set the row pointers to the beginning of each row
        for (int i = 1; i < rows; i++)
            a[i] = a[i - 1] + cols;
    
        return a;
    }
    
    void free_matrix(int **a) {
        free(a[0]);
        free(a);
    }
    You should have a separate function to fill the matrix. Allocating it and filling it with user data are two separate things.

    And remember to check for a NULL return from alloc_matrix in main!

  5. #5
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    733
    Please read this explanation of why you should not cast calloc(), malloc(), and realloc().

  6. #6
    Registered User
    Join Date
    Aug 2017
    Posts
    23
    Thank you very much

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing an array of Struct to a sorting function
    By alien1 in forum C Programming
    Replies: 3
    Last Post: 04-02-2015, 01:48 PM
  2. Replies: 8
    Last Post: 05-10-2012, 12:07 PM
  3. Replies: 1
    Last Post: 05-30-2009, 11:04 PM
  4. Passing array of struct as a function parameter
    By desmond5 in forum C Programming
    Replies: 5
    Last Post: 12-04-2007, 11:32 AM
  5. passing array of type struct to function
    By nappaji in forum C Programming
    Replies: 4
    Last Post: 05-02-2007, 05:13 PM

Tags for this Thread