Home > Back-end >  Pointer to learn, you can help me to look at it
Pointer to learn, you can help me to look at it

Time:09-23

Write function fun, defined as: void fun (int * a, int n)

Function is: will a referred to in the first half of the array elements and parameters are in the second part element value exchange, the number of data in the array in the parameter n, if n is odd, is in the middle of the elements, for example, if a referred to an array of data in the order: 1,4,3,2,5,6,8,7,9, after changing as: 6,8,7,9,5,1,4,3,2,

The main function is defined in 10 the length of the array, the input of n (n<=10) represents the number of array elements of concrete, calls the fun function and output results,


#include

Void fun (int * a, int n) {
Int I, p [10].
A=& amp; P;
{if (n % 2==0)
for (i=0; i [I]=p * (a + I + n/2);
}
For (I=n/2; i [I]=p * (a + I - n/2);
}

}
return p;

}

Int main () {
Int n, I, p [10];
Int * a=& amp; P;
Printf (" input tuple element number: ");
Scanf_s (" % d ", & amp; N);
Printf (" input tuple element: ");
for (i=0; i Scanf_s (" % d ", & amp; P [I]);

Fun (a, n);
for (i=0; i Printf (" % d ", p [I]);
return 0;

}

CodePudding user response:

You read the p [10] defined as a global variable, the function defined in the p [10] deleted, so can't return

CodePudding user response:

Don't return to a local variable, or use a global variable, or made into a pointer, the incoming buffer address assignment within the function

CodePudding user response:

refer to the second floor 21211321 response:
don't return to a local variable, or use a global variable, or made into a pointer, the incoming buffer address, inside the function to assign a value

What specific how to operate?

CodePudding user response:

Like the way you use global variables, that is outside the function definition: int p={0} [10]; Then function and don't define int p [10], directly in line, equivalent to outside the function to add one line of code, you reduce one line of code inside,

See you should be the basis of the pointer is not very familiar with, not method 2 is that the second method is simple, you do the name of an array parameter incoming line, function inside don't need to define an array, and use directly, this you do online check array function parameters,

CodePudding user response:

Void fun (int * a, int n)
{
Int I, * p;
int temp;
If (n % 2==0)
{
For (p=a; P & lt; A + n/2; P++)
{
Temp=* p;
* p=* (p + n/2);
* (p + n/2)=temp.
}
}
The else
{
For (p=a; P & lt; A + n/2; P++)
{
Temp=* p;
* p=* (p + n/2 + 1);
* (p + n/2 + 1)=temp;
}
}
return p;
}

CodePudding user response:

 # include & lt; Stdio. H> 


The static void swap (int * a, int * b)
{
Int TMP=* a;
*=* b;
* b=TMP;
}
Void fun (int * a, int n)
{
//int I, p [10];
Int I, * p=a, flag.

If (n % 2==0)
Flag=0;
The else
flag=1;
for (i=0; i Swap (& amp; [I], p & amp; P/n/2 + I + flag]);
# if 0
//a=& amp; P;
{if (n % 2==0)
for (i=0; i [I]=p * (a + I + n/2);
}
For (I=n/2; i [I]=p * (a + I - n/2);
}

}
//return p;
# endif

}

Int main ()
{
Int n, I, p [10];
//int * a=& amp; P;
Int * a=p;
Printf (" input tuple element number: ");
Scanf_s (" % d ", & amp; N);
Printf (" input tuple element: ");
for (i=0; i Scanf_s (" % d ", & amp; P [I]);

Fun (a, n);
for (i=0; i Printf (" % d ", p [I]);
Putchar (10);
return 0;

}

For your reference ~
  • Related