Ad

Chapter - 10: Strings

Point out the errors, if any, in the following programs:


Ad
B
Sections
1
Exercises

(a)

 

#include<stdio.h>
#include<string.h>
int main()
{
	char *str1 = "United";
	char *str2 = "Front";
	char *str3;
	str3 = strcat(str1, str2);
	printf("\n%s", str3);
	return 0;
}

Error: As the str3 is declared but not defined, so it is an only void pointer, so without allocating memory to it, we cannot store anything to it.


(b)

 

#include<stdio.h>
int main()
{
	int arr[] = { 'A', 'B', 'C', 'D' };
	int i;
	for (i = 0; i <= 3; i++)
		printf("\n%d", arr[i]);
	return 0;
}

Error: No error.


(c)

 

#include<stdio.h>
int main()
{
	char arr[8] = "Rhombus";
	int i;
	for (i = 0; i <= 7; i++)
		printf("\n%d", *arr);
	arr++;
	return 0;
}

Error: In arr++ statement. As you can see that arr[8] is initialized at the time of declaration. So, in that case, we cannot assign anything to arr, and statement arr++ is execute as arr = arr + 1, which is illegal.

Ad

© 2024 Garbage Valuegarbage value logo