Note that there are some explanatory texts on larger screens.

plurals
  1. POPassing variables to a function: scope and when to use & and *?
    text
    copied!<pre><code>#include "stdafx.h" #include "stdio.h" #include "math.h" #include "stdlib.h" void test (int a,int *b, int result[], int serie[]); int main() { int *serie = malloc(sizeof(int)); int result[20], a,b, i; a=0; b=0; for (i = 0; i &lt; 20; i++) { result[i]=i+10; serie[i]=rand(); printf("result is %d \n",result[i]); } test(a,&amp;b,result,serie); printf("value of a inside main %d \n",a); printf("value of b inside main %d \n",b); for (i = 0; i &lt; 20; i++) { printf("value of result inside main is %d and of serie is %d \n",result[i],serie[i]); } getchar(); return 0; } void test(int a, int *b, int result[], int serie[]) { int i; a=13; *b=14; printf("value of a inside the function %d \n",a); printf("value of b inside the function %d \n",*b); for (i = 0; i &lt; 20; i++) { result[i]=result[i]*2; serie[i]=7; printf("value of result inside the function is %d and of serie is %d\n",result[i],serie[i]); } } </code></pre> <p>Basically, all this code does is seeing the scope of the variables, I wrote it to help myself I thought as to change the value of the integer inside <code>main</code> with a function (see <code>int b</code>) You have to call it with <code>&amp;b</code> (<code>test(a,&amp;b,result,serie);</code>) and then inside the function <code>*b</code>. So I was trying those kind of operations <em>&amp;</em>* with the arrays but They did not work.</p> <p>It seems all you have to do It is write the array <code>void test(... int result[],int serie[])</code> and to call the function just put the name with no brackets at all: <code>test(...,result,serie);</code> am I right?</p> <p>What if I only want to change the arrays inside the function like with variable <code>a</code>?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload