Given the following declarations struct s1 char b struct s2
Given the following declarations: struct s1 {char *b;}; struct s2 {char a [10];}; struct s3 {char *c; struct s1 *s; struct s2 t;}; Correct the errors in the following code: int main(int argc, char *argv[]) {struct s3 *st; st = (struct s3*) malloc(sizeof(s3)); st.s = (struct s1*)malloc(sizeof(s1)); st rightarrow s.b = (char *)malloc(strlen(\"CLEMSON\")); strcpy(\"CLEMSON\", st rightarrows t rightarrow b); strcpy(st rightarrow t rightarrow a, \"TIGERS\");}
Solution
Answer
#include <string.h>
#include <stdio.h>
struct s1 { char *b; };
struct s2 { char a[10]; };
struct s3
{
char *c;
struct s1 *s;
struct s2 t;
};
int main(int argc,char *argv[])
{
struct s3 *st;
st=(struct s3*)malloc(sizeof(struct s3));
//st.s=(struct s1*)malloc(sizeof(s1)); //here s is of s1 structure
st->s=(struct s1*)malloc(sizeof(struct s1));
//st->s.b=(char*)malloc(strlen(\"CLEMSON\")); //here b is of pointer type in s1 structure
st->s->b=(char*)malloc(strlen(\"CLEMSON\"));
strcpy(\"CLEMSON\",st->s->b);
strcpy(st->t.a,\"TIGERS\"); //here a is an char array in s2 structure
return 0;
}
![Given the following declarations: struct s1 {char *b;}; struct s2 {char a [10];}; struct s3 {char *c; struct s1 *s; struct s2 t;}; Correct the errors in the fo Given the following declarations: struct s1 {char *b;}; struct s2 {char a [10];}; struct s3 {char *c; struct s1 *s; struct s2 t;}; Correct the errors in the fo](/WebImages/28/given-the-following-declarations-struct-s1-char-b-struct-s2-1075394-1761563945-0.webp)