如何将这些数字存储在C中?

如何将这些数字存储在C中?,c,arrays,C,Arrays,假设您有一个这样的数组 { 1 2 5 7 2 3 7 4 2 1 } int *c = getPositions( array, size ); if ( c != NULL ) for( int i = 0; i < (size/2)); i++ ) printf( "%d\t", c[i] ); free( c ); 您想存储数组的前半部分和后半部分之间的差值在位置2和4 诀窍是,我以后需要在其他代码中使用这些存储的数字,所以我不知道如何存储这些数字

假设您有一个这样的数组

{ 1 2 5 7 2 3 7 4 2 1 }
int *c = getPositions( array, size );

if ( c != NULL ) 
    for( int i = 0; i < (size/2)); i++ )
        printf( "%d\t", c[i] );

free( c );
您想存储数组的前半部分和后半部分之间的差值在位置2和4

诀窍是,我以后需要在其他代码中使用这些存储的数字,所以我不知道如何存储这些数字

我有这个方法

int * getPositions(int *array, int size){
    int * c[(size/2)];
    int counter = 0;
    for(int i = 0; i < size /2; i++) {
        if (*(array + i) != *(array + (size - 1) - i)) {
            c[counter]= (int *) i;
            counter++;
        }
    }return (int *) c;
 }
打印出来的只是

-1774298560 -1774298560 -1774298560 -1774298560 -1774298560
PS:我在其他地方初始化了
数组
数组的大小

PS:我已经考虑了这些评论,并将代码更改为以下内容

int * getPositions(int *array, int size){
    int * c = (int *) malloc((size_t) (size/2));
    int counter = 0;
    for(int i = 0; i < size /2; i++) {
        if (*(array + i) != *(array + (size - 1) - i)) {
            c[counter]= i;
            counter++;
        }
}
int*getPositions(int*array,int size){
int*c=(int*)malloc((size_t)(size/2));
int计数器=0;
对于(int i=0;i
如果函数应该返回一个简单的
int
数组,则需要声明一个指向int的指针,然后调用
malloc
为数组保留空间。然后填充数组并返回指针。调用函数需要在某个点释放内存

int *getPositions(int *array, int size)
{
    int *c = malloc( (size/2) * sizeof(int) );
    if ( c == NULL )
        return NULL;

    // put stuff in the array using array syntax, e.g. 
    c[0] = array[0];

    return c;
}
像这样调用函数

{ 1 2 5 7 2 3 7 4 2 1 }
int *c = getPositions( array, size );

if ( c != NULL ) 
    for( int i = 0; i < (size/2)); i++ )
        printf( "%d\t", c[i] );

free( c );
int*c=getPositions(数组,大小);
如果(c!=NULL)
对于(int i=0;i<(size/2));i++)
printf(“%d\t”,c[i]);
免费(c);
注:

  • 是的,在C中检查错误是一件痛苦的事情,但是你必须这样做,否则你的 程序将随机崩溃
  • 允许对指针使用数组语法,只需确保读写操作不会超过指针引用的内存的末尾
  • NULL
    指针传递到
    free
    是合法的

如果函数应该返回一个简单的
int
数组,则需要声明一个指向int的指针,然后调用
malloc
为数组保留空间。然后填充数组并返回指针。调用函数需要在某个点释放内存

int *getPositions(int *array, int size)
{
    int *c = malloc( (size/2) * sizeof(int) );
    if ( c == NULL )
        return NULL;

    // put stuff in the array using array syntax, e.g. 
    c[0] = array[0];

    return c;
}
像这样调用函数

{ 1 2 5 7 2 3 7 4 2 1 }
int *c = getPositions( array, size );

if ( c != NULL ) 
    for( int i = 0; i < (size/2)); i++ )
        printf( "%d\t", c[i] );

free( c );
int*c=getPositions(数组,大小);
如果(c!=NULL)
对于(int i=0;i<(size/2));i++)
printf(“%d\t”,c[i]);
免费(c);
注:

  • 是的,在C中检查错误是一件痛苦的事情,但是你必须这样做,否则你的 程序将随机崩溃
  • 允许对指针使用数组语法,只需确保读写操作不会超过指针引用的内存的末尾
  • NULL
    指针传递到
    free
    是合法的

我的编译器真的疯了……你在用哪个编译器?任何现代静态分析器都应该警告你

aftnix@dev:~⟫ gcc -std=c11 -Wall st.c 
st.c: In function ‘getPositions’:
st.c:8:25: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
             c[counter]= (int *) i;
                         ^
st.c:11:6: warning: function returns address of local variable [-Wreturn-local-addr]
     }return (int *) c;
      ^
st.c: In function ‘main’:
st.c:16:13: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
     int c = (int) getPositions(array, 10);
因此,编译器正在显示代码的所有问题。而您编辑的代码甚至无法编译:

aftnix@dev:~⟫ gcc -std=c11 -Wall st.c 
st.c:4:4: error: expected identifier or ‘(’ before ‘[’ token
 int[] getPositions(int *array, int size){
    ^
st.c: In function ‘main’:
st.c:17:5: warning: implicit declaration of function ‘getPositions’ [-Wimplicit-function-declaration]
     int c = (int) getPositions(array, 10);
有几件事你必须考虑

  • 如果需要返回存储地址,请不要尝试返回局部变量。因为局部变量是在堆栈中分配的。堆栈帧具有与函数相同的生存期。使用
    malloc
    和co在堆中分配内存。了解如何分配和使用运行时数据结构对于
    C非常重要
    

  • 在传递数组/指针时,应首先考虑您的设计。是否要“就地”更改原始数组?还是要“转换”数组的其他副本。通常应尽量避免“就地”更改内存因为可能会有其他用户这样做。但是如果您不担心这些问题,那么您可以将您的函数设计为“输入->输出”基础。这意味着您将不会返回
    void
    ,而是返回转换后的数组本身

  • 我的编译器真的疯了…你在用哪个编译器?任何现代的静态分析器都应该警告你

    aftnix@dev:~⟫ gcc -std=c11 -Wall st.c 
    st.c: In function ‘getPositions’:
    st.c:8:25: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
                 c[counter]= (int *) i;
                             ^
    st.c:11:6: warning: function returns address of local variable [-Wreturn-local-addr]
         }return (int *) c;
          ^
    st.c: In function ‘main’:
    st.c:16:13: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
         int c = (int) getPositions(array, 10);
    
    因此,编译器正在显示代码的所有问题。而您编辑的代码甚至无法编译:

    aftnix@dev:~⟫ gcc -std=c11 -Wall st.c 
    st.c:4:4: error: expected identifier or ‘(’ before ‘[’ token
     int[] getPositions(int *array, int size){
        ^
    st.c: In function ‘main’:
    st.c:17:5: warning: implicit declaration of function ‘getPositions’ [-Wimplicit-function-declaration]
         int c = (int) getPositions(array, 10);
    
    有几件事你必须考虑

  • 如果需要返回存储地址,请不要尝试返回局部变量。因为局部变量是在堆栈中分配的。堆栈帧具有与函数相同的生存期。使用
    malloc
    和co在堆中分配内存。了解如何分配和使用运行时数据结构对于
    C非常重要
    

  • 在传递数组/指针时,应首先考虑您的设计。是否要“就地”更改原始数组?还是要“转换”数组的其他副本。通常应尽量避免“就地”更改内存因为可能会有其他用户这样做。但是如果您不担心这些问题,那么您可以将您的函数设计为“输入->输出”基础。这意味着您将不会返回
    void
    ,而是返回转换后的数组本身
  • 另一种选择

    int * getPositions(int *array, int size);
    int main() {
        int array[] = { 1, 2, 5, 7, 2, 3, 7, 4, 2, 1 };
    
        int size_of_array = sizeof(array) / sizeof(int);
        int *ptr = getPositions(array, size_of_array);
    
        for(int i = 0; ptr[i] != '\0' ; i++){
            printf("%d\t", *(ptr + i));
        }
    
        return 0;
    }
    int * getPositions(int *array, int size) {
        int temp[size/2];
        int counter = 0;
    
        for (int i = 0; i < size / 2 ; i++) {
            if (array[i] != array[(size - 1) - i]) {
                temp[counter++] = i;
            }
        }
        int *c = malloc(counter * sizeof(int));
        for (int i = 0; i < counter; i++) {
            c[i] = temp[i];
        }
    
        return  c;
    }
    
    int*getPositions(int*array,int size);
    int main(){
    int数组[]={1,2,5,7,2,3,7,4,2,1};
    数组的int size\u=sizeof(数组)/sizeof(int);
    int*ptr=getPositions(数组,数组的大小);
    对于(int i=0;ptr[i]!='\0';i++){
    printf(“%d\t”,*(ptr+i));
    }
    返回0;
    }
    int*getPositions(int*array,int size){
    内部温度[尺寸/2];
    int计数器=0;
    对于(int i=0;i
    另一个选项

    int * getPositions(int *array, int size);
    int main() {
        int array[] = { 1, 2, 5, 7, 2, 3, 7, 4, 2, 1 };
    
        int size_of_array = sizeof(array) / sizeof(int);
        int *ptr = getPositions(array, size_of_array);
    
        for(int i = 0; ptr[i] != '\0' ; i++){
            printf("%d\t", *(ptr + i));
        }
    
        return 0;
    }
    int * getPositions(int *array, int size) {
        int temp[size/2];
        int counter = 0;
    
        for (int i = 0; i < size / 2 ; i++) {
            if (array[i] != array[(size - 1) - i]) {
                temp[counter++] = i;
            }
        }
        int *c = malloc(counter * sizeof(int));
        for (int i = 0; i < counter; i++) {
            c[i] = temp[i];
        }
    
        return  c;
    }
    
    int*getPositions(int*array,int size);
    int main(){
    int数组[]={1,2,5,7,2,3,7,4,2,1};
    数组的int size\u=sizeof(数组)/sizeof(int);
    int*ptr=getPositions(数组,大小_