views:

541

answers:

2

Hello,

I read the example on "Passing multi-dimensional arrays in C" on this site.

It is a great example using char arrays, and I learned a lot from it. I would like to do the same thing by creating a function to handle a dynamically allocated one-dimensional integer array, and after that, create another function for handling a multi-dimensional integer array. I know how to do it as a return value to a function. But in this application I need to do it on the argument list to the function.

Just like in the example I mentioned above, I would like to pass a pointer to an integer array to a function, along with the number of elements "num" (or "row" and "col" for a 2D array function, etc.). I got a reworked version of the other example here, but I cannot get this to work, try as I might (lines of code that are new, or modified, from that example, are marked). Does anyone know how to solve this?

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define ELEMENTS 5
void make(char **array, int **arrayInt, int *array_size) { 
    int i;
    char *t = "Hello, World!";
    int s = 10; // new
    array = malloc(ELEMENTS * sizeof(char *));
    *arrayInt = malloc(ELEMENTS * sizeof(int *));  // new
    for (i = 0; i < ELEMENTS; ++i) {
     array[i] = malloc(strlen(t) + 1 * sizeof(char));
     array[i] = StrDup(t);
     arrayInt[i] = malloc( sizeof(int)); // new
     *arrayInt[i] = i * s; // new
    }
}
int main(int argc, char **argv) {
    char **array;
    int  *arrayInt1D; // new
    int size;
    int i;
    make(array, &arrayInt1D, &size); // mod
    for (i = 0; i < size; ++i) {
     printf("%s and %d\n", array[i], arrayInt1D[i]); // mod
    }
    return 0;
}
A: 

You are missing size of a row here:


arrayInt[i] = malloc( sizeof(int)); // new

should be something like:


arrayInt[i] = malloc( row_len * sizeof(int)); // new

where before you were using length of given string as the row size (also strlen(t)+1 should be in parenthesis, though the effect is the same because sizeof(char) is 1)

Nikolai N Fetissov
+1  A: 

There are quite a lot of issues in that code. Have a look at the following:

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

#define ELEMENTS 5

/*
 * A string is an array of characters, say char c[]. Since we will be creating
 * an array of those, that becomes char *(c[]). And since we want to store the
 * memory we allocate somewhere, we must be given a pointer. Hence char
 * **(c[]).
 *
 * An int doesn't require a complete array, just int i. An array of those is
 * int i[]. A pointer to those is then int *(i[]).
 */
void
make(char **(chars[]), int *(ints[]), size_t len)
{
    static char hw[] = "Hello, World!";
    size_t i = 0;

    /*
     * Allocate the memory required to store the addresses of len char arrays.
     * And allocate the memory required to store len ints.
     */
    *chars = malloc(len * sizeof(char *));
    *ints = malloc(len * sizeof(int));

    /* Fill each element in the array... */
    for (i = 0; i < ELEMENTS; i++) {
        /* ... with a *new copy* of "Hello world". strdup calls malloc under
         * the hood! */
        (*chars)[i] = strdup(hw);
        /* ...with a multiple of 10. */
        (*ints)[i] = i * 10;
    }
}

int
main(void)
{
    /* A string c is a character array, hence char c[] or equivalently char *c.
     * We want an array of those, hence char **c. */
    char **chars = NULL;
    /* An array of ints. */
    int *ints = NULL;
    size_t i = 0;

    /* Pass *the addresses* of the chars and ints arrays, so that they can be
     * initialized. */
    make(&chars, &ints, ELEMENTS);
    for (i = 0; i < ELEMENTS; ++i) {
        printf("%s and %d\n", chars[i], ints[i]);
        /* Don't forget to free the memory allocated by strdup. */
        free(chars[i]);
    }

    /* Free the arrays themselves. */
    free(ints);
    free(chars);

    return EXIT_SUCCESS;
}
Stephan202
This is great! Exactly what I was trying to do. Thanks so much. In my LabWindows/CVI run-time debugger, it complains with a non-fatal error on the "free (chars[i]);" line of code, with the message: "Attempt to free pointer to memory not allocated by malloc() or calloc().
Jumper Bones
Interesting. Well Valgrind complains if I don't free it.
Stephan202
Thanks for the extensive comments you just added. It is crystal clear what I was doing wrong now. Also, I just took my first look at the Valgrind site. Impressive! This alone was worth as much as your answer...
Jumper Bones
You're welcome :)
Stephan202
@ Stephan202: I don't want to be nitpicking but while the code is correct from the final outcome, your comments are not.> A string is an array of characters, say char c[]. Since we will be creating an array of those, that becomes char *(c[]).In fact, `char c[]` is an array, but `char *(c[])` makes c an array of points to char. For function arguments, this makes no difference, as an unbounded array is just considered a pointer.
Bluehorn