*how does constant qualifier used to \"improve,\" that is, make more readable an
ID: 3674424 • Letter: #
Question
*how does constant qualifier used to "improve," that is, make more readable and reliable, the program :
/* names1.c – uses pointers to a structure */
#include <stdio.h>
#include <string.h>
Struct namect {
Char fname [20];
Char lname [20];
Int letters;
};
void getinfo (struct namect *);
void makeinfo (struct namect *);
void showinfo(const struct namect *);
int main (void)
{
struct namect person;
getinfo(&person);
makeinfo(&person);
showinfo(&person);
return 0;
}
void getinfo (struct namect * pst)
{
printf(“Please enter your first name. ”);
gets(pst ->fname);
printf(“Please enter your last name. ”);
gets(pst ->lname);
}
void makeinfo (struct namect * pst)
{
pst ->letters = strlen(pst->fname) +
strlen(pst->lname);
}
void showinfo (const struct namect * pst)
{
printf (“%s %s, your name contains %d letters. ”,
pst->fname, pst->lname, pst->letters);
{
*
Explanation / Answer
void showinfo (const struct namect * pst)
{
printf (“%s %s, your name contains %d letters. ”,
pst->fname, pst->lname, pst->letters);
}
Use of constant in the above function make sures that when we call showinfo function, the function is not allowed to make any changes to the struct pointer variable pst. This improves our code reliability as we are sure that showinfo doesn't modify our pointer.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.