Whats wrong with the following statement and why char c10 I

What\'s wrong with the following statement and why? char c[10] = \"I am a student\"; What value will the following program output? Explain in steps. #include int main() {int i. k, a[10], p[3]; k = 5; for (i - 0; i

Solution

Question 1:

Answer: Bufferoverflow exception. we defined char array of size 10 but we asigned number of character more than 10.

We will get this error message \"initializer-string for array of chars is too long\"

Question 2:

Answer: K value is 16. It will print 16

In first for loop, we are assigning a values to a int array

for(i=0; i<10; i++){
a[i] = i;
}

we are running loop from 0 to 9 and each time we are assigning i value each location of a int array so now a array location values are

a[0] = 0

a[1] = 1

a[2] = 2

a[3] = 3

a[4] = 4

a[5] = 5

a[6] = 6

a[7] = 7

a[8] = 8

a[9] = 9

in this for loop, we are calculating and assigning the values to p int array from location 0 to 2

for(i=0; i<3; i++){
p[i] = a[i*(i+1)];
}

at i= 0, p[0] = a[0 *(0+1)] = a[0] = 0 so p[0] = 0;

at i= 1, p[1] = a[1 *(1+1)] = a[2] = 2 so p[1] = 2;

at i= 2, p[2] = a[2 *(2+1)] = a[6] = 2 so p[2] = 6;

In this for loop, we are running i from 0 to 2

for(i=0; i<3; i++){
k+=p[i]*2;
}

at i =0, k = k + p[0] * 2 = 0 + 0 * 2 = 0 so k = 0

at i =0, k = k + p[1] * 2 = 0 + 2 * 2 = 4 so k =4

at i =0, k = k + p[2] * 2 = 4 + 6 * 2 = 16 so k =16

 What\'s wrong with the following statement and why? char c[10] = \
 What\'s wrong with the following statement and why? char c[10] = \

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site