0

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?

6
  • 6
    sample(a); and remember your dominant index is ignored in the array parameter decl of the function, so be careful. Commented Oct 24, 2013 at 6:21
  • Welcome to StackOverflow. When posting questions like this you should try to include details of what you have tried and what problems you encountered, e.g. in this specific case you should have included what you tried and what error you received. Commented Oct 24, 2013 at 6:28
  • Is there any particular reason why you are using a VLA? Commented Oct 24, 2013 at 6:35
  • I was passing array iteself in sample(a[3]) , which gave me an invalid argument Commented Oct 24, 2013 at 6:36
  • Its just random things i am trying during my learning process Commented Oct 24, 2013 at 6:37

5 Answers 5

2

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.

Sign up to request clarification or add additional context in comments.

Comments

1
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 


  }

Comments

0

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);
  }

1 Comment

The code you say "won't work" will compile fine and run fine. It would also work in its previous form with void sample(int b[SIZE]).
0

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.

}

Comments

0

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;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.