【问题标题】:C - Calling function of "mixedly allocated" arrayC - “混合分配”数组的调用函数
【发布时间】:2016-10-08 23:05:01
【问题描述】:

虽然我对 tex.stackexchange 非常熟悉,但我还是个 C 编程新手,也是 stackoverflow 社区的新成员,但我遇到了语法问题。

我想知道如何在静态分配另一个维度的同时动态分配数组的一个维度。 我可以在this question 中找到答案。正如答案所说,我只需要声明一个指向数组的指针,而不是指针数组。问题是我想不出如何调用这个“混合分配”数组的函数(我有什么合适的方法来调用它吗?)作为参数,因为我的变量声明如下:

char (*strings)[maxlen];

其中 maxlen 是一个全局变量,用于通知每个字符串的长度。然后我动态分配它,所以我有N个字符串,每个字符串的长度为maxlen

strings = malloc(N*sizeof(char));

所以我想调用这个字符串数组的函数,但我想不出一种方法来声明它。我的第一枪是愚蠢地尝试一下

void func(char **, int);

然后调用

func(strings,N);

但这不起作用,因为我没有char ** 参数,而是char (*)[100](我的maxlen 是100)。所以我收到以下错误:

expected ‘char **’ but argument is of type ‘char (*)[100]’

当然。

好吧,如果我选择动态分配两个维度,我的问题可能会得到解决,但这不是我的意图。

我如何声明一个函数,让我的变量可以毫无问题地通过它?

提前致谢。

编辑:maxlen 是一个,在编译时就知道了。

我的最小示例代码(尚未测试):

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

 #define maxlen 100


 void func(char (?), int N);

 int main()
 {

    char (*strings)[maxlen];
    int i, N;

    scanf("%d", &N);
    getchar();
    strings = malloc(N*sizeof(char));
    for(i=0;i<N;i++)
    {
       fgets(strings[i],sizeof(strings[i]),stdin);
    }

    func(strings,N);

    return 0;

 }

 void func(char (?), int N) ...

【问题讨论】:

  • char (*strings)[maxlen]; 是一个名为 strings 的数组,它具有 maxlen 元素,其中每个元素都是指向返回 char 的函数的指针。这可能不是您要查找的内容。试试char *strings[maxlen]; strings = malloc(N*sizeof(char));
  • error: assignment to expression with array type strings = malloc(N*sizeof(char)); 通过声明char *strings[maxlen];,我将strings 作为一个数组,大小为maxlen 指向char 的指针,而char (*strings)[maxlen];strings 作为指向一个数组大小 maxlen 的字符。
  • 这是最简单的,如果你这样做:char **string = malloc(N*sizeof(char*));。这相当于char *string[N]。现在你可以像这样传递它:func(string, N);.
  • 在编译时是否知道maxlen
  • 这将改善问题以显示您用于分配和填充数组的真实代码

标签: c arrays string function pointers


【解决方案1】:

您的问题不清楚maxlen 在编译时是否已知。以下代码适用于这两种情况。如果在编译时已知,则无需将其作为参数传递给func

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

void func( int n_strings, int maxlen, char (*ps)[maxlen] )
{
    for (int i = 0; i < n_strings; ++i)
        printf("%s\n", ps[i]);
}

int main()
{
// automatic allocation
    char arr[4][7] = { "Red", "Blue", "Green", "Yellow" };
    func(4, 7, arr);

// dynamic allocation
    int maxlen = 7;
    char (*arr2)[maxlen] = calloc(4, maxlen);
    strcpy(arr2[0], "Red");
    strcpy(arr2[1], "Blue");
    strcpy(arr2[2], "Green");
    strcpy(arr2[3], "Yellow");
    func(4, 7, arr2);
}

【讨论】:

  • 尖锐的回答。谢谢。
【解决方案2】:

您需要更改 malloc 的参数。

char (*strings)[maxlen];

strings = (char(*)[maxlen])malloc(N*sizeof(char[maxlen]));

( char(*)[maxlen] ) 在 malloc 之前告诉你变量的类型是什么

N*sizeof(char[maxlen]) 在 malloc 中,因为 sizeof(char[maxlen]) 是一个字符串所需的大小

如何:

定义函数void func(char (*string)[maxlen]){.....}

获取数组字符串中字符串的地址strings+i

访问字符串strings[i] where 0

记住

&amp;strings[i]strings+i 一样 --> 是一个字符串的地址

strings[i]*(strings+i) 相同--> 是具体字符串

【讨论】:

  • 演员阵容丑陋且不必要。
【解决方案3】:

试试这个:

void func(char *[100], int);

【讨论】:

    猜你喜欢
    • 2011-06-02
    • 2023-04-04
    • 2021-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-26
    相关资源
    最近更新 更多