in C, just edit where it says TODO. /* In this assignment, we experiment with passing references to functions On the following web site, you

Answered step by step
Verified Expert Solution
Question
63 users unlocked this solution today!

in C, just edit where it says "TODO".

/* In this assignment, we experiment with passing references to functions

On the following web site, you can find a swap funciton that swaps two integers. https://www.tutorialspoint.com/cprogramming/c_function_call_by_reference.htm

In this exercise, we implement two functions. One function swaps two pointers to int, and the other swaps structures.

Search TODO to find the locations where we need to work */

#include #include #include #include #include

#define NAME_SIZE 64

typedef struct node_tag { unsigned int id; char name[NAME_SIZE]; } person_t;

// TODO // implement a function to swap two (int *) // add two paramters of correct types // the function does not return a value // void swap_pointer_int();

void test_swap_pointer_int(int argc, char *argv[]) { int a = 10, b = 20; int *pa = &a, *pb = &b;

if (argc >= 2) a = atoi(argv[1]);

if (argc >= 3) b = atoi(argv[2]);

printf("Before swap "); // you can print the value of pointers, if you like // but submission should not print pointers // printf(" pa is %p, pb is %p ", pa, pb); printf("*pa is %d, *pb is %d ", *pa, *pb);

// TODO // one line to call swap_point_int() to swap pa and pb

printf("After swap "); // printf(" pa is %p, pb is %p ", pa, pb); printf("*pa is %d, *pb is %d ", *pa, *pb); }

// TODO // implement a function to swap two structures of type person_t // add two paramters of correct types // the function does not return a value // void swap_person();

void test_swap_person(int argc, char *argv[]) { // initialize structures person_t x = {1, "Alice"}, y = {2, "Bob"};

if (argc >= 2) { strncpy(x.name, argv[1], NAME_SIZE - 1); x.name[NAME_SIZE - 1] = 0; }

if (argc >= 3) { strncpy(y.name, argv[2], NAME_SIZE - 1); y.name[NAME_SIZE - 1] = 0; }

printf("Before swap "); printf("x's id is %d, x's name is %s ", x.id, x.name); printf("y's id is %d, y's name is %s ", y.id, y.name);

// TODO // one line to call swap_person() to swap x and y printf("After swap "); printf("x's id is %d, x's name is %s ", x.id, x.name); printf("y's id is %d, y's name is %s ", y.id, y.name); }

/* Do not change main() */ int main(int argc, char *argv[]) { if (argc == 1) { test_swap_pointer_int(argc, argv); test_swap_person(argc, argv); } else if (isdigit(argv[1][0])) // if the first argument starts with a digit test_swap_pointer_int(argc, argv); else test_swap_person(argc, argv); return 0; }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Link Copied!

Step: 1

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

100% Satisfaction Guaranteed-or Get a Refund!

Step: 2Unlock detailed examples and clear explanations to master concepts

blur-text-image_2

Step: 3Unlock to practice, ask and learn with real-world examples

blur-text-image_3

See step-by-step solutions with expert insights and AI powered tools for academic success

  • tick Icon Access 30 Million+ textbook solutions.
  • tick Icon Ask unlimited questions from AI Tutors.
  • tick Icon Order free textbooks.
  • tick Icon 100% Satisfaction Guaranteed-or Get a Refund!

Claim Your Hoodie Now!

Recommended Textbook for

SQL For Data Science Data Cleaning Wrangling And Analytics With Relational Databases

Authors: Antonio Badia

1st Edition

3030575918, 978-3030575915

More Books
flashcard-anime

Study Smart with AI Flashcards

Access a vast library of flashcards, create your own, and experience a game-changing transformation in how you learn and retain knowledge

Explore Flashcards

Students Have Also Explored These Related Databases Questions!