I think so. Especially when I learned C language and started to get into shape. (Experience story) That's why ** you can do that with pointers **
You're allergic to pointers. Especially after learning C language, I became able to do it a little and got better (ry)
However, the array and the pointer are inseparable. I have to get the pointer bitten a little. ..
I'm sure some people are allergic to pointers, but I would like to explain so that they do not have allergies.
--Suppose that the score of a certain test of a certain class is stored in an array for 10 pieces. ――Suppose you want to create a program that gives the average, maximum, and minimum of that score.
This time, only one array is prepared, but even if there are multiple arrays, there is no difference, just pass the name of the array as an argument.
For the time being, if you want to pass an array, please send me the name of the array
main.c
#include <stdio.h>
#define num 10
int main(void)
{
int ten[num]={12,15,91,56,43,72,66,100,99,83};
heikin(ten);
maxmin(ten);
/*Ten at the beginning of the array[0]Address is passed to the function
num[3]If you do, the start address where the fourth 56 is stored will be passed.*/
/*This time ten[0]Since we are assuming 10 pieces from, if ten[3]When processing 10 pieces from
Please note that it will process up to the unexpected*/
return 0;
}
I want to use a pointer, so add int * arr * at first! !! !! Is there a hole in the butt! !! !! ?? ?? ?? ?? So, if you want to use the contents of that, * put it on! !! !!
Then, In arr ++, the address is increased by one, so it is the same as shifting the elements of the array by one. ** If you want to shift the arrangement, do so! !! !! ** **
main.c
void heikin(int *arr){//Arr can be used as the start address of the array
int i;
double sum=0;
for( i = 0; i < num; i++ ){
sum += *arr;//Since arr is an address, the value of its contents is used.
arr++;//Increment the address by one
}
printf("average:%lf.\n", (sum/num) );
}
** If you want to use the contents * Put it on! !! !! ** **
main.c
void maxmin(int *arr){
int i, max=*arr, min=*arr;//Set the contents of the first address as the initial value.
for( i=0; i < num; i++ ){
if( *arr < min ){
min = *arr;
}
else if( max < *arr){
max = *arr;
}
arr++;
}
printf("max:%d\nmin:%d\n",max,min);
}
main.c
#include <stdio.h>
#define num 10
void heikin(int *arr){
int i;
double sum;
for( i = 0; i < num; i++ ){
sum += *arr;
arr++;
}
printf("average:%lf.\n", (sum/num) );
}
void maxmin(int *arr){
int i, max=*arr, min=*arr;
for( i=0; i < num; i++ ){
if( *arr < min ){
min = *arr;
}
else if( max < *arr){
max = *arr;
}
arr++;
}
printf("max:%d\nmin:%d\n",max,min);
}
int main(void)
{
int ten[num]={12,15,91,56,43,72,66,100,99,83};
heikin(ten);
maxmin(ten);
return 0;
}
output
average:63.700000.
max:100
min:12
main2.c
#include <stdio.h>
#define num 10
int main(void)
{
int ten[num]={12,15,91,56,43,72,66,100,99,83};
double sum=0;
int i,max=ten[0],min=ten[0];
for( i = 0; i < num; i++){
if( ten[i] < min ){
min = ten[i];
}
else if( max < ten[i]){
max = ten[i];
}
sum += ten[i];
}
printf("average:%lf.\n", (sum/num) );
printf("max:%d\nmin:%d\n",max,min);
return 0;
}
output
average:63.700000.
max:100
min:12
After all, what I want to say is that the array is a pointer, and all you need is a hole in the butt. There is only one array now, but it would be difficult to reach 100. In that case, it should be made into a function, but I don't know how to do it ... I understand well. I didn't understand either. It's okay, just add more things you can do. I would be grateful if you could use this page to help.
Postscript There is a great source in the comments section, so be sure to read it. ..
Recommended Posts