0

I found the similar question but it is for GCC compiler and I am using clang. I want to intialize the whole array with integer 1(C++ std=17).

std::cout<<a[5] ; // I want it to print 1
Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
Hmm
  • 23
  • 1
  • 7
  • I have intialized int a[100]; – Hmm Dec 01 '21 at 09:37
  • 3
    The `fill_n` approach works also with clang, it's part of the standard library, not a compiler extension. – mch Dec 01 '21 at 09:41
  • *but it is for GCC compiler and I am using clang.* -- That makes no difference. Any conforming C++ 11 compiler has `std::fill_n`. That includes g++, clang, Visual C++, and any other compiler that purports to be standard. Did you actually try the solution in the duplicate link, or just assumed it wouldn't work? – PaulMcKenzie Dec 01 '21 at 09:44
  • 1
    When picking tags for your question, please *read their description*. The [tag:compiler-construction] tag, for example, explicitly explains that it shouldn’t be used here. – Konrad Rudolph Dec 01 '21 at 09:45
  • @PaulMcKenzie Pretty sure they were talking about the GCC designated initialisers extension that lets you write `int x[10] = {[0...9] = 1};`. – Konrad Rudolph Dec 01 '21 at 09:47
  • @KonradRudolph Yes, but the link in the post clearly has `std::fill_n` as the top rated answer. Why the OP would skip over that answer is a mystery. – PaulMcKenzie Dec 01 '21 at 09:49
  • @PaulMcKenzie Yes I can use std::fill_n but I have to do it without using any stl other than , is there any similar way like designated intialisation to do this ? – Hmm Dec 01 '21 at 11:12
  • @TJSM -- You should state up front what you cannot use. You posted a link that has `std::fill_n` as the top answer, yet you didn't mention anything about it, which was confusing as to why you skipped over it. Last, your post has `C++ std=17` mentioned -- if that is the case, why are you not simply using the facilities offered by C++17? Those facilities being `std::fill`, and `std::fill_n`. – PaulMcKenzie Dec 01 '21 at 13:45

0 Answers0