As explained in the comment, you will allocate/free exactly the same as you would if the pointer were not a member to a struct. While it appears you do not need a char **name
(pointer-to-pointer-to-char) unless one id
is associated with many names
. Simply declare a pointer to name (e.g. char *name;
) would be sufficient to provide a one-to-one relationship with id
.
Putting those pieces together, and adjusting the format-specifiers for scanf
to read/discard the trailing newlines after the read of size
, you could do something like:
# include <stdio.h>
# include <stdlib.h>
// # include <conio.h>
typedef struct information {
int id ;
char *name ;
} info ;
int main (void) {
info s1;
int size;
printf ("\n Enter max size of students name : ");
scanf ("%d",&size);
s1.name = malloc (size * sizeof *s1.name);
printf ("\n Enter Id : ");
scanf ("%d%*c", &s1.id);
printf ("\n Enter Name : ");
scanf ("%[^\n]%*c", s1.name);
printf ("\n You entered, id: %d, name: %s\n\n", s1.id, s1.name);
free (s1.name);
// getche () ;
return 0;
}
Example Use/Output
$ ./bin/struct_stdnm
Enter max size of students name : 29
Enter Id : 231
Enter Name : Alfred Q. Myres
You entered, id: 231, name: Alfred Q. Myres
Let me know if you have further questions or further needs.
Note: if you need an array of id
and name
s, then you need an array of struct information
, not a double-pointer to name
-- if I interpret what makes sense for what you are doing correctly.
Protect Against Input Beyond Allocation
As noted in the comments below, scanf
is not optimal for reading s1.name
because it does not allow a maximum field width by variable. To properly protect against against writing beyond the end of your allocation, you should use fgets
and limit your read to size
:
printf ("\n Enter Name : ");
fgets (s1.name, size, stdin);
Example
$ ./bin/struct_stdnm
Enter max size of students name : 5
Enter Id : 123
Enter Name : Johnny Jones
You entered, id: 123, name: John
Validation Required on All Allocation & Input
Regardless of whether you stick with scanf
for input or use fgets
, you must validate all allocations and input to insure you actually allocate the memory you intend to use, and you actually read what you think you have read. Think of this is just another layer of completeness for your code. While learning it may be convenient to use examples without complete validation, you should never consider your code complete until you provide sufficient checks for all allocations and input. Here is an update showing minimal validation checks for your code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// # include <conio.h>
typedef struct information {
int id ;
char *name ;
} info ;
int main (void) {
info s1;
int size;
size_t len;
printf ("\n Enter max size of students name : ");
if (scanf ("%d",&size) != 1) { /* validate converison */
fprintf (stderr, "error: conversion of size failed.\n");
return 1;
}
if (size < 1) { /* validate size */
fprintf (stderr, "error: invalid size entered.\n");
return 1;
}
/* always validate allocations */
if (!(s1.name = malloc (size * sizeof *s1.name))) {
fprintf (stderr, "error: virtual memory exhausted.\n");
return 1;
}
printf ("\n Enter Id : ");
if (scanf ("%d%*c", &s1.id) != 1) { /* validate converison */
fprintf (stderr, "error: conversion of size failed.\n");
return 1;
}
printf ("\n Enter Name : ");
if (fgets (s1.name, size, stdin)) {
len = strlen (s1.name);
/* validate all characters fit within 'size' allocation */
if (len + 1 == (size_t)size && (s1.name)[len-1] != '\n')
printf (" warning: name exceeded allocation.\n");
/* remove trailing newline from name */
if (len && (s1.name)[len-1] == '\n')
(s1.name)[--len] = 0;
}
else {
fprintf (stderr, "error: name entry failed.\n");
return 1;
}
printf ("\n You entered, id: %d, name: %s\n\n", s1.id, s1.name);
free (s1.name);
// getche () ;
return 0;
}
This also flags any time you have not allocated enough space to hold the entire name entered:
Short Allocation Example
$ ./bin/struct_stdnm
Enter max size of students name : 5
Enter Id : 123
Enter Name : Johnny Jones
warning: name exceeded allocation.
You entered, id: 123, name: John
Let me know if you have further questions.