Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
85 views
in Technique[技术] by (71.8m points)

c - After a pointer Returns from a function i cant print it

I am relatively new to C. My program is supposed to fill in the array with random numbers and i have to find the max and min using 1 function. The program works fine up until the point i have to return the values my 2 pointers get from the function. When i go to print them the porgram stop working and exits with the return value of 3221225477. I have been trying to fix this for 3 hours and i am going INSANE. Please help in any way you can i would really apreciate it.

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

void MaxMin(int size, int *B, int *Max, int *Min);

int main(int argc, char *argv[])
{
    int N, i,*A,*MAX,*MIN;
    srand(time(NULL));
    
    
    /*Making sure the user enters a proper value for the array*/
    do
    {
        printf("Give the number of spaces in the Array
");
        scanf("%d",&N);
    }
    while(N<1);
    
    A = (int *) malloc(N*(sizeof(N)));
    
    
    
    /*Giving random numbers to the array and printing them so i can make sure my code is finding the max min*/
    for(i=0;i<N;i++)
    {
        A[i]=rand()%100;
        printf("
%d
",A[i]);
    }
    
    
    
    
    /*Calling my void function so that the pointers MAX and MIN have a value assigned to them */
    MaxMin(N, A, MAX, MIN);
    
    
    /*Printing them*/
    printf("
Max = %d
Min = %d",*MAX,*MIN);
    free(A);
    return 0;
}



/*The function*/
void MaxMin(int size, int *B, int *Max, int *Min)
{
    /*using 2 temporary ints to get max min cause pointers and arrays confuse me*/
    int max=B[0],min=B[0],i;
    for(i=1;i<size;i++)
    {
        if(max<B[i])
        {
            max = B[i];
        }
        if(min>B[i])
        {
            min = B[i];
        }
    }
    
    /*These have the proper value last i chekced */
    Max = &max;
    Min = &min;
}

(edit) SOLUTION Ty so much for the help !

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

void MaxMin(int size, int *B, int *Max, int *Min);

int main(int argc, char *argv[])
{
    int N, i,*A,MAX ,MIN ;
    srand(time(NULL));
    
    
    /*Making sure the user enters a proper value for the array*/
    do
    {
        printf("Give the number of spaces in the Array
");
        scanf("%d",&N);
    }
    while(N<1);
    
    A = (int *) malloc(N*(sizeof(int)));
    
    
    
    /*Giving random numbers to the array and printing them so i can make sure my code is finding the max min*/
    for(i=0;i<N;i++)
    {
        A[i]=rand()%100;
        printf("
%d
",A[i]);
    }
    
    
    
    
    /*Calling my void function so that the pointers MAX and MIN have a value assigned to them */
    MaxMin(N, A, &MAX, &MIN);
    
    
    /*Printing them*/
    printf("
Max = %d
Min = %d",MAX,MIN);
    free(A);
    return 0;
}



/*The function*/
void MaxMin(int size, int *B, int *Max, int *Min)
{
    *Max=B[0];
    *Min=B[0];
    int i;
    for(i=1;i<size;i++)
    {
        if(*Max<B[i])
        {
            *Max = B[i];
        }
        if(*Min>B[i])
        {
            *Min = B[i];
        }
    }
}
question from:https://stackoverflow.com/questions/65852030/after-a-pointer-returns-from-a-function-i-cant-print-it

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You passed to the function MaxMin pointers MAX and MIN by value. That is the function deals with copies of (indeterminate) values of the passed pointers. Changing the copies does not influence on the original arguments.

Within main you should declare MIN and MAX as objects of the type int.

int N, i,*A, MAX, MIN;

and call the function ,like

MaxMin(N, A, &MAX, &MIN);

Within the function you should write

*Max = &max;
*Min = &min;

And at last in main you should call printf like

printf("
Max = %d
Min = %d", MAX, MIN);

Pay attention to that the expression sizeof( N ) used in this statement

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

is error prone. The type of the variable N can be changed for example from the type int to the type size_t. In this case the size of the allocated memory will be incorrect, You should write for example

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...