Question

Need help writing this code in C: /*WRITE A FUNCTION THAT MALLOCS 1 PHONE BOOK, INITALIZES...

Need help writing this code in C:

/*WRITE A FUNCTION THAT MALLOCS 1 PHONE BOOK, INITALIZES IT WITH A DEEP COPY OF THE GIVEN STRING, THEN REALLOCS TO 10 PHONE BOOKS, SETTING EACH OF THEM TO THE SAME DEFAULT NAME. YOU MAY USE STRCPY*/
PhoneEntries * initbook(char * defaultstring)
{
PhoneEntries * pBook;
int i = 0;

/*UNCOMMENT THIS CODE OUT AFTER YOU FINISH. IT SHOULD PRINT OUT TEST NAME TEN TIMES.
for( i =0; i < 10; i++)
    printf("Phone entries %s \n", pBook[i].pNames);*/

return pBook;
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1
PhoneEntries * initbook(char * defaultstring)
{
  //MALLOCS 1 PHONE BOOK
  PhoneEntries * pBook = (PhoneEntries*) malloc(sizeof(PhoneEntries));
  int i = 0;

  pBook->pNames = (char*) malloc(sizeof(char)*50);
  //INITALIZES IT WITH A DEEP COPY OF THE GIVEN STRING
  strcpy(pBook->pNames,defaultstring);

  pBook = (PhoneEntries*) realloc(pBook, sizeof(PhoneEntries)*10);
  for(i = 0;i<10;i++){
    pBook[i].pNames = (char*) malloc(sizeof(char)*50);
    //INITALIZES EACH WITH A DEEP COPY OF THE GIVEN STRING
    strcpy(pBook[i].pNames,defaultstring);
  }


  for( i =0; i < 10; i++)
    printf("Phone entries %s \n", pBook[i].pNames);

  return pBook;
}

\color{red}Note: \;Please \;comment \;below \;if \;you \;have \;any \;issues \;with \;this \;code.\;

\color{red}Please\; upvote\;the \;solution \;if \;it \;helped.\;Thanks!

Add a comment
Know the answer?
Add Answer to:
Need help writing this code in C: /*WRITE A FUNCTION THAT MALLOCS 1 PHONE BOOK, INITALIZES...
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 code in C++ by considering the following conditions :- Tasks :- 1. Create a...

    Write a code in C++ by considering the following conditions :- Tasks :- 1. Create a class Employee (with member variables: char * name, int id, and double age). 2. Create a constructor that should be able to take arguments and initialize the member variables. 3. Create a copy constructor for employee class to ensure deep copy. 4. Create a destructor and de allocate the dynamic memory. 5. Overload the assignment operator to prevent shallow copy and ensure deep copy....

  • I need help with the creation of the pre and post conditions for each function of...

    I need help with the creation of the pre and post conditions for each function of my program. Please see below. #include <iostream> #include <string.h> using namespace std; #define TOTAL_COMMANDS 7 struct command { char name[1024]; // Store the name of the command int *paramsCount; // Stores the valid no. of params char error[4096]; // Stores the valid errors int differentParams; // Store no. of different ways to specify params int differentErrors; // Store no. of different ways to error...

  • Language is in C. Need help troubleshooting my code Goal of code: * Replace strings with...

    Language is in C. Need help troubleshooting my code Goal of code: * Replace strings with a new string. * Append an s to the end of your input string if it's not a keyword. * Insert a period at the end, if you have an empty input string do not insert a period Problems: //Why does my code stop at only 2 input strings //If I insert a character my empty string case works but it's still bugged where...

  • C Question: I apologize for this long size post, but I wanted to explain the way...

    C Question: I apologize for this long size post, but I wanted to explain the way of doing it. Kindly HELP me . Question is: implement char *mystrcat(char *dest, const char*src)   adds a copy of the string src onto the end of the string starting at dest. The first char is src is put at the location of the 0 at the end of string dest. Returns dest Could you please implement / write the code in the same way...

  • Write a program **(IN C)** that displays all the phone numbers in a file that match the area code...

    Write a program **(IN C)** that displays all the phone numbers in a file that match the area code that the user is searching for. The program prompts the user to enter the phone number and the name of a file. The program writes the matching phone numbers to the output file. For example, Enter the file name: phone_numbers.txt Enter the area code: 813 Output: encoded words are written to file: 813_phone_numbers.txt The program reads the content of the file...

  • 3. (8 pts) You need to understand how to define and use a struct for this exercise. You should be able to figure out the...

    3. (8 pts) You need to understand how to define and use a struct for this exercise. You should be able to figure out the answer without actually compiling or running the program. Which is true about the following codes? If you choose a, you need to specify where the syntax error(s) occur; if you choose b, you need to specify what error occurs when you run it; if you choose c, you need to specify the outputs of the...

  • Need help writing this lab. Directions in lab attachment above. Thank you. Output should look like...

    Need help writing this lab. Directions in lab attachment above. Thank you. Output should look like ones in attachment ab Ass Part 1 sing Static Arra Do this part on your own. Write a program named lab11 a.c containing the following function: preconditions arc is terminated by dest is big enough hold arc postconditions dest contains src and is terminated by "10" void my strcpy (char dest const char srclj) This function will take two character arrays as parameters. The...

  • How would I change the following C code to implement the following functions: CODE: #include <stdio.h>...

    How would I change the following C code to implement the following functions: CODE: #include <stdio.h> #include <stdlib.h> int main(int argc, char * argv[]) { if (argc != 2) printf("Invalid input!\n"); else { FILE * f = fopen (argv[1], "r"); if (f != NULL) { printf("File opened successfully.\n"); char line[256]; while (fgets(line, sizeof(line), f)) { printf("%s", line); } fclose(f); } else { printf("File cannot be opened!"); } } return 0; } QUESTION: Add a function that uses fscanf like this:...

  • Write a full MIPS that behaves exactly like the following C program. The following C code...

    Write a full MIPS that behaves exactly like the following C program. The following C code shows the proposed algorithm. The string is traversed with two indices, called old_index and new_index, where the latter always takes a value less or equal to the former. When a non-space character is found, the character at position old_index is copied to position new_index, and both indices are incremented. When a space is found, the current character is not copied, and only old_index is...

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