C Programming - Arrays - Discussion

3. 

In C, if you pass an array as an argument to a function, what actually gets passed?

[A]. Value of elements in array
[B]. First element of the array
[C]. Base address of the array
[D]. Address of the last element of array

Answer: Option C

Explanation:

The statement 'C' is correct. When we pass an array as a funtion argument, the base address of the array will be passed.


Ganesh said: (Aug 14, 2011)  
Since arrays name represent its base adress.

Shakil said: (Aug 24, 2011)  
Can anyone exaplin this in detail please.

Sameera said: (Jan 28, 2012)  
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.

Poonam said: (Jun 28, 2012)  
What is base address ?

Srilakshmi said: (Jul 7, 2012)  
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.

Mayank said: (Aug 22, 2012)  
What is difference in base address and first element address?

Suraj said: (Aug 23, 2012)  
@Mayank....
Both r same dude.

Sailusha said: (Aug 29, 2012)  
Thanks Sameera.

Sri Ram said: (Aug 30, 2012)  
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.

Sri Ram said: (Aug 30, 2012)  
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.

Sricharan said: (Sep 1, 2012)  
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

Praveena said: (Jan 23, 2013)  
What is first element address?

Ravneet said: (Jan 24, 2013)  
@Poonam.

Base address is the address of list element of the array.

If we pass only name of the Array to a function, it basically means that The base address is passed.

Promila said: (Jun 28, 2013)  
Base address of the array and array passing by reference are same or different.

Vini said: (Oct 23, 2013)  
What is meant by first element?

Sayantani said: (Mar 3, 2014)  
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 ?

Tanu said: (Mar 10, 2014)  
Base address same as first address of array element?

Rajab said: (Apr 1, 2014)  
Write a program that reads values into an array from a file and sorts the data and writes it in a file.

Help me out with this.

Manisha said: (Jun 20, 2014)  
Find out the value of array.

Diksha said: (Jun 26, 2014)  
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.

Shikha said: (Mar 31, 2015)  
Base address is the first element of the memory locations.

Ashish said: (May 17, 2015)  
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?

Vignesh P said: (Sep 14, 2015)  
Please answer any one, What is an argument in a array element?

Ch.Nagalaxmi said: (Jul 27, 2016)  
How to write with the logic base?

Raji said: (Sep 11, 2016)  
Please say the difference between the base address and first element?

As of my knowledge base address is nothing but an address of the first element.

Am I right?

Suganthy said: (Sep 29, 2016)  
I also guess the same @Raji.

Pranjali said: (Nov 26, 2016)  
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.

Jyoti said: (Mar 14, 2017)  
I'm confused in either A or C can anyone explain?

Reetika Chaudhary said: (Jan 15, 2018)  
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])
}

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.