9

I created a dynamic array ,and i need to initialize all the members to 0. How can this be done in C?

   int* array;
    array = (int*) malloc(n*sizeof(int));

5 Answers 5

21

In this case you would use calloc():

array = (int*) calloc(n, sizeof(int));

It's safe to assume that all systems now have all zero bits as the representation for zero.

§6.2.6.2 guarantees this to work:

For any integer type, the object representation where all the bits are zero shall be a representation of the value zero in that type.

It's also possible to do a combination of malloc() + memset(), but for reasons discussed in the comments of this answer, it is likely to be more efficient to use calloc().

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

8 Comments

It's more than "safe to assume", I'd say that it's actually guaranteed by §6.2.6.2.
@Mysticial it is guaranteed by the standard (C99, 6.2.6.2p5) "For any integer type, the object representation where all the bits are zero shall be a representation of the value zero in that type."
Yes, you are right, I found the line that says this. Adding to my answer.
Value bits => "Each bit that is a value bit shall have the same value as the same bit in the object representation of the corresponding unsigned type"; Sign bits => "If the sign bit is zero, it shall not affect the resulting value." Padding bits aren't relevant.
The cast is unnecessary too in C.
|
7
memset(array, 0, n*sizeof(int));

Or alternatively, you could allocate your block of memory using calloc, which does this for you:

array = calloc(n, sizeof(int));

calloc documentation:

void *calloc(size_t nmemb, size_t size);

The calloc() function allocates memory for an array of nmemb elements of size bytes each and returns a pointer to the allocated memory. The memory is set to zero. ...

Comments

4

Use calloc function (usage example):

int *array = calloc(n, sizeof(int));

From calloc reference page:

void *calloc(size_t nelem, size_t elsize);

The calloc() function shall allocate unused space for an array of nelem elements each of whose size in bytes is elsize. The space shall be initialized to all bits 0.

Comments

0
memset(array, 0, n * sizeof *array);

Comments

0
                    for (i=0; i<x; ++i){
                        array[i]=0;
                    }

That should do the trick. I guess you have to make a loop that makes 0 every element in the array. Memset could also work for you.

2 Comments

Hey, you do not have to write a program that is interactively proving the answer correct. Finding the one line that does the trick in this source is way harder...
@dgunchev Yes you are right but i was just writing something similar in c arm when i saw the question by chance and thought about a fast answer. :)

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.