C Programming - Arrays - Discussion
Discussion Forum : Arrays - General Questions (Q.No. 3)
3.
In C, if you pass an array as an argument to a function, what actually gets passed?
Answer: Option
Explanation:
The statement 'C' is correct. When we pass an array as a funtion argument, the base address of the array will be passed.
Discussion:
31 comments Page 1 of 4.
Reetika Chaudhary said:
7 years ago
If we look at the scanf() then we need to specify the (&) operator (which means address operator ) along with the array first from where we can access the elements and later on using printf() we need to pass only array elements . for eg : for reading the elements
int arr[5];
for(int i=0; i<=5;i++)
{
scanf(''%d '' , &arr[i])
}
for printing the elements
for (i=0;i<=5;i++)
{
printf(''%d'', arr[i])
}
int arr[5];
for(int i=0; i<=5;i++)
{
scanf(''%d '' , &arr[i])
}
for printing the elements
for (i=0;i<=5;i++)
{
printf(''%d'', arr[i])
}
(3)
Sayantani said:
1 decade ago
When we passing a hole array as argument like:
getArray(int formal[])
{
int i;
for(i=0;i<12;i++)
{
scanf("%d",&formal[i]);
}
return (formal)
}
main
{
int Array[12];
getArray(Array);
for(i=0;i<12;i++)
{
printf("%d\n",Array[i]);
}
getch();
}
--------------------------------------
In getarray(array); ... which technique we are using call by value or call by reference ?
getArray(int formal[])
{
int i;
for(i=0;i<12;i++)
{
scanf("%d",&formal[i]);
}
return (formal)
}
main
{
int Array[12];
getArray(Array);
for(i=0;i<12;i++)
{
printf("%d\n",Array[i]);
}
getch();
}
--------------------------------------
In getarray(array); ... which technique we are using call by value or call by reference ?
Sameera said:
1 decade ago
Take a[5], it will store address of a[0] in a. when it will access the array only by a it will increment a or it will take a+1,a+2,a+3 and so on.
When array is passed to any function we will pass as
add(a);
In definition it will take it in pointer and access whole array
Hence at the time of passing array it will pass only base address i.e. address of first element of array.
When array is passed to any function we will pass as
add(a);
In definition it will take it in pointer and access whole array
Hence at the time of passing array it will pass only base address i.e. address of first element of array.
Diksha said:
1 decade ago
When we pass the array name of a 2-d array as an argument to a function and store the address in a integer pointer in the function, the compiler shows an error "10 C:\Users\DJ\Documents\devc\pointerarray.cpp cannot convert `int (*) [4]' to `int*' for argument `1' to `void display (int*, int, int) ' ".
Compiler- DEV C.
Compiler- DEV C.
Sri ram said:
1 decade ago
If we print the array without using loops for example
printf ("%d\n", a) the output will be the address of the first element nothing but the base address.
So if we pass this array to a function as an argument it will take the base address of that array and the output will be the base address of that array.
printf ("%d\n", a) the output will be the address of the first element nothing but the base address.
So if we pass this array to a function as an argument it will take the base address of that array and the output will be the base address of that array.
Pranjali said:
8 years ago
Array elements can be passed to a function by calling the function by value or by reference.
In the call by value, we pass values of array elements to the function, whereas in the call by reference, we pass addresses of array elements to the function. So, the answer should be both A and C.
In the call by value, we pass values of array elements to the function, whereas in the call by reference, we pass addresses of array elements to the function. So, the answer should be both A and C.
(1)
Sri ram said:
1 decade ago
If we print an array without using loop like printf ("%d", array) then the first element address will be printed that is base address.
So if we pass array in a function as an argument, base address of array will be taken.
So if we pass array in a function as an argument, base address of array will be taken.
Sricharan said:
1 decade ago
Suppose int arr[]={10,20,30};
calculate base address:
treat base address as 500
arr[1]=*(arr[0]+1);
i.e,*(500+1)=*(502)
so 502 pointing to element 20.so arr[1]=20
like wise......
arr[0]=10;
arr[2]=30;
@poonam
calculate base address:
treat base address as 500
arr[1]=*(arr[0]+1);
i.e,*(500+1)=*(502)
so 502 pointing to element 20.so arr[1]=20
like wise......
arr[0]=10;
arr[2]=30;
@poonam
Srilakshmi said:
1 decade ago
When an array is declared along with required no. Of memory locations then compiler allocates extra memory location under the array name and stores address of first element ie. , base address of array.
Ashish said:
1 decade ago
main( )
{
int i,a=2,b=3;
int arr[2+3];
for(i=0;i<a+b;i++)
{
scanf("%d",&arr[i]);
printf("\n%d",arr[i]);
}
}
Can you tell me the output of this program?
{
int i,a=2,b=3;
int arr[2+3];
for(i=0;i<a+b;i++)
{
scanf("%d",&arr[i]);
printf("\n%d",arr[i]);
}
}
Can you tell me the output of this program?
(2)
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers