Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 在函数调用的参数内定义新函数、数组、结构等_C_Arrays_Function_Pointers_Compound Literals_Java - Fatal编程技术网

C 在函数调用的参数内定义新函数、数组、结构等

C 在函数调用的参数内定义新函数、数组、结构等,c,arrays,function,pointers,compound-literals,java,C,Arrays,Function,Pointers,Compound Literals,Java,如果您的函数具有以下功能: void foo(char **arr); void foo(char* x[] = { "hello", "my", "friend" }); public void foo(String[] x); foo(new String[] { "hello", "my", "friend" }); void foo(char* x[] = { "hello", "my", "friend" }); 如何做到以下几点: void foo(char **arr);

如果您的函数具有以下功能:

void foo(char **arr);
void foo(char* x[] = { "hello", "my", "friend" });
public void foo(String[] x);

foo(new String[] { "hello", "my", "friend" });
void foo(char* x[] = { "hello", "my", "friend" });
如何做到以下几点:

void foo(char **arr);
void foo(char* x[] = { "hello", "my", "friend" });
public void foo(String[] x);

foo(new String[] { "hello", "my", "friend" });
void foo(char* x[] = { "hello", "my", "friend" });
如果这让您感到困惑,那么在Java中,我们可以通过以下方式实现:

void foo(char **arr);
void foo(char* x[] = { "hello", "my", "friend" });
public void foo(String[] x);

foo(new String[] { "hello", "my", "friend" });
void foo(char* x[] = { "hello", "my", "friend" });
目前,我用C语言做了以下我讨厌的事情,因为它看起来很难看:

char* myArr[] = 
    { 
        "hello", "my", "friend"
    };

foo(myArr);

你一定是另一个受害者的人开始,然后试图进入,这就是为什么我会回答

我想在该参数内初始化我的数组。如何做到这一点

您不能。在C99之前


一般来说,这是您可以做的:

#include <stdio.h>

void foo(char** arr, int size)
{
    int i;
    for(i = 0; i < size; ++i)
        printf("%s\n", arr[i]);
}

void bar(char** arr)
{
    while(*arr)
        printf("%s\n", *arr++);
}

int main(void)
{
    char* x[] = { "hello", "my", "friend" };
    foo(x, 3);

    char* null_terminated[] = { "ShadowRanger", "Correct", NULL };
    bar(null_terminated);

    return 0;
}
#包括
void foo(字符**arr,整数大小)
{
int i;
对于(i=0;i

其中
foo()
显式使用数组的大小,而
bar()
要求数组以NULL结尾。

Java和C是不同的语言,具有不同的习惯用法

如果是我,我会尽量避免强迫C变成“类似Java的”。接受每种语言各自的优点

对于第一个“丑陋”的例子,可以使用CPP[C预处理器]宏——这是Java中不存在的概念:

#define FOO(_av...) \
    do { \
        char *myArr[] = { _av, NULL }; \
        foo(myArr); \
    } while (0)

FOO("hello", "my", "friend");

但是,这可能会被很多人认为是“太可爱了”。最好创建某种类型的表

每当Java执行
new
时,它都在执行堆分配[这很慢]。这在一定程度上是因为所有东西或多或少都必须“在堆上”

C可以使用
malloc
,但是一个好的C程序员会尽量避免不必要的堆分配,因为它有全局变量、静态变量和函数作用域变量

如何做到以下几点:

void foo(char **arr);
void foo(char* x[] = { "hello", "my", "friend" });
public void foo(String[] x);

foo(new String[] { "hello", "my", "friend" });
void foo(char* x[] = { "hello", "my", "friend" });
你差点就成功了…;-)

如果执行C99或更新版本,请使用如下复合文字:

foo((char *[]){"hello", "my", "friend"});
请注意,被调用的函数(
foo()
此处)不知道指针数组有多少个元素,因此您希望添加一个最终的空指针作为sentinel:

foo((char *[]){"hello", "my", "friend", NULL});
例如:

#include <stdio.h>
#include <stdlib.h> /* for EXIT_xxx macros */


void foo(char **arr)
{
  while (arr && *arr)
  {
    printf("%s\n", *arr);
    ++arr;
  }
}

int main(void)
{
  foo((char *[]){"hello", "my", "friend", NULL}); /* Mind the final NULL. */

  return EXIT_SUCCESS;
}
复合文字在其定义的范围离开之前是有效的(
main()
here)。如果要确保在使用后立即将其从堆栈中删除,请在调用
foo()
创建本地作用域/块时使用大括号:

int main(void)
{
  {
    foo((char *[]){"hello", "my", "friend", NULL}); /* Mind the final NULL. */
  }

  /* The compound literal passed to foo() is already deallocated here, had been 
     removed from the stack. */

  ...

对不起,你好像没读我的问题。查看您在何处键入的
foo(x,3)
?我想在该参数内初始化我的数组。“这怎么可能呢?”哈特芬德说。不能。传递长度的替代方法是在
char*
数组的末尾添加一个
NULL
sentinel,这样
foo
可以继续,直到
arr[i]
NULL
。显式长度通常更好,但哨兵偶尔也有用。我也不能在参数中声明函数?@ShadowRanger updated,你怎么看?好主意!HateFind,不,但是您可以传递函数指针,正如您已经知道的…;)我建议你在新问题中提出第二个完全无关的问题。小贴士:先做些调查…:)简单回答:C不是Java。有些事情在C中不能像在Java中那样完成。这是其中之一。你会发现C程序员会感激用Java的方式做不到这一点。”但是,这可能会被很多人认为是“太可爱了”P:P:P不管怎样,幸好我们有一个替代我的答案,+1:)