Question

Function with Pointer (C++) 1. The function should be named "myFunction" which takes two pointers to...

Function with Pointer (C++)

1. The function should be named "myFunction" which takes two pointers to chars and returns void (nothing).

2. The value pointed in the first argument should be copied into the value of the second pointer (and the second to the first).

3. The pointer itself is not copied, just the char it points to.

Thanks in advance!

0 0
Add a comment Improve this question Transcribed image text
Answer #1

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

#include <iostream>

using namespace std;

// The function should be named "myFunction"
// which takes two pointers to chars
// and returns void (nothing).
void myFunction(char*, char*);

int main()
{

// accept the first and second characters
char first, second;
cout << "Enter first character : ";
cin >> first;
cout << "Enter second character : ";
cin >> second;

  
// print the values of the variables before calling function
cout << endl << "Values before copying::" << endl;
cout << "first character = " << first << endl;
cout << "second character = " << second << endl << endl;
  

// call the function by passing the variables first and second
// &first is the address of first and &second is the address of second
myFunction(&first, &second);
  
// print the values of the variables after calling functiona
cout << endl << "Values after copying::" << endl;
cout << "first character = " << first << endl;
cout << "second character = " << second << endl << endl;
return 0;
}

void myFunction(char* c1, char* c2) {
// The value pointed in the first argument
// is copied into the value of the second argument
// and it is done with the help of an intermediate
// temporary variable temp
  
// The pointer itself is not copied, just the char it points to.
// *c1 and *c2 refers the value stored at address c1 and c2 respectively.
char temp;
temp = *c1;
*c1 = *c2;
*c2 = temp;
}

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

Add a comment
Know the answer?
Add Answer to:
Function with Pointer (C++) 1. The function should be named "myFunction" which takes two pointers to...
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
  • In C: Write a function "MinMax" that takes as an argument an integer array, an integer...

    In C: Write a function "MinMax" that takes as an argument an integer array, an integer for the size of the array, and two integer pointers. The function should print nothing and return nothing but change the value of the first pointer to the minimum value in the array and change the value in the second pointer to the max value in the array.

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

  • **C** Write a C function that inputs a pointer to a string and a pointer to...

    **C** Write a C function that inputs a pointer to a string and a pointer to a buffer. The function then scans through the string removing leading and trailing whitespace and replacing any run of whitespace within the string with a single space. Your function should follow this prototype: void tighten(char *oldstring, char* newstring, int length); The first argument is a pointer to a null-terminated string sitting somewhere in memory. The second argument is a pointer to the first char...

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

  • 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 programming. 1.Create a program that does the following - Creates three pointers, a character pointer...

    C programming. 1.Create a program that does the following - Creates three pointers, a character pointer professor, and two integer pointers student_ids, grades - Using dynamic memory, use calloc to allocate 256 characters for the professor pointer - Prompts the professor for their name, and the number of students to mark. - Stores the professor’s name using the professor pointer and in an integer the number of students to mark. - Using dynamic memory, use malloc to allocate memory for...

  • 2) (10 pts) Write a function that takes in a pointer to a linked list of...

    2) (10 pts) Write a function that takes in a pointer to a linked list of nodes storing integers and a variable named value, and returns the number of nodes in the list storing that value. For example, if a list pointed to by listPtr stores 2, 6, 2, 3, 4, 2, 6, and 6 and value = 6, your function should return 3, since 6 appears in the list 3 times. Please use the struct and function prototype provided...

  • 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++ pointers and linked lists 1. Declare an integer pointer variable intPointer. Initialize it to point...

    C++ pointers and linked lists 1. Declare an integer pointer variable intPointer. Initialize it to point to an int variable named someInt. Assign the value 451 to someInt and output (cout) the variable someInt and output (cout) the value pointed to by intPointer. Write an assignment statement that indirectly stores 900 into the value pointed to by intPointer. Output (cout) the value pointed to by intPointer and output (cout) the variable someInt, 2. Declare a pointer variable charArrPointer and initialize...

  • In the space below, write a C function definition for a function named StrLower, which will...

    In the space below, write a C function definition for a function named StrLower, which will receive one parameter, a pointer to a null-terminated string (i.e., pointer to char), convert each character in the string to lowercase (using the tolower function from <ctype.h>), and return nothing to the caller. Inside the function definition, you may use a for loop or a while loop. If you use any variables other than the incoming parameter, you must define them locally in the...

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