Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 7503

C/C++ • Function with pointer example code not compiling

$
0
0
Hi.
I am following the examples in the book Learn to Code with C https://magazine.raspberrypi.com/articles/learn-code-c and I got the example codes to work until I got to page 39. I am using a RPi 4 with Geany.

#include <stdio.h>
int sum_and_diff (int a, int b, int *res)
{
int sum;
sum = a + b;
*res = a – b;
return sum;
}
int main (void)
{
int b = 2;
int diff;
printf ("The sum of 5 and %d is %d\n", b, sum_and_diff (5, b, &diff));
printf ("The difference of 5 and %d is %d\n", b, diff);
}

Image

I understand that the pointer is the address in memory for the integer variable but I'm not grasping the * & syntax. There is a short ERRATA online for this book but it does not cover this problem.
Any suggestion on how to solve the problem is appreciated so I can understand more.

Statistics: Posted by BjornK — Thu Feb 05, 2026 1:30 pm — Replies 3 — Views 62



Viewing all articles
Browse latest Browse all 7503

Trending Articles