Use of pointers 1
C program from books:
#include <stdio.h>
int *addition ( int a , int b) {
int c = a + b;
int *d = &c;
printf("%d %d, ",c, &c);
printf("%d %d, ",d, *d);
return d ;
}
int main (void) {
int result = *(addition(1,2));
int *result_ptr = addition(1,2);
////
printf("result_ptr = %d\n" , *result_ptr ) ;
printf("result = %d\n" ,result) ;
return 0 ;
}
I am writing a simple program to practise the use of pointers. I use the
function addition() to calculate the sum of two integers. Instead of
returning the value, the function returns a pointer to the result. The
program correctly outputs the expected result. However, when I swaps last
two Line (15 and 16, the program becomes buggy.
Question.
(a) What's wrong with the program??
(b) addition() should return a pointer??
I fix it by change the *result_ptr to result_ptr
No comments:
Post a Comment