james
james

Reputation: 221

Accessing an array present in main in a function

I have this code:

#include <stdio.h>

void sample(int b[3])
{
    //access the elements present in a[counter].
    for(int i=0;i<3;i++)
        printf("elements of a array are%d\n",b[i]);
}        

int main()
{
    int count =3;
    int a[count];
    int i;
    for(i=0;i<count;i++)
    {
        a[i]=4;
    }

    for(i=0;i<count;i++)
    {
        printf("array has %d\n",a[i]);
    }
    sample(//pass the array a[count]);

}

I want to access the array declared in this main function in a user defined function outside main() by passing it as parameter of this function. How can I do this?

Upvotes: 0

Views: 104

Answers (5)

Sahil Yadav
Sahil Yadav

Reputation: 69

To pass a complete array to a function you need to pass its base address i.e.&a[0] and its length. You can use the following code:

#include<stdio.h>
#include<conio.h>
void sample(int *m,int n)
{
 int j;
 printf("\nElements of array are:");
 for(j=0;j<n;j++)
 printf("\n%d",*m);
}
int main()
{
int a[3];
int i;
for(i=0;i<3;i++);
{
   a[i]=4;
}
printf("\nArray has:");
for(i=0;i<3;i++)
{
    printf("\n%d",a[i]);
 }
sample(&a[0],3)
getch();
return 0;
}

Upvotes: 0

Anurag
Anurag

Reputation: 143

Arrays are always passed as reference. You need to pass address of array to actual parameters and accept it using pointer in formal parameter. Below code should work for you.

void sample(int *b)     //pointer will store address of array.
{

     int i;
     for(i=0;i<3;i++)
         printf("elements of a array are%d\n",b[i]);
}        

int main()
{
    int count =3;
    int a[count];
    int i;
    for(i=0;i<count;i++)
{
    a[i]=4;
}

for(i=0;i<count;i++)
{
    printf("array has %d\n",a[i]);
}
sample(a);    //Name of array is address to 1st element of the array.

}

Upvotes: 0

Gangadhar
Gangadhar

Reputation: 10516

sample(a); //pass beginning address of array is same as sample(&a[0]);

Function declaration

  void sample(int b[]);

Function definition

  void sample(int b[]) // void sample(int *b)
  {  
      //access the elements present in a[counter].
      //You can access  array elements Here with the help of b[0],b[1],b[2]
      //any changes made to array b will reflect in array a
      //if you want to take SIZE into consideration either define as macro or else declare and define function with another parameter int size_array and From main pass size also 


  }

Upvotes: 1

Vallabh Patade
Vallabh Patade

Reputation: 5110

pass the parameter as sample(a);

However this code will not work. You cannot use a variable to pass as size of array.

   #include<stdio.h>
   #define SIZE 3
   void sample(int b[]) {
      //access the elements present in a[counter] .
      for(int i=0;i<3;i++){
          printf("elements of a array are%d\n",b[i]);
      }        
   }

   int main() {
   int a[SIZE];
   int i;
   for(i=0;i<SIZE;i++){
       a[i]=4;
   }

   for(i=0;i<SIZE;i++){
       printf("array has %d\n",a[i]);
   }
   sample(a);
  }

Upvotes: 0

tangrs
tangrs

Reputation: 9930

The function expecting it usually has to know where the array is and the size of it. To do that, you'd pass a pointer to the first element of the array.

Your sample function could look like

void sample(int *b, size_t count) {
    for(int i = 0; i < count; i++) {
        printf("elements of a array are%d\n",b[i]);
    }  
}

You can 'pass' the array by passing a pointer to its first element and of course, also pass the length of the array.

sample(a, count);

You could also simplify this by omitting the count parameter if you can be sure the array will be at least 3 element long.

Upvotes: 2

Related Questions