Question
Programming in C
Purpose The purpose of the lab is to learn how to pass pointers in C. Process Create the functions as defined in the followin
0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <stdio.h>

#include <stdlib.h>

int add2a(int a, int b){

return a+b;

}

void add2b(int *sum, int a, int b){

*sum = a+b;

}

int add2c(int *sum, int a, int b){

*sum = a+b;

return *sum;

}

int * add2d(int a, int b){

int *sum = (int*)malloc(sizeof(int));

*sum = a+b;

return sum;

}

const int * add2e(int a, int b){

int *sum = malloc(sizeof(int));

*sum = a+b;

return sum;

}

int main() {

int result;

int *sum = &result;

printf("add2a = %d\n",add2a(2,3));

add2b(sum,2,3);

printf("add2b = %d\n",result);

int p = 5, q = 9;

printf("add2c = %d\n",add2c(sum,p,q));

p = 11, q = 20;

sum = add2d(p,q);

printf("add2d = %d\n",*sum);

p = 10, q = 12;

const int *ptr = add2e(p,q);

printf("add2e = %d\n",*ptr);

return 0;

}

=====================================================================
SEE OUTPUT
main.c saved clang version .0.0-3ubunt al) add2a5 add2b5 add2c 14 add2d- 31 add2e- 22 Files 21 22 E const int add2e(int a, in

Thanks. PLEASE UPVOTE. Thanks, PLEASE COMMENT if there is any concern.

Add a comment
Know the answer?
Add Answer to:
Purpose The purpose of the lab is to learn how to pass pointers in C. Process Create the function...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C++ Pointers Below is a partially written function that has two parameters (an int and a...

    C++ Pointers Below is a partially written function that has two parameters (an int and a pointer to an int). Finish the function by making the value being pointed at the first parameter and also return the pointer. int * func(int x, int * y) { // Finish me! }

  • C PROGRAM the functions create and free a dynamically allocated 2D array. It should function like...

    C PROGRAM the functions create and free a dynamically allocated 2D array. It should function like a statically allocated array (use malloc()) int **createArray(int rows, int cols) parameters to this function are the number of rows and cols in the array. The return value is the array that you have dynamically allocated void freeArray (int **array, int rows, int cols) the parameters to this function are: -the array created by createArray() -the number of rows in the array -the number...

  • C PROGRAM the functions create and free a dynamically allocated 2D array. It should function like...

    C PROGRAM the functions create and free a dynamically allocated 2D array. It should function like a statically allocated array (use malloc()) int **createArray(int rows, int cols) parameters to this function are the number of rows and cols in the array. The return value is the array that you have dynamically allocated void freeArray (int **array, int rows, int cols) the parameters to this function are: -the array created by createArray() -the number of rows in the array -the number...

  • write C code that uses pointers, arrays, and C strings. 3. Write a function called pow_xy....

    write C code that uses pointers, arrays, and C strings. 3. Write a function called pow_xy. The function should be passed 2 parameters, as illustrated in the prototype below. int pow_xy(int *xptr, int y); Assuming that xptr contains the address of variable x, pow_xy should compute x to the y power, and store the result as the new value of x. The function should also return the result. Do not use the built-in C function pow. For the remaining problems,...

  • 3460:209 Assignment 9-B Assignment9-B: The Element Shifter The purpose of this assignment is to help gauge...

    3460:209 Assignment 9-B Assignment9-B: The Element Shifter The purpose of this assignment is to help gauge your skills in writing small programs that involve pointers. The program also contains functions and may perform input, output, files and file processing, use arrays and vectors and/or c-string/string arrays, flow of control, and/or calculations. PROGRAM SPECIFICATION For this program, we are going to expand a standard array by dynamically allocating a new one with a larger footprint. The program will use a function...

  • IntList Recursion Assignment Specifications: You will add some additional recursive functions to your IntList class as...

    IntList Recursion Assignment Specifications: You will add some additional recursive functions to your IntList class as well as make sure the Big 3 are defined. IntNode class I am providing the IntNode class you are required to use. Place this class definition within the IntList.h file exactly as is. Make sure you place it above the definition of your IntList class. Notice that you will not code an implementation file for the IntNode class. The IntNode constructor has been defined...

  • Need helps on C++ attach. Thanks Pointers with classes a) A user-defined class named Timer has...

    Need helps on C++ attach. Thanks Pointers with classes a) A user-defined class named Timer has a constructor that takes two integer parameters to initialize hour and minute data members. Write a single C++ statement to create an object of the Timer class using dynamic memory allocation and assign it to a pointer variable named timePt r. It should call the constructor with two parameters. Use values of 10 and 20 for hour and minute respectively. b) Write the definition...

  • Hi!, having trouble with this one, In this class we use visual studio, C++ language --------------------------------------------------------------...

    Hi!, having trouble with this one, In this class we use visual studio, C++ language -------------------------------------------------------------- Exercise #10 Pointers - Complete the missing 5 portions of part1 (2 additions) and part2 (3 additions) Part 1 - Using Pointers int largeArray (const int [], int); int largePointer(const int * , int); void fillArray (int * , int howMany); void printArray (const char *,ostream &, const int *, int howMany); const int low = 50; const int high = 90; void main()...

  • C++ Chapter 16 Problem: Implement #25 as a template function (Demonstrate using int, double, string, and...

    C++ Chapter 16 Problem: Implement #25 as a template function (Demonstrate using int, double, string, and x,y pair object) 24. Write a function that searches a numeric array for a specified value. The function should return the subscript of the element containing the value if it is found in the array. If the value is not found, the function should throw an exception. 25. Write a function that dynamically allocates a block of memory and returns a char pointer to...

  • (C++ concept on local variable) On page 14 of the “Pointers and Memory” handout there is...

    (C++ concept on local variable) On page 14 of the “Pointers and Memory” handout there is a reference to the “Amperand (&)” Bug in the TAB() function. Draw a trace of the local variables of the functions Victim() and TAB() on the run-time stack as these functions are executed. Your diagram should be similar to the one provided on page 13. Your diagram should depict exactly three instances in program execution - T1: Right after the first statement of Victim(),...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT