The c language pointer outputs the maximum and minimum values of two numbers and sums them up. It is not clear why it is not possible to write this way.

I saw a post online, but I couldn't figure out the reason.

The c language pointer outputs the maximum and minimum values ​​of two numbers and sums them up. It is not clear why it is not possible to write this way.

#include<stdio.h>
int main()
{     int *p,*q,*s,a,b;     s=NULL;p=&a;q=&b;     scanf("%d%d",p,q );     if(*p<*q)     {         *s=*q;         *q=*p;         *p=*s;     }     printf("The maximum value is: %d, the minimum value is: %d, and the sum is% d",*p,*q,*p+*q);     return 0; }











The code cannot pass because of this sentence: s=NULL;

Just remove this sentence. I didn't even know the reason.

One big brother answered like this.

s is initialized to a null pointer, and s is used in the subsequent exchange assignment. At this time, s is a null pointer, and the assignment operation does not know where to put the data.

 

Guess you like

Origin blog.csdn.net/digitalkee/article/details/112735198
Recommended