0% found this document useful (0 votes)
5 views2 pages

C Program: Struct and String Concatenation

The document defines a struct called TEST with integer and string pointer members. It allocates memory for a TEST pointer, initializes the integer member, and concatenates two strings into the string member. It then prints and frees the allocated memory before returning.

Uploaded by

Pradeep Kumar
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views2 pages

C Program: Struct and String Concatenation

The document defines a struct called TEST with integer and string pointer members. It allocates memory for a TEST pointer, initializes the integer member, and concatenates two strings into the string member. It then prints and frees the allocated memory before returning.

Uploaded by

Pradeep Kumar
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

int a;

static int b;

main()
{
int c;
static int d;
}

typedef struct test {


int a;
char *str;
}TEST;

int main()
{
TEST *ptr = NULL;
char *name1 = "CHIRAGKUMAR";
char *name2 = "PANELIYA";

/* populate the ptr with values. Use name1 and name2 to fill *str.
Expected end result for str is "CHIRAGKUMARPANELIYA" */

/* After successful population, terminate the program gracefully */

[11:57 AM] deepak (Guest)


ptr = (TEST *) malloc(sizeof(TEST));
[11:57 AM] deepak (Guest)
ptr->a = 100;
[11:58 AM] deepak (Guest)
int i =0, j=0;
[11:59 AM] deepak (Guest)
while (name[i] = '\0')
[11:59 AM] deepak (Guest)
{
[12:02 PM] deepak (Guest)

ptr = (TEST *) malloc(sizeof(TEST));

ptr->a = 100;
ptr->str = strcat(name1, name2);

typedef struct test {


int a;
char *str;
}TEST;
int main()
{
TEST *ptr = NULL;
char *name1 = "CHIRAGKUMAR";
char *name2 = "PANELIYA";

/* populate the ptr with values. Use name1 and name2 to fill *str.
Expected end result for str is "CHIRAGKUMARPANELIYA" */

printf("enter the number\n");


scanf("%d",&ptr->a);
strcpy(ptr->str,name1);
strcat(ptr->str,name2);

printf("a=%d and str=%s\n",ptr->a,ptr->str);

/* After successful population, terminate the program gracefully */


free(ptr);
ptr=NULL;

return 0;
}

You might also like