Chapter - 11: Structures

What would be the output of the following programs:


A
Sections
1
Exercises

(a)

#include<stdio.h>
#include<string.h>
int main()
{
	struct gospel
	{
		int num;
		char mess1[50];
		char mess2[50];
	} m;
	m.num = 1;
	strcpy(m.mess1, "If all that you have is hammer");
	strcpy(m.mess2, "Everything looks like a nail");
	/* assume that the strucure is located at address 1004 */
	printf("\n%u %u %u", &m.num, m.mess1, m.mess2);
	return 0;
}

Output: 1004 1008 1058


(b)

#include<stdio.h>
#include<string.h>
int main()
{
	struct part
	{
		char partname[50];
		int partnumber;
	};
	struct part p, *ptrp;

	ptrp = &p;
	strcpy(p.partname, "CrankShaft");
	p.partnumber = 102133;
	
	printf("%s %d\n", p.partname, p.partnumber);
	printf("%s %d\n", (*ptrp).partname, (*ptrp).partnumber);
	printf("%s %d\n", ptrp->partname, ptrp->partnumber);
	return 0;
}

Output: 
CrankShaft 102133
CrankShaft 102133
CrankShaft 102133


(c)

#include<stdio.h>
#include<string.h>

struct gospel
{
	int num;
	char mess1[50];
	char mess2[50];
} m1 = { 2, "If you are driven by success",
"make sure that it is a quality drive"
};

int main()
{
		struct gospel m2, m3;
		m2 = m1;
		m3 = m2;
		printf("\n%d %s %s", m1.num, m2.mess1, m3.mess2);

	return 0;
}

Output: 2 If you are driven by success make sure that it is a quality drive


© 2020 Garbage Valuegarbage value logo