|
INTERVIEW QUESTIONS
COMPUTER HARDWARE
EMBEDDED SYSTEMS
DETAILS
Question: Why cannot arrays be passed by values to functions?
Answer: Arrays can't be passed by values. Because , the array name is evaluated to be a pointer to the first element of the array. e.g. when we pass array x, its equivalent to &x[0] i.e. pointer to the first element. Its type is, therefore, int *, and a called function uses this pointer (passed as an argument) to indirectly access the elements of the array. e.g . int main() { void function1(int A[],int n); int x[10],i; for(i=0;i<10;i++) { x[i] = 10; } function1(x,10); } void function1(int A[],int n) { ..../* some processing on the array */ }
In the above example,prototype shows the first formal parameter as an integer array without specifying the size. In C, this syntax is interpreted as a pointer variable; so array A is declared as an int * variable.
This is the unique feature of C is that array access is always indirect; thus making it particularly easy for a called function to indirectly access elements of an array and store or retrieve values.
Submitted by Manisha Savale ([email protected])
________
Because in C when you say the name of the array it means the address of the first element. example : int a[]; func (a); int func(int a[]);
In this when you call the function by passing the argument a actually &a[0](address of first element) gets passed. Hence it is impossible to pass by value in C.
Submitted by Pooja Agrawal ([email protected])
|
|
|
Category |
Embedded Systems Interview Questions & Answers -
Exam Mode /
Learning Mode
|
Rating |
(0.3) By 7338 users |
Added on |
5/21/2011 |
Views |
74827 |
Rate it! |
|
|
Question:
Why cannot arrays be passed by values to functions?
Answer:
Arrays can't be passed by values. Because , the array name is evaluated to be a pointer to the first element of the array. e.g. when we pass array x, its equivalent to &x[0] i.e. pointer to the first element. Its type is, therefore, int *, and a called function uses this pointer (passed as an argument) to indirectly access the elements of the array. e.g . int main() { void function1(int A[],int n); int x[10],i; for(i=0;i<10;i++) { x[i] = 10; } function1(x,10); } void function1(int A[],int n) { ..../* some processing on the array */ }
In the above example,prototype shows the first formal parameter as an integer array without specifying the size. In C, this syntax is interpreted as a pointer variable; so array A is declared as an int * variable.
This is the unique feature of C is that array access is always indirect; thus making it particularly easy for a called function to indirectly access elements of an array and store or retrieve values.
Submitted by Manisha Savale ([email protected])
________
Because in C when you say the name of the array it means the address of the first element. example : int a[]; func (a); int func(int a[]);
In this when you call the function by passing the argument a actually &a[0](address of first element) gets passed. Hence it is impossible to pass by value in C.
Submitted by Pooja Agrawal ([email protected]) Source: CoolInterview.com
If you have the better answer, then send it to us. We will display your answer after the approval.
Rules to Post Answers in CoolInterview.com:-
- There should not be any Spelling Mistakes.
- There should not be any Gramatical Errors.
- Answers must not contain any bad words.
- Answers should not be the repeat of same answer, already approved.
- Answer should be complete in itself.
|
|
Related Questions |
View Answer |
|
Can structures be passed to the functions by value?
|
View Answer
|
Please Note: We keep on updating better answers to this site. In case you are looking for Jobs, Pls Click Here Vyoms.com - Best Freshers & Experienced Jobs Website.
View All Embedded Systems Interview Questions & Answers - Exam Mode /
Learning Mode
|