I have a problem with this code:
main :
#include <stdio.h>
#include <stdlib.h>
#include "Function.h"
int main()
{
int B[9];
saisie_B_M(&B[9]);
return 0;
}
Function.c
void saisie_B_M(int B[9])
{
int i;
for(i=0; i<8; i++)
{
printf("Une cellule morte ayant %d voisins sera t-elle morte ou vivante à la génération suivante ? \n", i);
scanf("%d", &B[i]);
}
}
function.h
#ifndef Function_H_INCLUDED
#define Function_H_INCLUDED
void saisie_B_M(int B[9]);
#endif // Function_H_INCLUDED
The principle is simple, it is an array of 9 and I just return a value in each cell of the table. But there is a bug at the end and I don't know why the compiler does not show a message.
The problem is when you call your function with
saisie_B_M(&B[9]);
It should be
saisie_B_M(B);
In the first case, you give an array but starting at the 9th offset B[9]
. So your function will start iterating at 9th then 10th, 11th, 12th... the behaviour is undefined.
扩展@Miguel Prz
的答案,如果您想从第一个元素开始,则需要传递对数组中第一个元素的引用: saisie_B_M(&B[0])
或只使用saisie_B_M(B)
。
Arrays in C are zero-indexed, so B[9] has elements in the 0..8 range. This is not valid:
saisie_B_M(&B[9]);
if you want to pass the pointer to the last element yo need to use:
saisie_B_M(&B[8]);
but it seems you need the complete array, so pass &B[0] (or simply B) to saisie_B_M function. Also your "for" loop should be changed to this:
for(i=0; i<9; i++) {
/* ... */
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.