简体   繁体   English

如何通过调用函数使用指针来编辑主数组

[英]How to use a pointer to edit array in the main through called function

I'm trying to use a random number generator function I wrote myself, but I want to call the rng in the main function and have it save the numbers generated into two arrays in the main. 我正在尝试使用自己编写的随机数生成器函数,但我想在main函数中调用rng,并将其将生成的数字保存到main中的两个数组中。 I assume I have to pass some sort of pointer into rng(); 我假设我必须将某种指针传递给rng(); to have it edit the arrays in the main? 让它编辑主数组? How to actually format that pointer? 如何实际格式化该指针? Not allowed to use strings or global arrays... 不允许使用字符串或全局数组...

so I want to put a pointer in place of r[i] so that the function edits the array in the main instead of the array in the function. 所以我想在r [i]处放置一个指针,以便该函数编辑主数组而不是函数中的数组。

do{
printf("\nthis is the row number\n\n");
for ( i = 0; i < 11; i++){
 r[i] = rand()%8;
 printf("%d\n", r[i]);
}

printf("\nthis is the column number\n\n");
for ( i = 0; i < 11; i++){
c[i] = rand()%8;
printf("%d\n", c[i]);
}

And I want this bit above to save the random numbers it makes to row[] and the second bit to column[] in the main. 我希望上面的这一位将它生成的随机数保存到主行中的row []并将第二位保存到column []中。

int main (){
int row[11];
int column[11];
rng();

The entire code is shown below 整个代码如下所示

#include <stdio.h>
#include "stdlib.h"
#include "time.h"
int * rng( ){
int x;
int *t;
x=1;
t=&x;
int y;
int *g;
y=1;
g=&y;
static int  r[11];
static int  c[11];
int i;
int j;
srand(time( NULL ) );

do{
printf("\nthis is the row number\n\n");
for ( i = 0; i < 11; i++){
r[i] = rand()%8;
printf("%d\n", r[i]);
}

printf("\nthis is the column number\n\n");
for ( i = 0; i < 11; i++){
c[i] = rand()%8;
printf("%d\n", c[i]);
}

for (i=0; i<11; i++){
for(j= i+1 ;j<11;j++){
if ((r[i]+1)*(r[i]+1)*(c[i]+1)==(r[j]+1)*(r[j]+1)*(c[j]+1)){
printf("\nOops duplicate number, recalculating\n");
*t=2;
*g=2;
printf("%i", *t);
printf("%i", *g);

}
if (((r[0]+1)*(r[0]+1)*(c[0]+1)!=(r[1]+1)*(r[1]+1)*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[10]+1)*(c[10]+1))){
printf("\nno errors\n");
*t=2;
*g=1;
printf("%i", *t);
printf("%i", *g);
break;
}
}
}
}while(*t==*g);

}




int main (){

int map[8][8];
int row[11];
int column[11];
//int *p;
int i;
// p = row[];
rng(row, column);

Instead of declaring r and c locally in your rng() function, just pass row and column to rng(row, column) by reference. 无需在rng()函数中局部声明r和c,只需通过引用将行和列传递给rng(row,column)即可。

rng (int* row, int* column){

}
void rng(int *r, int *c){
    ...
    do{
        printf("\nthis is the row number\n\n");
        for ( i = 0; i < 11; i++){
            r[i] = rand()%8;
            printf("%d\n", r[i]);
        }

        printf("\nthis is the column number\n\n");
        for ( i = 0; i < 11; i++){
            c[i] = rand()%8;
            printf("%d\n", c[i]);
        }
    ...

int main (){
    int row[11];
    int column[11];
    rng(row, column);
    ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM