Boopathi T
Boopathi T

Reputation: 117

array variable inside function affects the array variable outside the function in c

my code is

#include <stdio.h>
#define MAXVAL 100
int getCharsMethod(char s[],int);

main(){
   char foo[MAXVAL];
   int len;
   len=getCharsMethod(foo,5);
   printf("value of foo is -- %s and len is %d",foo,len);

}

int getCharsMethod(char s[],int lim){
   int i;
   for(i=0;i<lim;++i)
       s[i]=getchar();

   return i;
}

now i havent inserted any value inside the array foo[]. but the values that are inserted in the array s[] inside the function is reflected in the foo[] array.

I just passed an empty foo[] array with length MAXVAL into the function getCharsMethod() how does the chars inserted in the array s[] inside the function is reflected in the foo[] array.

Upvotes: 2

Views: 115

Answers (4)

Cherubim
Cherubim

Reputation: 5457

When any array is sent from a function, the receiving argument if array; then it gets the base address.

This means,

  • both the main parameter and argument arrays hold the same address.
  • So this implicitly acts as a call by reference mechanism.
  • Finally, any changes done to argument in the function effects the main parameter.

So, Why does foo[] reflect s[]?

Here, in your code, s gets the base address of foo so this implicitly becomes as a call by reference mechanism and hence any change to s affects foo.

Upvotes: 1

unwind
unwind

Reputation: 399763

When you pass an array to a function, it (basically) just gets the address of the array's first element.

So the arrays s and foo are using the same memory, which is why writes inside the function will be seen from the outside.

One would think this has to be obvious from your code, else what would char s[] even mean? It has no length, so how can it accept storing elements using assignment to s[i] inside the function. Did you expect C to automatically create and manage a magical growing array? If so, then please read up more on the language. It doesn't work like that.

Upvotes: 6

John Bollinger
John Bollinger

Reputation: 180113

I just passed an empty foo[] array with length MAXVAL into the function getCharsMethod()

No, you didn't. In fact, you can't. In C, no function parameter or return value is an array. In almost all contexts, the value of an expression of array type "decays" to a pointer to the first element of the array, and the argument lists of functions and the value provided in a return statement are not exceptions. You passed a pointer to the first element of main()'s array foo.

Additionally, declaring a function parameter via array syntax, with or without a length, in fact declares that parameter as a pointer, of exactly the same pointer type to which an array of the nominal type given in the parameter list would decay. This is a syntactic convenience made possible by the fact that, again, function arguments cannot ever be arrays. Again, this not a prohibition, but rather a consequence of C semantics for expression evaluation.

Thus, these declarations are semantically identical:

int getCharsMethod(char s[], int lim);

and

int getCharsMethod(char *s, int lim);

and even

int getCharsMethod(char s[MAXVAL], int lim);

how does the chars inserted in the array s[] inside the function is reflected in the foo[] array.

There is no array s in function getCharsMethod(). There is only a pointer, which during your particular call points to the first element of main()'s foo. Thus, for any i between 0 (inclusive) and the length of foo (exclusive), s[i] in designates the same char in getCharsMethod() that foo[i] designates in main() -- not just the same value, but the same actual object.

Upvotes: 2

Vlad from Moscow
Vlad from Moscow

Reputation: 310950

In C arguments passed to functions by coping their values.

Consider for example

#include <stdio.h>

void f( int x )
{
    x += 10;
    printf( "Inside f x = %d\n", x );
}

int main(void) 
{
    int x = 5;

    printf( "Before call of f x = %d\n", x );
    f( x );
    printf( "After  call of f x = %d\n", x );
}   

The program output is

Before call of f x = 5
Inside f x = 15
After  call of f x = 5

That is the function deals with a copy of the value of the original variable x. The original variable x itself is not changed.

Now consider a modified program

#include <stdio.h>

void f( int *px )
{
    *px += 10;
    printf( "Inside f x = %d\n", *px );
}

int main(void) 
{
    int x = 5;

    printf( "Before call of f x = %d\n", x );
    f( &x );
    printf( "After  call of f x = %d\n", x );
}

The program output is

Before call of f x = 5
Inside f x = 15
After  call of f x = 15

In this case the argument is passed by reference that is the function gets the address of the original variable x and consequently it changes the variable.

Arrays could be also passed by value as arguments to functions. That is functions could deal with copies of arrays. But creating a copy of a whole array is a very expensive and time-consuming operation.

So in C when an array is passed to a function then its designator is implicitly converted to pointer to its first element. And correspondingly the parameter is adjusted to pointer.

For example these function declarations are equivalent and declare the same one function

void f( int a[] );
void f( int a[10] );
void f( int a[100] );
void f( int *a );

Thus the elements of the array in fact are passed by reference. You can change any element of an array the same way as it is shown in the second demonstrative program in my post.

For example

#include <stdio.h>

void f( int *a, size_t n )
{
    for ( size_t i = 0; i < n; i++ ) *( a + i ) = i;

    printf( "Inside f array a is " );

    for ( size_t i = 0; i < n; i++ ) printf( "%d ", *( a + i ) );
    printf( "\n" );
}

int main(void) 
{
    int a[] = { 0, 0, 0, 0, 0 };
    const size_t N = sizeof( a ) / sizeof( *a );

    printf( "Before call of f array a is " );
    for ( size_t i = 0; i < N; i++ ) printf( "%d ", *( a + i ) );
    printf( "\n" );

    f( a, N );

    printf( "After  call of f array a is" );
    for ( size_t i = 0; i < N; i++ ) printf( "%d ", *( a + i ) );
    printf( "\n" );
}   

The program output is

Before call of f array a is 0 0 0 0 0 
Inside f array a is 0 1 2 3 4 
After  call of f array a is0 1 2 3 4 

Upvotes: 2

Related Questions