14758 - Pointer add function   

Description

Write a program that defines a function add_n(iptr, n) to increase the value of an integer by a specified amount.

The function has the following behavior:

  • Parameters:

    • iptr: a pointer to an integer

    • n: an integer value to add

  • Function Rules:

    1. If iptr is NULL, the function should immediately return 0.

    2. If iptr is not NULL, the function increases the integer pointed to by iptr by n, and then returns the updated value.

Function to implement:

int add_n (int *iptr, int n);

Main program:

#include<stdio.h>
int add_n (int *iptr, int n);

int main(void)
{
    int i, j, k;

    scanf("%d%d", &i, &j);
    printf("i = %d\n", i);

    k = add_n(NULL, j);

    printf("i = %d, k = %d\n", i, k);

    k = add_n(&i, j);

    printf("i = %d, k = %d\n", i, k);

    return 0;
}

int add_n (int *iptr, int n)
{
    // Your Task
}

Your task is to implement the function add_n(iptr, n) and complete the main program.

Input

Two integers i and j.

Output

Three lines formatted as follows:

i = <i>

i = <i>, k = <k>

i = <i>, k = <k>

Here, <i> and <k> represent the output values.

Ensure that the output, including formatting and commas, exactly matches the provided samples.

Make sure the placement of space空格 and newline 換行 characters in your output is identical to the sample output.

Sample Input  Download

Sample Output  Download

Tags

11410EE 231002



Discuss