Reputation: 31
I'm confused about the syntax of struct namect *
within the function declarations of getinfo
, makeinfo
, showinfo
and cleanup
.
Normally I'd expect that a variable name would follow the asterisk ante-ceding "namect" to create a pointer to a structure namect
. Does this simply mean that the argument passed to the function is a pointer to data of type struct namect
?
// names3.c -- use pointers and malloc()
#include <stdio.h>
#include <string.h> // for strcpy(), strlen()
#include <stdlib.h> // for malloc(), free()
#define SLEN 81
struct namect {
char * fname; // using pointers
char * lname;
int letters;
};
void getinfo(struct namect *); // allocates memory
void makeinfo(struct namect *);
void showinfo(const struct namect *);
void cleanup(struct namect *); // free memory when done
char * s_gets(char * st, int n);
int main(void)
{
struct namect person;
getinfo(&person);
makeinfo(&person);
showinfo(&person);
cleanup(&person);
return 0;
}
void getinfo (struct namect * pst)
{
char temp[SLEN];
printf("Please enter your first name.\n");
s_gets(temp, SLEN);
// allocate memory to hold name
pst->fname = (char *) malloc(strlen(temp) + 1);
// copy name to allocated memory
strcpy(pst->fname, temp);
printf("Please enter your last name.\n");
s_gets(temp, SLEN);
pst->lname = (char *) malloc(strlen(temp) + 1);
strcpy(pst->lname, temp);
}
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.\n",
pst->fname, pst->lname, pst->letters);
}
void cleanup(struct namect * pst)
{
free(pst->fname);
free(pst->lname);
}
char * s_gets(char * st, int n)
{
char * ret_val;
char * find;
ret_val = fgets(st, n, stdin);
if (ret_val)
{
find = strchr(st, '\n'); // look for newline
if (find) // if the address is not NULL,
*find = '\0'; // place a null character there
else
while (getchar() != '\n')
continue; // dispose of rest of line
}
return ret_val;
}
Upvotes: 2
Views: 132
Reputation: 41
Yes. The function declaration void getinfo(struct namect *);
(for example) just says:
struct namect
.In void showinfo(const struct namect *);
, the declaration says:
struct namect
.Upvotes: 0
Reputation: 67476
void getinfo(struct namect *);
It is called function prototype and it informs the compiler that somewhere in the code there is a definition of the function called getinfo
which takes one parameter of type "pointer to struct namect" and does not return anything.
This information compiler needs to call this function correctly.
i'm just confused as to why
void getinfo(struct namect *);
isntvoid getinfo(struct namect * pointer_name);
The pointer_name
is not required in the function prototype. But you can also declare it as
void getinfo(struct namect * pointer_name);
Both versions are correct.
Upvotes: 2
Reputation: 63167
Since there is no typedef struct namect namect;
, only struct namect
exists. There is no namect
.
Thus, const struct namect *
is just a non-constant pointer to a constant struct namect
.
Upvotes: 2