0

Firstly I should say that I have very little C experience, and when I say very little I mean about 2 1/2 hours. So please forgive and correct any inaccuracies, stupidities, or other personal failings.

Here is the code as it currently stands:

typedef struct
{
    float n;
    int x;
    int y;
    int values[5];
} Cell;

typedef Cell Grid[10][10];

void update(Grid *source)
{
    // This should be a 2D array of Cells.
    // All the values in the Cell should be 0,
    // including the contents of the values array.
    Grid grid;
}

Update will be called fairly frequently and is somewhat performance critical, so I am willing to sacrifice some readability/simplicity/coding time if required for the sake performance. No, this is not premature optimisation.

Thanks for any help,

Sam.

2 Answers 2

2

The easiest and fastest way would be to memset the array:

memset(grid, 0 sizeof(Cell)*10*10);

Actually size of grid is known at compile time so

memset(grid, 0, sizeof(Grid));

should be enough.

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

1 Comment

Thanks, this seems to be the best answer to the question and I will mark it as such. As it happens I had already tried this and concluded that it wasn't working, but upon further investigation it appears that the erroneous behaviour the code is exhibiting is from another, as yet unknown source. I'll continue investigation independently for now.
1

This will initialize your array.

Grid grid={0};

2 Comments

The trouble with that is that some versions of GCC under some commonly used levels of warning (such as -Wall) will complain about 'incomplete initializers'. It shouldn't, but it does.
@JonathanLeffler yes. I tried with couple of compilers. I worked fine. But yes as you said, it should not give warning.

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.