C 传入函数后给出随机值的指针 #包括 #包括 无效交易查询(int*arr、int limit、int numquerys、int filled){ //在这里编写代码 对于(int i=0;i

C 传入函数后给出随机值的指针 #包括 #包括 无效交易查询(int*arr、int limit、int numquerys、int filled){ //在这里编写代码 对于(int i=0;i,c,function,pointers,C,Function,Pointers,变量array是read\u input的本地变量,因此当从函数中重试并在此之后访问它时,它的生命终止是非法的 相反,您应该在堆上分配一个数组并使用它 #include<stdio.h> #include <stdlib.h> void deal_queries(int *arr, int limit, int numQueries, int filled) { // Write your code here for(int i=0;i<fille

变量
array
read\u input
的本地变量,因此当从函数中重试并在此之后访问它时,它的生命终止是非法的

相反,您应该在堆上分配一个数组并使用它

#include<stdio.h>
#include <stdlib.h>
void  deal_queries(int *arr, int limit, int numQueries, int filled) {
    // Write your code here
    for(int i=0;i<filled;i++)
    {
        printf("%d ",*(arr+0));
    }
}
int * read_input(int N,int n){
    //declare dynamic array of size N
    // take input n integers and store them in the array and return pointer to this
    int i;
    int *ptr;
    int array[N];
    ptr=array;
    for(i=0;i<n;i++)
    {scanf("%d ",ptr);ptr++;}
    ptr=array;
    return (ptr);
}
int main()
{
    int N,Q,n; 
    scanf("%d %d %d",&N,&Q,&n);
    int* arr=read_input(N,n);
    printf("%d ",*(arr+0));
    deal_queries(arr,N,Q,n);
    return 0;
}
#包括
#包括
无效交易查询(int*arr、int limit、int numquerys、int filled){
//在这里编写代码

对于(int i=0;i您返回指向本地自动存储变量的指针,该变量为UB,因为当函数返回时变量停止存在

#include<stdio.h>
#include <stdlib.h>
void  deal_queries(int *arr, int limit, int numQueries, int filled) {
    // Write your code here
    for(int i=0;i<filled;i++)
    {
        printf("%d ",*(arr+0));
    }
}
int * read_input(int N,int n){
    //declare dynamic array of size N
    // take input n integers and store them in the array and return pointer to this
    int i;
    int *ptr;
    int *array=malloc(sizeof(*array)*N); /* allocate an array on the heap */
    if(array==NULL) return NULL; /* check if allocation succeeded */
    ptr=array;
    for(i=0;i<n;i++)
    {scanf("%d ",ptr);ptr++;}
    ptr=array;
    return (ptr);
}
int main()
{
    int N,Q,n; 
    scanf("%d %d %d",&N,&Q,&n);
    int* arr=read_input(N,n);
    if(arr==NULL) return 1; /* check if allocation succeeded */
    printf("%d ",*(arr+0));
    deal_queries(arr,N,Q,n);
    free(arr); /* free allocated array */
    return 0;
}
或者更好

static int array[N];
ptr=array;

错误出现在
read\u input
中,您有:

//int array[N];
ptr=malloc(sizeof(*ptr)*N);

变量
数组
是函数中的一个局部变量。对
ptr
的赋值使其指向此局部数组的第一个元素。一旦函数返回且
数组
的生存期结束,此指针将变为无效

您需要使用
malloc
动态分配阵列:

int array[N];
ptr=array;
return (ptr);

指针不是问题,而是它指向的对象。问题在于:

int *array = malloc(N * sizeof *array);

int *ptr = array;
// Fill array using the ptr variable...

return array;
为了完成您想做的事情,您需要使用
malloc

int * read_input(int N,int n) {
    ...
    int array[N]; // This array is in automatic memory,
    ptr=array;    // so it is valid only inside read_input
    ...
    return (ptr); // you are returning a pointer to it
}
在循环中使用另一个指针,或对
ptr
应用索引,如下所示:

int* ptr = malloc(sizeof(int)*N);
(i=0;i{ scanf(“%d”和&ptr[i]); }
也许您想要
printf(“%d”、*(arr+i));
而不是
printf(“%d”、*(arr+0))在函数
deal\u querys
中,请不要在
scanf
格式中使用尾随空格。这可能导致
scanf
手动等待一个非空格字符,因为它知道空格何时结束。请参阅。
sizeof(int)
错误易发的做法。在typedef-s中使用对象而不是类型。但我对调用read\u input函数call ends有一个疑问,在这之后,如果我尝试打印arr指针main中的arr值,它会给出正确的值,这意味着arr指向read\u input中数组的地址
int* ptr = malloc(sizeof(int)*N);
for (i = 0 ; i < n ; i++) {
    scanf("%d ", &ptr[i]);
}