Question

a) (C language) Let’s consider an integer array of size 10. (10 marks, each part is...

a) (C language) Let’s consider an integer array of size 10. (10 marks, each part is 2 marks)
int a[10];
I. How would you assign a pointer, called pA, to store the address of element 0 of this array? Write
the C code for your answer.
II. Using pA, how would you obtain the value of the next element (element 1) of the array?
III. Explain in 2-3 sentences what the statement pA = a[1]; would do.
IV. Is the statement a = pA; valid (would it cause compilation errors)? How about a++;? Explain
briefly in 3-4 sentences.
V. Write a C code to print all the elements from this array.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Solution:

So, we have an array int a[10];

I. How would you assign a pointer, called pA, to store the address of element 0 of this array? Write
the C code for your answer.

=> int *pA= &a[0];

II. Using pA, how would you obtain the value of the next element (element 1) of the array?

=> pA++; //by just incrementing the pointer by one the pointer will start to point at the next element value of the array a.

*pA is the value of the element at 1 position in the array a.

III. Explain in 2-3 sentences what the statement pA = a[1]; would do.

=> The statement will store the element value in the variable pA, but if pA is a pointer variable then there will be an error at compile time saying cannot assign int to an address.

IV. Is the statement a = pA; valid (would it cause compilation errors)? How about a++;? Explain
briefly in 3-4 sentences.

=> Yes, the statement is valid.

because a is also a pointer pointing to the base element of array a which is a[0], and pA is also of pointer type.

a++ will assign the address of a[1] to the pointer variable a.

V. Write a C code to print all the elements from this array.

#include <stdio.h>

#include<conio.h>

int main()

{

int a[10]={0};

for(int i=0; i<10; i++)

{

printf("The element at %d index is= %d", i, a[i]); //printing the elements of the array

return 0;

}

Please give it a thumbs up if this helped you, also provide your valuable feedback.

Add a comment
Know the answer?
Add Answer to:
a) (C language) Let’s consider an integer array of size 10. (10 marks, each part is...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT