func.c
/**
* In-class work from 2/2.
*
* Passing by value, with pointers, and showing how status codes / perror work.
*/
#include <stdio.h>
#include <fcntl.h>
/* Admittedly, this cool thing is not that cool. */
int cool_thing(long int *a)
{
if (*a > 1000000) {
return -1;
}
/* I dub this starfest 2021. Look at all those *s */
*a = *a * *a;
int ret = cool_thing(a);
if (ret == -1) {
return -1;
}
return 0;
}
void set_three(long int *a)
{
/* Figure out what's at the memory location in a, set its value to 3 */
*a = 3;
}
void swap(long int *a, long int *b)
{
printf("a is: %p\n", a);
printf("b is: %p\n", b);
long temp = *a;
*a = *b;
*b = temp;
}
int main(void)
{
printf("The size of a pointer is: %zu\n", sizeof(int *));
long int a = 12;
long int b = 4;
printf("Addresses of a, b: %p, %p\n", &a, &b);
printf("a: %ld, b: %ld\n", a, b);
swap(&a, &b);
printf("a: %ld, b: %ld\n", a, b);
set_three(&a);
printf("a: %ld, b: %ld\n", a, b);
int ret = cool_thing(&b);
printf("a: %ld, b: %ld, and the return value is: %i\n", a, b, ret);
int fd = open("blah.c", O_RDONLY);
if (fd == -1) {
perror("open");
/* non zero exit code to indicate program failure */
return 1;
}
printf("The file descriptor is: %d\n", fd);
return 0;
}