Pointer increment

PHOTO EMBED

Sun Jun 09 2024 11:27:57 GMT+0000 (Coordinated Universal Time)

Saved by @meanaspotato #c

#include <stdio.h>

void increment(int* int_pointer);

int main(void)
{
	int x = 0;
	printf("> \n");
	scanf("%d", &x);
	// TODO: Call increment with x by reference
	increment(&x);
	printf("x holds %d\n\n", x);
	// TODO: Call increment with x by reference
	increment(&x);
	printf("x holds %d\n\n", x);
	// TODO: Call increment with x by reference
	increment(&x);
	printf("x holds %d\n\n", x);
	return 0;
}

// TODO: Define increment function:
void increment(int* int_pointer)
{
     (*int_pointer) ++;
    
}
content_copyCOPY

Objective The exercise aims to implement a function named increment that takes an integer pointer as a parameter and increments the value it points to. The function is to be called multiple times in the main function, and each time it should increase the value of a local variable x by one. Requirements Function Prototype: Declare a function prototype for increment that returns void and takes a single parameter, which is a pointer to an integer. Function Definition: Define the increment function such that it increases the value pointed to by its parameter by 1. Main Function: In the main function: Initialize a local integer variable x to zero. Scan a value for x. Call the increment function multiple times, passing the address of x. After each call, print the new value of x. Code Standards: Your code should follow good programming standards, including meaningful variable names, whitespace management, and proper commenting. Guidance Declare the function prototype for increment carefully before writing its definition. When calling increment, remember to pass the variable by reference, i.e., use the address-of operator (&). For example: Input Result 5 > x holds 6 x holds 7 x holds 8