使用C传递参考数组 [英] Pass array by reference using C

查看:247
本文介绍了使用C传递参考数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是的,我读过这个问题&答案:在c中通过引用传递数组。我有一个类似的问题,并从该问题实现相同的想法。

Yes, I've read this question & answers: pass array by reference in c. I have a similar problem and implemented the same idea from that question.

但是,我仍然得到以下代码的错误:

However, I still get an error from the following code:

#include <iostream>

void FillArray(int** myArray)
{
   free( *myArray ) ;
   * myArray = (int*) malloc(sizeof(int) * 2);

   *myArray[0] = 1;
   *myArray[1] = 2;
}

int main()
{
   int* myArray = NULL;
   FillArray(& myArray);    
   return 0;
}



我在FillArray函数结束后得到以下运行时错误: / p>

I got the following run-time error right after the FillArray function ends:


未处理的异常0x773115de在Program.exe 0xC00000005:访问冲突写入位置0xcccccccccc。

Unhandled exception at 0x773115de in Program.exe 0xC00000005: Access violation writing location 0xcccccccccc.

我使用Visual Studio,打开Visual C ++空项目。和文件名为main.cpp。这是否意味着它是用C ++编译器而不是C编译器编译?如果是这样,我如何打开一个文件,将只编译C编译器?我试着用main.c重命名main.cpp,但仍然有同样的问题。 (我问这个问题,因为我读了一些关于通过引用,并看到它是不同的C和C ++。)

I'm using Visual Studio, opened Visual C++ Empty Project. And the file named main.cpp. Does that mean it's compiled with C++ compiler instead of C compiler? If so, how can I open a file which will be compiled only C compiler? I tried renaming main.cpp with main.c, but still have the same problem. (I'm asking this question, because I read some about "pass by reference" and saw that it's different in C and C++.)

对于这个非常根本的问题

Sorry for this very fundamental question.

我将非常感谢任何帮助,

I will be appreciated for any help,

Sait。

推荐答案

优先级不能像您期望的那样工作。请尝试以下操作:

Precedence is not working as you expect it to be. Try these:

(*myArray)[0] = 1;
(*myArray)[1] = 2;

(注意: iostream 它是C ++。)

(Note: iostream is not C. It's C++.)

这篇关于使用C传递参考数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆