I’m a beginner in C and am having some trouble with structures in C. Here is my code;
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
struct rec
{
char i;
char b;
char j;
} ;
int main()
{
struct rec *p;
p=(struct rec *) malloc (sizeof(struct rec));
(*p).i='hello';
(*p).b='world';
(*p).j ='there';
printf("%c %c %c\n",(*p).i,(*p).b,(*p).j);
free(p);
getch();
return 0;
}
The out of this is;
o d e
How can I pass in the whole word, rather than just one letter.
Define the structure members as
char *:and use
printfwith%s:Also, you need to replace
'with":(*p).j ="there";and if you assign string literals (which may not be modified), change the struct members toconst: