Ad

Chapter - 9: Arrays

What would be the output of the following programs:


Ad
J
Sections
1
Exercises

(a)

#include<stdio.h>
int main()
{
	int n[3][3] = {
		2, 4, 3,
		6, 8, 5,
		3, 5, 1
	};
	printf("\n%d %d %d", *n, n[3][3], n[2][2]);
	return 0;
}

Output: [Address of 1st 1d Array] [Garbage Value] 1


(b)

#include<stdio.h>
int main()
{
	int n[3][3] = {
		2, 4, 3,
		6, 8, 5,
		3, 5, 1
	};
	int i, *ptr;
	ptr = n;
	for (i = 0; i <= 8; i++)
		printf("\n%d", *(ptr + i));
	return 0;
}

Output:
2
4
3
6
8
5
3
5
1


(c)

#include<stdio.h>
int main()
{
	int n[3][3] = {
		2, 4, 3,
		6, 8, 5,
		3, 5, 1
	};
	int i, j;
	for (i = 0; i <= 2; i++)
		for (j = 0; j <= 2; j++)
			printf("\n%d %d", n[i][j], *(*(n + i) + j));
	return 0;
}

Output:
2 2
4 4
3 3
6 6
8 8
5 5
3 3
5 5
1 1

Ad

© 2024 Garbage Valuegarbage value logo