C 复制数组并修改其元素的函数

C 复制数组并修改其元素的函数,c,arrays,C,Arrays,我被要求定义一个包含三个参数的函数数组\u ncopy: int*ptr表示指向整数数组的指针 int array表示数组的大小 int n表示一个整数 将*ptr指向的数组元素复制到另一个列表中。但是,索引大于NUMBER的所有元素都将被复制为0 这是我的代码: 我应该得到这样的结果: 对于数字=4: Value of array[0] is 83 Value of array[1] is 86 Value of array[2] is 77 Value of array[3] is 15

我被要求定义一个包含三个参数的函数数组\u ncopy:

int*ptr表示指向整数数组的指针

int array表示数组的大小

int n表示一个整数 将*ptr指向的数组元素复制到另一个列表中。但是,索引大于NUMBER的所有元素都将被复制为0

这是我的代码: 我应该得到这样的结果: 对于数字=4:

Value of array[0] is 83
Value of array[1] is 86
Value of array[2] is 77
Value of array[3] is 15
对于数字=12:

Value of array[0] is 83
Value of array[1] is 86
Value of array[2] is 77
Value of array[3] is 15
Value of array[4] is 93
Value of array[5] is 35
Value of array[6] is 86
Value of array[7] is 92
Value of array[8] is 0
Value of array[9] is 0
Value of array[10] is 0
Value of array[11] is 0
相反,我得到以下输出: 对于数字=12

Value of array[0] is 83
Value of array[1] is 86
Value of array[2] is 77
Value of array[3] is 15
Value of array[4] is 93
Value of array[5] is 35
Value of array[6] is 86
Value of array[7] is 92
Value of array[8] is 0
Value of array[9] is 0
Value of array[10] is 4113
Value of array[11] is 0
是否有人可以更正我的代码,使其输出正确的内容。

试试这个

int* array_ncopy(int *ptr, int array, int n) {
    for (int i=0; i < array; i++){
        if (i >= n) {
            ptr[i] = 0;
        }
        ptr[i] = ptr[i];
    }
    return ptr;
}

祝你好运这就是编译器警告的作用:

++ clang -Wall -Wextra -std=c11 -pedantic-errors prg.c
prg.c:23:32: warning: unused parameter 'array' [-Wunused-parameter]
int* array_ncopy(int *ptr, int array, int n) {
                               ^
1 warning generated.
您根本没有使用变量数组。做:

for (int i=0; i < array; i++){

因为有一些非常令人困惑的事情,有时甚至是错误的事情,所以我会尝试一个接一个地把它们讲清楚

#define NUMBER 12 
#define MAX  100
#define SIZE 8
这里数字>大小;这意味着您的数组永远不会达到上限

    int* array_ncopy(int *ptr, int array, int n)
 {
    for (int i = 0; i < n; i++) 
        if (i >= n) {
            ptr[i] = 0;
        }
        ptr[i] = ptr[i];
    }
    return ptr;
}
除此之外:

在几乎所有的函数声明中,您的命名方案都很不清楚。名称很好,但变量名可能更具描述性。 乍一看,很难真正知道int*array\u ncopyint*ptr,int-array,int-n-arrays是什么意思,这里是int而不是ptr和或n的意思。所以我建议将它改为array_ncopyint*ptr,int-length,int-upperBound

在下面,您可以找到您的程序以更惯用的方式运行


根据规范,复制功能应为:

int* array_ncopy(int *ptr, int array, int number)
{
    int *new_array= malloc(array*sizeof(int));
    for (int i=0; i < array; i++){
        if (i >= number) {
            new_array[i] = 0;
        }
        else new_array[i] = ptr[i];
    }
    return new_array;
}
这会将元素复制到指定的新数组中。数组大小与隐式指定的旧数组大小相同。索引为=array的旧数组中的所有元素,则没有任何元素被隐式指定为零。

另一种方法:

int *array_ncopy(int *ptr, unsigned int size, unsigned int number)
{
    // create a zero-filled array the same size
    int *newArray = calloc( size, sizeof( *newArray ) );

    // error checking - ALWAYS do error checking!
    if ( !newArray )
    {
        return( NULL );
    }

    // copy the first "number" elements to the new array
    // making sure to limit the max amount copied
    number = MIN( number, size );
    memcpy( newArray, ptr, number * sizeof( *newArray ) );
    return( newArray );
}

此外,使用带符号的int表示数组中的元素数是毫无意义的,可能会导致一些奇怪的错误。

对于int i=0;i对于int i=0;i<阵列;i++。顺便说一句,ptr[i]=ptr[i];是一个no-op.ptr[i]=ptr[i]并不真正复制任何东西。数组\u ncopy返回的指针是什么?我建议您对代码做一些修改。如何使其复制指向列表的元素?array\u ncopy返回的指针应该是新修改的列表?您可能希望在array\u ncopy函数中的某个位置有一个malloc creast函数必须创建一个MAXSIZE、NUMBER的数组。目前没有。ptr[i]=ptr[i];是无意义的,并覆盖ptr[i]=0;。你的代码完全有道理,但是我得到了相同的输出?为什么打印的是4113而不是数组[10]的0值4113@MisterTusk-因为您的测试用例传递的数值超过了数组,所以返回的元素都不会与传递的元素不同。换句话说,对于任何元素,都不会执行new_array[i]=0。如果您想要一个可见的更改,请按相反的顺序传递这两个参数。
    int* array_ncopy(int *ptr, int length, int upperBound) // ptr = first array
    {    
int *cpy = (int*)malloc(length * sizeof(int)); // create copy array
        for (int i = 0; i < length; i++)
        {
            if (i >= upperBound) 
                cpy[i] = 0; 
            else
            cpy[i] = ptr[i]; // copy to array if i < upperBound
        }
        return cpy;
    }
#include <stdio.h>
    #include <stdlib.h>
    #define UPPER_BOUND 8
    #define MAX  100
    #define SIZE  12

void array_print(int *ptr, int length) {
    for (int i = 0; i < length; i++) {
        printf("Value of array[%d] is %d", i, ptr[i]);
        printf("\n");
    }
}

int* array_create(int length) {
    int *t = (int*)malloc(length * sizeof(int));
    for (int i = 0; i < length; i++) {
        t[i] = rand() % MAX;
    }

    return t;
}

    int* array_ncopy(int *ptr, int length, int upperBound)
    {    
int *cpy = (int*)malloc(length * sizeof(int));
        for (int i = 0; i < length; i++)
        {
            if (i >= upperBound)
                cpy[i] = 0;
            else
            cpy[i] = ptr[i];
        }
        return cpy;
    }

    int main()
 {
    int *t = array_create(SIZE);
    int *cpy = array_ncopy(t, SIZE, UPPER_BOUND);
    array_print(cpy, SIZE);
    free(t);
    free(cpy);
    return 0;
}
int* array_ncopy(int *ptr, int array, int number)
{
    int *new_array= malloc(array*sizeof(int));
    for (int i=0; i < array; i++){
        if (i >= number) {
            new_array[i] = 0;
        }
        else new_array[i] = ptr[i];
    }
    return new_array;
}
int *array_ncopy(int *ptr, unsigned int size, unsigned int number)
{
    // create a zero-filled array the same size
    int *newArray = calloc( size, sizeof( *newArray ) );

    // error checking - ALWAYS do error checking!
    if ( !newArray )
    {
        return( NULL );
    }

    // copy the first "number" elements to the new array
    // making sure to limit the max amount copied
    number = MIN( number, size );
    memcpy( newArray, ptr, number * sizeof( *newArray ) );
    return( newArray );
}