使用C中的指针通过引用传递和更改数组

使用C中的指针通过引用传递和更改数组,c,arrays,pointers,C,Arrays,Pointers,我正在为大学做一个项目,我已经在这一部分工作了一段时间,似乎找不到答案。基本上,我们必须制作一个程序,使用pass-by-reference填充数组,而不使用下标(只是为了让人恼火,因为他认为它们执行起来更快)。 到目前为止,我得到的是: 这些主要是相关部分: #define SIZE 4 int *enteredCode; enteredCode = (int*)calloc(SIZE, sizeof(int)); codeEnter(&enteredCode); 这在头文件中:

我正在为大学做一个项目,我已经在这一部分工作了一段时间,似乎找不到答案。基本上,我们必须制作一个程序,使用pass-by-reference填充数组,而不使用下标(只是为了让人恼火,因为他认为它们执行起来更快)。 到目前为止,我得到的是:
这些主要是相关部分:

#define SIZE 4

int *enteredCode;
enteredCode = (int*)calloc(SIZE, sizeof(int));
codeEnter(&enteredCode);
这在头文件中:

//codeEnter function
void codeEnter(int **code){
//Declarations

system("cls");
puts("************* INPUT CODE *************"
    "\n\nPlease enter your 4 digit code:");
for (int i = 0; i < SIZE; i++){
    scanf("%d", *(code + i));
}//End of for
标题部分:

void codeEnter(int *code){
//Declarations

system("cls");
puts("************* INPUT CODE *************"
    "\n\nPlease enter your 4 digit code:");
for (int i = 0; i < SIZE; i++){
    scanf_s("%d", &*(code + i));
}//End of for

}//End of codeEnter
void代码输入(int*code){
//声明
系统(“cls”);
puts(“*************输入代码*****************”
“\n\n请输入您的4位代码:”;
对于(int i=0;i

任何帮助和解释都将不胜感激

主要问题是如何在
codenter
函数中取消对数组的引用

您正在传递一个指向
int
数组的指针,需要获取数组第i个元素的地址

所以

void代码输入(int**code){
int*array=*code;//
建议如下:
#定义大小4
int*输入代码:
if(NULL==(enteredCode=calloc(SIZE,sizeof(int)))
{//然后calloc失败了
perror(“calloc失败”);
退出(退出失败);
}
//否则,calloc成功
代码输入(输入代码);
---
这在头文件中:
//codeEnter函数声明
无效代码输入(整数*代码);
//在源文件中显示以下内容:
无效代码输入(整数*代码)
{  
系统(“cls”);
puts(“*************输入代码*****************”
“\n\n请输入您的4位代码:”;
对于(int i=0;i
1)调用任何malloc系列时,始终检查返回值(!=NULL)以确保操作成功。2)在C中,不要从任何malloc系列强制转换返回值。当实现是指向(在本例中)的指针数组时,这种类型的行:“codeEnter(&enteredCode);”非常有用整数。但是,这不是正在实现的。建议从:codenter(enteredCode);''开始
void codeEnter(int *code){
//Declarations

system("cls");
puts("************* INPUT CODE *************"
    "\n\nPlease enter your 4 digit code:");
for (int i = 0; i < SIZE; i++){
    scanf_s("%d", &*(code + i));
}//End of for

}//End of codeEnter
void codeEnter(int **code) {
  int* array = *code; // <- you obtain the original array
  int* element = (array+i); // <- you obtain the address of the element
  ...
}
suggest something like this:

#define SIZE 4

int *enteredCode:
if( NULL == (enteredCode = calloc(SIZE, sizeof(int)) ) )
{ // then calloc failed
    perror( "calloc failed" );
    exit( EXIT_FAILURE );
}

// implied else, calloc successful

codeEnter(enteredCode);

---

And this is in a header file:

//codeEnter function declaration
void codeEnter(int *code);

// this in the source file:

void codeEnter(int *code)
{  
     system("cls");
     puts("************* INPUT CODE *************"
          "\n\nPlease enter your 4 digit code:");

    for (int i = 0; i < SIZE; i++)
    {
        if( 1 != scanf("%d", (code + i)) )
        { // then, scanf failed
            perror( "scanf failed" );
            free(code);
            exit( EXIT_FAILURE );
        }

        // implied else, scanf successful

    }//End for

    // rest of function here

} // end function: enterCode