Question

Fill in the blanks in the following C statement such that it allocates memory for an array of 50 character values

 1.  Fill in the blanks in the following C statement such that it allocates memory for an array of 50 character values:

 char *name = (_______ ) malloc(_______ )

 2.  Write a declaration for an array a of 10 strings, each of which has at most 80 characters (including the null character):

 3.  Circle or underline all syntax, logic, and runtime errors found in the following C fragment. Be sure to circle omitted punctuation.

image.png

 4. What are two advantages of using recursion in a C program?


 5. Complete the following statements based on this declaration. Assume that the operations are cumulative:

image.png

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

Ans1: char *name = (char *)malloc(50);

Syntax: pointer_variable = (cast-type*)malloc(byte-size)

Ans2: char [10][80];

We have to declare this by using 2d array where 1st dimension is the number of strings and the 2nd dimension is the size of each strings.

Ans3:

char *name_ptr == NULL,name[50] = "Barack Obama", *name2 = "Joe Biden";
name_ptr = name[0];
do
{
printf("Enter a new name: ")
scanf("%c",name_ptr);
}while(name==name2);

1. == syntax error, comparing pointer variable and NULL value instead of assigning NULL value to pointer variable.

2. name_ptr = name[0] error is you are assigning value of name[0] to the pointer but pointer variable is used to store the address of another variable.

3.   printf("Enter a new name: ") there should be semi-colon at the end.

Correct code:

char *name_ptr = NULL,name[50] = "Barack Obama", *name2 = "Joe Biden";
   name_ptr = &(name[0]);
    do
    {
        printf("Enter a new name: ");
        scanf("%c",name_ptr);
    }while(name==name2);

Ans4:

1. Reduces time complexity.

2. Reduces number of unnecessary function calls as recursive function calls itself when it is needed or satisfy some conditions.

Ans5:

1. 6 as strlen function returns the length of the string.

2. The contents of touch[4] is NULL as the touch variable string ends with NULL character which is in 4th index place of string.

3. Positive, as 1st letter of both the string is unmatched at starting so stopping character stops there and stops comparing other characters in the strings. And also the ascii value of 1st character of string 1st is greater than ascii value of 1st character of string 2nd that's why it results positive value.

4. bitt , syntax: strncpy(char *s1, char *s2,size)

strncpy(touch, taste,4) here the string of 4 characters is copied to character touch variable.

5. t , syntax: strncat(char *destination, char *source, size)

strncat(taste,&touch[3],1) so here only 1 character is appended to destination string which is taste character variable.

so after appending the string the result is : bittert

so taste[2] so character 't' is at index 2 position.

Add a comment
Know the answer?
Add Answer to:
Fill in the blanks in the following C statement such that it allocates memory for an array of 50 character values
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
  • I have some questions about pointers/pointer arithmetic in C++ 1) Pointers and Pointer Arithmetic a.)       What...

    I have some questions about pointers/pointer arithmetic in C++ 1) Pointers and Pointer Arithmetic a.)       What is the difference between statically allocated arrays and dynamically allocated arrays (be brief) b.)      Which of the following pointers can be used for a dynamically allocated array? (Circle) char ptr; char * ptr; char ptr *; char ptr[]; char [] ptr; c.)       Show now, using that pointer, how to dynamically allocate array of characters of size 10: (don't use malloc) d.)      Which of the...

  • 31. True or False: The condition in the following if statement is a syntax error:

     31. True or False: The condition in the following if statement is a syntax error: int number=50;…… if (number)…… 32. True or False: The standard C functlon strlen) will return 4 when invoked upon the varlable name, declared below: char namel = (e', 'p', 't', 's', '\0', '1', '2', '1', '\0']; 33. True or False: A structure is a collection of related variables under one name. 34. True or False: In C, output parameters allow for a function to return more than one value Indirectly. 35. True or...

  • 2. (50 marks) A string in C++ is simply an array of characters with the null...

    2. (50 marks) A string in C++ is simply an array of characters with the null character(\0) used to mark the end of the string. C++ provides a set of string handling function in <string.h> as well as I/O functions in <iostream>. With the addition of the STL (Standard Template Library), C++ now provides a string class. But for this assignment, you are to develop your own string class. This will give you a chance to develop and work with...

  • Lab 2: (one task for Program 5): Declare an array of C-strings to hold course names,...

    Lab 2: (one task for Program 5): Declare an array of C-strings to hold course names, and read in course names from an input file. Then do the output to show each course name that was read in. See Chapter 8 section on "C-Strings", and the section "Arrays of Strings and C-strings", which gives an example related to this lab. Declare the array for course names as a 2-D char array: char courseNames[10] [ 50]; -each row will be a...

  • C program: Write a program that assigns and counts the number of each alphabetic character in...

    C program: Write a program that assigns and counts the number of each alphabetic character in the Declaration of Independence and sorts the counts from the most used to the least used character. Consider upper and lower case letters the same. The frequency of each character should be accumulated in an array. USE POINTERS to increment counts for each letter, sort your array, and display the results. Output should consist of two columns: (1) each letter 'a'-'z' and (2) the...

  • using C codding language, Write a program for following tasks: 1. get string (character array) from...

    using C codding language, Write a program for following tasks: 1. get string (character array) from user and save it in char input. Assume maximum size of input is going to be 50 characters. 2. print out given input on the screen. 3. print out size of given input. (Hint: sizeof function)

  • C programming 1) When setting a two-dimensional character array, how is the size (number of characters)...

    C programming 1) When setting a two-dimensional character array, how is the size (number of characters) in the second dimension set? Select an answer: The number of elements are equal to the average size of all the strings. To the length of the longest string; you don't need to add one because the first array element is zero. To the length of the longest string, plus one for the null character. The second dimension is equal to the number of...

  • I am having problems with the following assignment. It is done in the c language. The...

    I am having problems with the following assignment. It is done in the c language. The code is not reading the a.txt file. The instructions are in the picture below and so is my code. It should read the a.txt file and print. The red car hit the blue car and name how many times those words appeared. Can i please get some help. Thank you. MY CODE: #include <stdio.h> #include <stdlib.h> #include <string.h> struct node { char *str; int...

  • Please answer the following questions in terms of C syntax: [q9_3]: Fill in the blanks in...

    Please answer the following questions in terms of C syntax: [q9_3]: Fill in the blanks in each of the following: a) Computers store large amounts of data on secondary storage devices as __________. b) A(n) __________ is composed of several fields. c) To facilitate the retrieval of specific records from a file, one field in each record is chosen as a(n) __________. d) A group of related characters that conveys meaning is called a(n) __________. e) The file pointers for...

  • Need this in C The starter code is long, if you know how to do it...

    Need this in C The starter code is long, if you know how to do it in other way please do. Do the best you can please. Here's the starter code: // ----------------------------------------------------------------------- // monsterdb.c // ----------------------------------------------------------------------- #include #include #include // ----------------------------------------------------------------------- // Some defines #define NAME_MAX 64 #define BUFFER_MAX 256 // ----------------------------------------------------------------------- // Structs typedef struct { char name[NAME_MAX]; int hp; int attackPower; int armor; } Character; typedef struct { int size; Character *list; } CharacterContainer; // ----------------------------------------------------------------------- //...

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