adimoh
adimoh

Reputation: 728

Initialize entire array with a value in C

I was trying to initialise an entire int array and after searching through Stackoverflow, i found that the best way is to use std::fill_n from <algorithm> or <vector> header but including both is giving an error. I think these methods are only possible in C++ or am I doing something wrong?

How can I initialise an entire array with a value in C without any loops i.e. in a single statement?

I am working on Fedora 14 terminal and compiling it using gcc.

Upvotes: 3

Views: 4443

Answers (7)

Richard Fung
Richard Fung

Reputation: 1020

Yes, those are C++ libraries. You can actually tell just by looking because C standard library headers end with .h while C++ standard library headers don't. For example, in C you would use <stdio.h> but in C++ you would use <cstdio>.

Upvotes: 1

olegarch
olegarch

Reputation: 3891

if you need initialize with 0 or ~0, or any byte value, populated into all bytes of your integer, you can use bzero() or memset().

Upvotes: 0

AnT stands with Russia
AnT stands with Russia

Reputation: 320421

A reusable universal solution can be built in the following fashion

  1. Write a "stable" memcpy_forward function (analog of standard memcpy) that copies memory regions in forward direction specifically

    void *memcpy_forward(void *dst, const void *src, size_t n)
    {
      char *pdst;
      const char *psrc;
    
      for (pdst = dst, psrc = src; n > 0; --n, ++pdst, ++psrc)
        *pdst = *psrc;
    
      return dst;
    }
    
  2. To fill an array like

    int a[1000];
    

    with the same value (say, 42) first do

    a[0] = 42;
    

    and then do

    memcpy_forward(a + 1, a, sizeof a - sizeof *a);
    

This trick can be used to fill any memory region with repetitive pattern of any size. You start by forming the initial "sample" of the pattern at the beginning of the region and then use memcpy_forward to replicate that pattern across the desired region.

Upvotes: 1

user405725
user405725

Reputation:

std::fill_n is indeed an algorithm from C++ that you cannot use in C.

Bear in mind that fill_n does not initialize the array, it assigns a specified value to each an every element of the array.

To do a similar thing in C, you have to iterate over elements of the array and assign a value to each and every one. For example:

// Compile: gcc -std=c99 -Wall -pedantic -o test ./test.c

int main()
{
    double array[100];
    unsigned int i;

    for (i = 0; i < sizeof(array)/sizeof(array[0]); ++i)
        array[i] = 9.99;
}

If your array is a byte string, then using memset may be a better choice — developers are familiar with it so the code would be more compact, and it may also be faster. For example:

// Compile: gcc -std=c99 -Wall -pedantic -o test ./test.c
#include <string.h>

int main()
{
    char array[100];
    memset(array, 1, sizeof(array)); // Set every element to 1.
}

If you want to initialize instead of doing assignment, then you need to either:

  • Use zero-initializer to set each element to 0: int array[100] = { 0 };
  • Specify each element's value: int array[] = { 1, 2, 3 };

Hope it helps. Good Luck!

Upvotes: 1

adimoh
adimoh

Reputation: 728

So I believe the conclusion is that there is no pre-defined way to initialise entire array using an expression and that std::fill_n is only possible in C++.

The only way is the brute force for loop.

Upvotes: 0

ldav1s
ldav1s

Reputation: 16305

The most general way is to write a loop to initialize:

for (i = 0; i < some_len; ++i) {
    ary[i] = some_val;
}

If you just need 0, memset or initialization will work. But I'm guessing you're looking for arbitrary values. The memset call can fill your array with arbitrary patterns of a single byte, which probably isn't what you're after.

Upvotes: 2

Jashaszun
Jashaszun

Reputation: 9270

If you have your array declaration int arr[ARRAY_SIZE];, then you can fill it with an int VALUE with the following code (the declaration of i needs to be at the beginning of a block).

int i;
for (i = 0; i < ARRAY_SIZE; i++)
   arr[i] = VALUE;

A simple for loop will do.

Upvotes: 5

Related Questions