Home > Back-end >  Novice C language problem: how to pass an array of variable length between function?
Novice C language problem: how to pass an array of variable length between function?

Time:01-16

I haven't written a C , I have to do now is: in the function 1 defines a variable length arrays, and then back to the main function, the function USES the array in the 2,
I wrote a program, a simple two problems:
(1) using the malloc defines the variable-length array arr, can not access the arr [1] arr [2] and other elements, can only access arr [0],
Pass into the function value of 2 (2) is completely wrong, seems to be random values,
Master looked glad, thank you!

#include
#include

Struct array
{
int n;
Int * arr;
};

The static struct array * func1 ();
Static int func2 (struct array *);

Struct array * func1 ()
{
Struct array * p1;
Struct array p1_t;
Int I, n;

N=16;//assume n will change -based on user input
P1=& amp; P1_t;
P1 - & gt; N=n;
P1 - & gt; Arr=(int *) malloc (sizeof (int) * n);
For (I=1; I & lt;=n; I++) p1 - & gt; Arr (I - 1]=I;

Return p1;
}

Int func2 (struct array * p2)
{
Int I, n;

N=p2 - & gt; n;
For (I=1; I & lt;=n; I++) p2 - & gt; Arr=n + 1 - [I - 1] I;

return 1;
}

Int main ()
{
Struct array * p;
Int I, n;

P=func1 ();
N=p - & gt; n;
For (I=1; I & lt;=n; I++) printf (" % d ", p - & gt; Arr] [I - 1);
Printf (" after func1. \ n ");
Func2 (p);
For (I=1; I & lt;=n; I++) printf (" % d ", p - & gt; Arr] [I - 1);
Printf (" after func2. \ n ");

return 1;
}

CodePudding user response:

 # include & lt; stdio.h> 
#include

Struct array
{
int n;
Int * arr;
};

The static struct array * func1 ();
Static int func2 (struct array *);

Struct array * func1 ()
{
# if 0
Struct array * p1;
Struct array p1_t;
Int I, n;

N=16;//assume n will change -based on user input
P1=& amp; P1_t;
P1 - & gt; N=n;
P1 - & gt; Arr=(int *) malloc (sizeof (int) * n);
For (I=1; I & lt;=n; I++) p1 - & gt; Arr (I - 1]=I;
Return p1;//returns the p1_t address, the address is the address of the local variable, according to a local variable life cycle is this function, so the return of p1 is invalid, also lead to memory leaks, because malloc didn't release,
# the else
Struct array * p1;
Int n, I;

P1=(struct array *) malloc (sizeof (struct array));
if (! (p1)
exit(0);

N=16;
P1 - & gt; N=n;
P1 - & gt; Arr=(int *) malloc (sizeof (int) * n);
if (! P1 - & gt; Arr) {
Free (p1);
exit(0);
}
For (I=1; i<=n; I++)
P1 - & gt; Arr (I - 1]=I;

Return p1;

# endif
}

Int func2 (struct array * p2)
{
Int I, n;

N=p2 - & gt; n;
For (I=1; I & lt;=n; I++)
The p2 - & gt; Arr=n + 1 - [I - 1] I;

return 1;
}

Int main ()
{
Struct array * p;
Int I, n;

P=func1 ();
N=p - & gt; n;
For (I=1; I & lt;=n; I++)
Printf (" % d ", p - & gt; Arr] [I - 1);
Printf (" after func1. \ n ");
Func2 (p);
For (I=1; I & lt;=n; I++)
Printf (" % d ", p - & gt; Arr] [I - 1);
Printf (" after func2. \ n ");

Free (p - & gt; Arr);
free(p);

return 1;
}

For your reference ~

CodePudding user response:

I'm thinking about you and this is not an array variable but linked list, you can find out how to write list
  • Related