216k views
4 votes
Questions: 1) Run the C program, attach a screenshot of the output in the answer sheet. 2) Check the address of the array and the address of the first element in the array printed out using method 1. Are they the same? If yes, please explain why. 3) Check the addresses of the elements in the array printed out using method 1 and method 2 separately. Are they the same? If yes, please explain why. 4) Write down the statement to print out the length of the array by using sizeof operator. 5) If array numbers is passed to a function, can we still use the sizeof operator to obtain the length the array numbers in that function? If not, please explain why.

1 Answer

3 votes

Answer:

Output

mumners = 0x7ffc4d2767d0

Method1: Address of array Elements

numbers[0] = 0x7ffc4d2767d0

numbers[1] = 0x7ffc4d2767d4

numbers[2] = 0x7ffc4d2767d8

numbers[3] = 0x7ffc4d2767dc

numbers[4] = 0x7ffc4d2767e0

Method2: Address of array Elements

numbers[0] = 0x7ffc4d2767d0

numbers[1] = 0x7ffc4d2767e4

numbers[2] = 0x7ffc4d2767f8

numbers[3] = 0x7ffc4d27680c

numbers[4] = 0x7ffc4d276820

sizeof(numbers) = 20

2.

mumners = 0x7ffc4d2767d0

Method1: Address of array Elements

numbers[0] = 0x7ffc4d2767d0

Yes both are same since address of the array is the address of its first element

3.

Method1: Address of array Elements

numbers[0] = 0x7ffc4d2767d0

numbers[1] = 0x7ffc4d2767d4

numbers[2] = 0x7ffc4d2767d8

numbers[3] = 0x7ffc4d2767dc

numbers[4] = 0x7ffc4d2767e0

Method2: Address of array Elements

numbers[0] = 0x7ffc4d2767d0

numbers[1] = 0x7ffc4d2767e4

numbers[2] = 0x7ffc4d2767f8

numbers[3] = 0x7ffc4d27680c

numbers[4] = 0x7ffc4d276820

Address printed using Method1 and Method2 are same since name of the array itself a pointer and adding 1 to it gives second element address and 2 to it gives third element and so on similarly applying address operator over the array elements provides the direct address of the element.

4.

printf("Length(numbers) = %lu\\",sizeof(numbers)/sizeof(numbers[0]));

Dividing an size of array by size of individual array elements gives length of an array

Length(numbers) = 5

5.

Yes even if numbers are passed to a function sizeof can be applied since numbers is starting address of an array and to pass an array as argument its address passed.

User TimonNetherlands
by
4.2k points