0

I am trying to repeat elements of an array but stuck in an infinite loop the size of the array is currently 2 , I am trying to repeat these elements again to make the size 5, for example sample output: char a[]="ED"; trying to make it char a[]="EDEDE"; the code is as follows

while(sizeof(a)<5)
{
    for(i=0;i<2;i++){
        a[fk]=a[i];
        fk++;}
}

goes into infinite loop

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
Waqar_1995
  • 11
  • 3
  • 4
    sizeof(a) is a fixed value – Temani Afif Nov 04 '17 at 10:20
  • 2
    Writing to elements beyond the bounds of the array is an invalid operation(This is _undefined behavior_), not an expansion of the array. – BLUEPIXY Nov 04 '17 at 10:22
  • 1
    You need to resize the array first, then fill it with contents. See [Resizing an array with C](https://stackoverflow.com/questions/2937409/resizing-an-array-with-c). – Serge Nov 04 '17 at 10:22
  • @Serge: Arrays defined as shown cannot be resized. – alk Nov 04 '17 at 10:39

2 Answers2

0

to make the size 5,

An array of any type T defined like this

T a[] = ... some initialiser ...;

or like this

T b[42];

cannot be resized (in C).

alk
  • 69,737
  • 10
  • 105
  • 255
0

C11 6.4.5 String literals(P7):

It is unspecified whether these arrays are distinct provided their elements have the appropriate values. If the program attempts to modify such an array, the behavior is undefined.

So, There is no resizing if you reassign a to something else, otherwise it is undefined behaviours.

msc
  • 33,420
  • 29
  • 119
  • 214