如何编写一个可以同时采用动态/静态分配的2D数组的c函数?

时间:2015-05-28 07:28:37

标签: c arrays function gcc4.7

我有一个假设将2D数组作为参数的函数,我的代码看起来像这样 -

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

void func(double**, int);

int main()
{
    double m[3][3] = {{1, 1, 1}, {2, 2, 2}, {3, 3, 3}};
    func(m, 3);
}

void func(double **m, int dim)
{
    int i, j ;
    for(i = 0 ; i < dim ; i++)
    {
        for(j = 0 ; j < dim ; j++)
            printf("%0.2f ", m[i][j]);
        printf("\n");
    }
}

然后编译器说 -

test.c: In function ‘main’:
test.c:9:2: warning: passing argument 1 of ‘func’ from incompatible pointer type [enabled by default]
  func(m, 3);
  ^
test.c:4:6: note: expected ‘double **’ but argument is of type ‘double (*)[3]’
 void func(double**, int);
      ^

但是当我说 -

int main()
{
    int i, j;
    double m[3][3] = {{1, 1, 1}, {2, 2, 2}, {3, 3, 3}};
    double **m1 ;
    m1 = (double**)malloc(sizeof(double*) * 3);
    for(i = 0 ; i < 3 ; i++)
    {
        m1[i] = (double*)malloc(sizeof(double) * 3);
        for(j = 0 ; j < 3 ; j++)
            m1[i][j] = m[i][j] ;
    }
    func(m1, 3);
    for(i = 0 ; i < 3 ; i++) free(m1[i]);
    free(m1);
}

它编译并运行。

有什么方法可以让func()同时采用静态/动态定义的2D数组?我很困惑,因为我传递指针m,为什么第一种情况不正确?

这是否意味着我需要为两种不同类型的参数编写两个单独的函数?

1 个答案:

答案 0 :(得分:5)

您对二维数组的动态分配不正确。用它作为:

{'Chennai': ['Adambakkam # 044 22530209 # Opp. Murugan Temple, ; Brindavan Nager, ; Mohanpuri - 5th Street, ; Adambakkam, Chennai \x96 600 088',
             'Adambakkam - 2 # 044 - 22553195, 64540549 # No. 2 B, Ground Floor, Ganesh Nagar Main Road, ; Near NGO Colony Bus Stop, Telephone Colony, ; Adambakkam, Chennai - 600088.',
             'Allapakkam # 044- 64520024 # New No.131, Old No.10 M, ; Shop No. F, Alapakkam Main Road, ; Near Jeva Complex, Alapakkam, Chennai-16.',
             'Anna nagar # 044-26220891 # New No.1, AI Block, Second Street, ; Near Anna Adarsh College for Women, ; Shanthi Colony, Anna Nagar, ; Chennai- 600040.',
 ...
 'Visakhapatnam': ['Adarsh Nagar # 9247001943 # H. No. 3-352, Beside Andhra Bank, ; Near Manapuram Finance Ltd. Adarsha Nagar, ; Old Dairy Form, Visakhapatnam',
                   'B.C. Road, Gajuwaka # 0891 2546005  # D. No.13-6-14/1, ; Opp. Dr. T. Dhanalatha Hospital, ; B. C. Road, Gajuwaka, Visakhapatnam.',
                   'Chinawaltair # 0891-2546001, 6464501 # D.No: 6-5-3, Opp. Jaganadh Temple, ; China Waltair, Visakapatnam-17.',
                   'Marripalem #  9247000573 # D. No. 38-40-70,  Opp. Ramalingeswara Alayam, ; Marripalem Main Road, ; Marripalem, Visakhapatnam.',
                   'Muralinagar #  0891-6464507# D.No.39-8-9/5, ; Varma Complex, 48th Bus stop, ; Murali Nagar, Visakhapatnam',
                   'NRI Hospital # 0891-2714453, 6464506 # 50-27-16, Rammahon Chamber, ; Near NRI Hospital, ; Seethammadhara, Visakapatnam.',
                   'Pedawaltair # 0891-2546006 # H.No.8-1-97/2/2, ; Near Vishaka Eye Hospital, ; Pedawaltair junction, Vizag.',
                   'Ramnagar # 0891-2546002, 6464502 # D.No. 10-50-11/2, 1st Floor, ; Beside Care Hospital, Main Road, ; Ramnagar, Visakapatnam.',
                   'Seetammadhara # 0891-2713706, 6464504 # H.No: 55-14-109/1, ; Beside Sri Sivaramareddy Sweets, ; Opp to E- Seva kendram, ; Seetammadhara, Visakhapatnam."']}

然后它会起作用。

此外,将函数原型更改为:

double (*m1)[3] = malloc(sizeof(double[3][3]));

另一种方法是使用大小为void func(double m[][3], int dim) 的1-D数组,而不是2-D数组。

Working example

从@TheParamagneticCroissant c99的评论开始,您也可以使用VLA并使您的维度变量。 (你需要正确分配2D数组)

将功能签名更改为:

w * h

Working example