c - 从 C 中的函数传递和返回 2 个数组

标签 c arrays function pointers

我在 main 函数中有 2 个数组,一个 double 类型和一个整数类型。我想写一个函数,我们称它为“func1”,我想向它发送 2 个数组。我打算对其他两种类型做一些操作(如果需要我可以给出操作的细节),然后将这两个函数返回给我的主函数。

我明白不可能一次返回两个对象,所以我认为我必须传递和返回指针。

我试过的是:

int main (void){
...
int str2[]=...
double str3[]=...
func1 (&str2, &str3) /* Thş was the best suggestion I could find on the internet */
...
}

void func1 (int *intType, double *doubleType){
...
intType[33]=27; /* just for example */
...
return;
}

结果,我收到了很多警告,如果我运行我的程序就会崩溃。我在上面遇到了什么问题,我该如何解决?

附言我真的不知道我做错了什么而且我不是 C 的高手,看来我在传递指针方面有严重的问题所以请不要生我的气。

谢谢!

最佳答案

离你不远了

void func1 (int *intType, double *doubleType); // note: prototype

int main (void)
{
    int str2[100];
    double str3[100];
    func1 (str2, str3); // note: no `&` on the parameters here
    return 0;
}

void func1 (int *intType, double *doubleType)
{
    intType[33] = 27;
    doubleType[42] = 1.0;
}

关于c - 从 C 中的函数传递和返回 2 个数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20710513/

相关文章:

python - Pandas fillna with method=None (默认值)会引发错误

function - Matlab GUI 重用功能 block

c - 通过 FILE 流读取和写入原始内存

c - C/Cython 中的动态类型转换

c - 为什么用C修改二进制文件的double是错误的?

arrays - bash - 数组扩展和函数调用

java - 从内部存储读取字符串数组

C: Malloc 字符串数组混淆

.net - 将 UnmanagedMemoryStream 转换为字节数组

function - 对常量值使用def vs. val有什么含义