2014-11-25 66 views
0

我试图搜索这个问题,但我无法解决它。我试图使用结构和联盟,但我认为我没有正确使用它。请求会员`结果'的东西不是结构或工会

我想在另一个函数中使用数组resultrandomNum()的成员。什么是最好的方式来做到这一点?

这里是我的一些代码:

void randomNum() 
{ 
    int i; 
    int result[10]; 
    int x[10]; 
    int y[10]; 
    for (i=0; i<11; i++) 
    { 
     x[i]= 100+rand()%100; 
     y[i]= 80+rand()% 90; 
     result[i] = (-2*(x[i])) + (5*(y[i])); 
     printf("Fitness = %d \n" , result[i]); 
    } 
} 

我想在这个函数中使用result[10]

void thebest() 
{ 
    printf("Fitness = %d \n" , randomNum.result[2]); 
} 
+1

如果你想用'结果[10]',那么你最好声明你的阵列'INT结果[11]'! ! C数组索引是[基于零](http://en.wikipedia.org/wiki/Zero-based_numbering) – FoggyDay 2014-11-25 04:16:51

+1

如果你想在randomNum()之外使用“result []”,那么你最好声明它“randomNum()”之外。 – FoggyDay 2014-11-25 04:29:41

+0

你的数组的上下文应该是全局的,然后你可以使用它是多个函数。你说过让它在全球化后出现了一些问题,它是什么? – Gopi 2014-11-25 04:49:10

回答

0

你不能从一个函数外部访问本地值。相反,请尝试在最后更改randomNumreturn result[2],并将签名中的void更改为int

或者,你可以让resultthebest函数内部变量,然后把它传递给randomNum被填充,那么你可以使用它作为thebest规则排列。

但是从randomNum函数返回您感兴趣的特定项目可能是最简单和最干净的解决方案。

另外,当您拨打randomNum时,请不要忘记括号:randomNum()而不是在使用点。

+0

这个数组可以做成'global'吗? – Gopi 2014-11-25 04:22:05

+0

是的,但这也会有点奇怪,尤其是如果在一个表达式中调用两次函数 – 2014-11-25 04:37:08

+0

我试图将它用作全局函数,但事情是我不能在第二个函数中使用第一个函数的变量的相同值。 – 2014-11-25 04:43:35

0

你可以做这样的:

void randomNum(int *result) 
{ 
    // same code except don't declare result[10] 
} 

void thebest() 
{ 
    int result[11]; 

    randomNum(result); 
    printf("Fitness = %d \n" , result[2]); 
} 
0

您试图访问或更改randomNum.result []的成员不存在。 array的索引array_name [index]从0到index - 1。因此,在for循环中,您需要将它更改为i < 11,因为它是下面的for循环。

for (i=0; i<10; i++) 
{ 

      x[i]= 100+rand()%100; 
      y[i]= 80+rand()% 90; 
      result[i] = (-2*(x[i])) + (5*(y[i])); 
      printf("Fitness = %d \n" , result[i]); 

} 

当试图访问它需要数组:

void thebest() 
{ 
    printf("Fitness = %d \n" , randomNum.result[9]); 

} 
+0

这是我的问题,我不能用这种方式 “randomNum.result [9]” 它给出了相同的按摩! – 2014-11-25 04:55:26

+0

我明白你在做什么。您不能使用带点运算符的函数来访问该函数本地的变量。你将不得不用这个名字来构造一个结构,要么你在结构中要么在main中使用x和y数组。你也必须调用该函数才能使用它。 – 2014-11-25 06:37:15

+0

如果你想我可以做出另一个答案,显示我的意思 – 2014-11-25 06:37:40

0

C函数不上课般的成员,因此,您无法访问randomNum.result[2]你的方式。还有一些其他的方法,但:

调用者提供的缓冲区:

调用函数可以提供由randomNum()填写的缓冲区。这可能是最简单的解决方案:

void randomNum(int result[10]) 
{ 
    int i; 
    /* int result[10]; // Converted into a function parameter. */ 

    ... 
} 

void thebest() 
{ 
    int r[10]; 

    randomNum (r); 
    printf ("Fitness = %d \n", r[9]); 
} 

这样,thebest()函数数组分配存储,并传递一个指针randomNum()。无法将数组直接传递给函数,因此将randomNum()定义中的数组参数静默地调整为指向int,并且函数调用randomNum (r)将传递指向r的第一个元素的指针。


返回一个指向静态存储:

如果您要访问内部randomNum()result变量,可以从函数返回。与上面的例子类似,您不能直接从函数返回数组。您必须将指针返回给阵列。但是,当它定义的函数返回时,局部变量不再存在。为了解决这个问题,你可以定义这个变量,使其具有static存储。这意味着该变量贯穿整个程序的执行过程。它有缺点,两个调用函数将在变量相同的变量,因此覆盖以前的数据。

/* Define randomNum to return pointer of int */ 

int *randomNum(void) 
{ 
    int i; 
    static int result[10]; /* Changed to static */ 

    ... 

    return result; 
} 

void thebest() 
{ 
    printf("Fitness = %d \n" , randomNum()[2]); 
} 

返回一个指向分配存储:

为了避免使用本地静态缓冲区的缺点,你可以使用动态分配的缓冲区。这会在每次调用时返回一个新的缓冲区。请注意,分配可能会失败(返回NULL),并且指针必须在free()之后。

int *randomNum(void) 
{ 
    int i; 
    int *result; /* Changed to a pointer */ 

    result = malloc (10 * sizeof *result); 
    /* Allocate 10 elements of whatever result points to */ 

    if (result) { 

    ... 

    } 

    return result; 
} 

void thebest() 
{ 
    int *r = randomNum(); 

    if (r) { 
    printf("Fitness = %d \n" , r[2]); 
    } 

    free (r); 
} 

返回一个结构内的数组:

好的,我撒谎之前:有传递和返回一个数组,并从功能的方式。您可以将其包装在struct中。这有一个缺点,在某些实现中,传递大型结构可能比传递指针复杂得多。

struct result_t { 
    int result[10]; 
}; 

struct result_t randomNum(void) 
{ 
    int i; 
    struct result_t r; 

    ... 

    r.result[i] = (-2*(x[i])) + (5*(y[i])); 

    ... 

    return r; 
} 

void thebest() 
{ 
    struct result_t res; 
    res = randomNum(); 
    printf("Fitness = %d \n" , res.result[2]); 
} 

或:(如果你的编译器符合最近的C类标准)

void thebest() 
{ 
    printf("Fitness = %d \n" , randomNum().result[2]); 
} 
0

谢谢你们的帮助

我弄明白:

我需要在main中声明结果[],然后将它传递给两个函数:

main(){ 
int result[10]; 


randomNum(result); 
thebest(result); 


} 

及格成绩[]的函数()

void randomNum(int result[]) 
     { 
     ... 

     } 


    void thebest(int result[]) 
    { 

    ... 
     } 
相关问题