Question

Write a C function  f such that … the one parameter of function  f is  p , which is...

  • Write a C function  f such that …
    • the one parameter of function  f is  p , which is an  int * to the last  int in a contiguous block of random  int values
      • Assume that there are at least 2 int values in the block of int values.
    • function  f returns the penultimate (next-to-last)  int value within the block of int values
      • You must calculate the difference between a pointer and an integer, and apply the dereferencing operator to that difference to determine that value that function  f returns.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Thanks for the question, here is the code for function f, also I write a program to demonstrate it. Please go through the comments so that you can understand and learn.

Hope it helps, let me know for any help with any other questions.

================================================================

#include<stdio.h>

int f(int*p){
  
   p = p-1; // subtracting 1 will point to the previous memory address
   return *p; // use deference to get the value at that memory address and return the value
}

int main(){
  
   int arr[] ={1,2,3};
  
   int*p = arr; // this now points to memory address where 1 is stored
   p++; // this now points to memory address where 2 is stored
   p++; // this now points to the lasst element in the array
  
   int penultimate = f(p); // pass the address to the function f
   printf("%d",penultimate); // it should print 2
  
}

Add a comment
Know the answer?
Add Answer to:
Write a C function  f such that … the one parameter of function  f is  p , which is...
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
  • Write a C function  f such that … the one parameter of function  f is  p , which is...

    Write a C function  f such that … the one parameter of function  f is  p , which is an  int * to the last  int in a contiguous block of random  int values Assume that there are at least 2 int values in the block of int values. function  f returns the penultimate (next-to-last)  int value within the block of int values You must calculate the difference between a pointer and an integer, and apply the dereferencing operator to that difference to determine that value that function  f...

  • In this assignment, you must complete the 5 functons below. Each function's body must consist of...

    In this assignment, you must complete the 5 functons below. Each function's body must consist of just a single C statement. Furthermore, the statement must not be a control statement, such as if, switch, or any kind of loop. For each problem, you are also restricted to the operators and constants as specified. */ /* 1. Write a function which zeros a variable x. A pointer to x is passed as a parameter. YOU MAY USE NO CONSTANTS in this...

  • Purpose The purpose of the lab is to learn how to pass pointers in C. Process Create the function...

    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 following table. Each function will add the last two integer parameters together Function PrototypeReturr int add2a(int,int); Returns the sum as a return value void add2b(int* int,int); Returns the sum in the first parameter int add2c(int*,int,int); Returns the sum as a return value and in the first parameter Returns the sum as a pointer to...

  • 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 language 3. (10 pts) Write a function readSegment that takes three parameters, a float array...

    C language 3. (10 pts) Write a function readSegment that takes three parameters, a float array as a pointer p as the first parameter, and two int indices startldx and endldx. The function readSegment reads the content of the array between startIdx and endldx, without using additional local variables. 4. (10 pts) Write a function called doubleOdds that takes two parameters, an array of double pointed to by pointer p and endldx of int type. The function doubleOdds doubles (multiply...

  • Write a recursive function (C++) that searches a binary tree that is NOT ordered like a...

    Write a recursive function (C++) that searches a binary tree that is NOT ordered like a BST. In other words, there is no ordering relationship among the parent, child or sibling node values. Use the following prototype and data structure: struct node { node *left; node *right; int val; }; // First parameter: pointer to a node // Second parameter: the value to find bool searchValue(node *, int); The function searchValue() should return true if the integer value in the...

  • 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,...

  • Write a C function named  f such that … the prototype of function  f is … unsigned long...

    Write a C function named  f such that … the prototype of function  f is … unsigned long int f ( unsigned long int X, unsigned long int Y ) function  f returns … the nonnegative integer  Z  such that   X 2  +   Y 2  =   Z 2  if such a nonnegative integer  Z  exists zero if there is no nonnegative integer  Z  such that   X 2  +   Y 2  =   Z 2  Example 1 If   X = 3  and   Y = 4 ...

  • Write a function that takes an array of C-strings as a parameter and the number of...

    Write a function that takes an array of C-strings as a parameter and the number of elements currently stored in the array, and returns a single C-string pointer that is the concatenation of all C-strings in the array. Note: The function must take 2 parameters and return "char *". Any other ways will cause some deduction of points.

  • C language please Write the definition of a function isSenior, which receives an integer parameter and...

    C language please Write the definition of a function isSenior, which receives an integer parameter and returns true if the parameter's value is greater or equal to 65, and false otherwise. So if the parameter's value is 7 or 64 or 12 the function returns false. But if the parameter's value is 69 or 83 or 65 the function returns true.

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