1

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.

4
  • 1
    Have you tried using a debugger? Commented Feb 5, 2013 at 22:45
  • You're passing in to the saisie function a reference to the space in memory where the 10th element of your int array would be, if it were 10 elements long; modify it so you pass in the array itself saisie(B) Commented Feb 5, 2013 at 22:46
  • Yes, the debug included in code blocks but he didn't find errors and warning ... Commented Feb 5, 2013 at 22:48
  • 1
    Actually, what is the error message? Commented Feb 5, 2013 at 22:48

3 Answers 3

4

Expanding on @Miguel Prz's answer, if you wanted to start with the first element, then you need to pass a reference to the first element in the array: saisie_B_M(&B[0]) or you could just use saisie_B_M(B).

Sign up to request clarification or add additional context in comments.

Comments

4

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.

Comments

2

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++) {
/* ... */
}

1 Comment

Ignoring the spelling problems, you first statement is correct but your solution ignores what the OP is looking to do.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.