تمرير المصفوفات الصحيحة المخصصة ديناميكيا في ج

StackOverflow https://stackoverflow.com/questions/894604

سؤال

قرأت المثال على "تمرير صفائف متعددة الأبعاد في ج" على هذا الموقع.

إنه مثال رائع باستخدام صفائف Char، وتعلمت الكثير منه. أود أن أفعل نفس الشيء من خلال إنشاء وظيفة للتعامل مع مجموعة عدد صحيح واحد الأبعاد المخصص بشكل حيوي، وبعد ذلك، قم بإنشاء وظيفة أخرى للتعامل مع مجموعة عدد صحيح متعدد الأبعاد. أنا أعرف كيفية القيام بذلك كقيمة العودة إلى وظيفة. ولكن في هذا التطبيق، أحتاج إلى القيام بذلك في قائمة الوسيطة إلى الوظيفة.

تماما مثل في المثال الذي ذكرته أعلاه، أود أن أقدم مؤشر إلى مجموعة عدد صحيح إلى وظيفة، إلى جانب عدد العناصر "الأسطوانات" (أو "الصف" و "COL" لوظيفة صفيف ثنائية الأبعاد، إلخ. ). حصلت على نسخة إعادة صياغة من الأمثلة الأخرى هنا، لكنني لا أستطيع الحصول على هذا للعمل، حاول كما يمكنني (خطوط التعليمات البرمجية الجديدة، أو تعديل، من ذلك المثال، ملحوظ). هل من احد يعرف كيفية حل هذا؟

#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;
}
هل كانت مفيدة؟

المحلول

هناك الكثير من القضايا في هذا القانون. إلقاء نظرة على ما يلي:

#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;
}

نصائح أخرى

أنت تفتقد حجم الصف هنا:


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

يجب أن يكون شيء مثل:


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

حيث قبل أن تستخدم طول سلسلة معين مثل حجم الصف (أيضا strlen(t)+1 يجب أن يكون بين قوسين، على الرغم من أن التأثير هو نفسه ل sizeof(char) هو 1)

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top