array in C

Suppl

Newbie
Joined
Nov 16, 2013
Messages
16
Reaction score
0
I just have one question about preincrementation. For ex. we got
Code:
array[6] = { 1, 2, 3, 4, 5, 6 };
for(i=0; i < 6; i++)
{
array[i]=array[i+1];
}

why this one does not work:
Code:
for(i=0; i < 6; 1)
{
array[i]=array[++i];
}
 
I don't develop in C but the 3rd parameter doesn't change. It just loops at i=0
You need to increment or decrement i with each loop iteration.
 
It "works," just not the way you're expecting it to. The portion with the increment operator is evaluated first and it returns a value. The value will either be the value of 'i' before the incrementation or the value after the incrementation.

In this case, you've chosen pre-incrementation, which increments the value of a variable and returns the resulting incremented value.

So, using this line "array=array[++i]", the left side and right side are always accessing the same position in the array.

If 'i' is 3:
++i increments i to 4, and then returns 4 so
array[4]=array[4];

If you were to use the post-increment operator, it would look like this:

If 'i' is 3:
i++ increments i to 4, and then returns 3 so
array[4]=array[3];


Notice that neither case will perform the same way as your initial code block that uses "array=array[i+1];".

The functional equivalent of your initial code block would be this:

array[6] = { 1, 2, 3, 4, 5, 6 };
for(i=0; i < 6; 1)
{
array[i++]=array;
}

In that case, if 'i' is 3:
++i increments i to 4 and returns 3 so
array[3]=array[4];
 
I don't develop in C but the 3rd parameter doesn't change. It just loops at i=0
You need to increment or decrement i with each loop iteration.

He's incrementing from within the loop. It's perfectly fine and quite common.

Note that you can easily turn a for-loop into a while-loop with for-loop syntax:

for( ; i<6 ; )
{
i++;
}

is the same as

while(i<6)
{
i++;
}
 
eimFlood thanks(ofc other too) :)
 
I know Java basics, in this case it's similar with C, i think it's because this:
(i=0; i < 6; 1)

It can't be a static number. preincrement must be defined in for () as a third argument.
 
I know Java basics, in this case it's similar with C, i think it's because this:
(i=0; i < 6; 1)

It can't be a static number. preincrement must be defined in for () as a third argument.

Though it fails in Java, it's valid in GNU C.

Give it a try:
http://www.compileonline.com/compile_java_online.php
http://www.compileonline.com/compile_c_online.php

In fact, there's another important difference between Java and C here. Java will throw an "array out of bounds" exception, but the code will execute without complaints in C, even though the last iteration of the loop will set array[5] to an unknown value (that value being whatever happens to be in memory at the position of array+6 at the time).


Also, note that if you wanted to do the incrementation inside the loop in Java, and you really wanted to use a for-loop, you could just construct the for-statement like this "for(i=0; i < 6; i=i)"
 
Problem is with the loop, you are not incrementing it right. make it like i++ and it will work properly
 
I just have one question about preincrementation. For ex. we got


why this one does not work:
Code:
for(i=0; i < 6; 1)
{
array[i]=array[++i];
}

i think it's because the third parameter from the forloop is incorrect.
the third parameter should be the incrementation. 'i++' . :)
 
Last element index will be out of bound so it wont work with your code a= a[i+1] or another one too
 
It "works," just not the way you're expecting it to. The portion with the increment operator is evaluated first and it returns a value. The value will either be the value of 'i' before the incrementation or the value after the incrementation.

In this case, you've chosen pre-incrementation, which increments the value of a variable and returns the resulting incremented value.

So, using this line "array=array[++i]", the left side and right side are always accessing the same position in the array.

If 'i' is 3:
++i increments i to 4, and then returns 4 so
array[4]=array[4];

If you were to use the post-increment operator, it would look like this:

If 'i' is 3:
i++ increments i to 4, and then returns 3 so
array[4]=array[3];


Notice that neither case will perform the same way as your initial code block that uses "array=array[i+1];".

The functional equivalent of your initial code block would be this:

array[6] = { 1, 2, 3, 4, 5, 6 };
for(i=0; i < 6; 1)
{
array[i++]=array;
}

In that case, if 'i' is 3:
++i increments i to 4 and returns 3 so
array[3]=array[4];


Actually you're wrong. The result of both:
Code:
array[i] = array[i++];

And
Code:
array[i] = array[++i];

Is undefined behavior. Look up sequence points. You shouldn't have code with either increment on i, and rather just i + 1...

i think it's because the third parameter from the forloop is incorrect.
the third parameter should be the incrementation. 'i++' .

This would seem like a textbook reply to me. You don't know much about C...
 
Last edited:
Array is a collection of variables belongings to the same data type. You can store group of data of same data type in an array.

#include<stdio.h>
int main()
{ int i;

int arr[5] = {10,20,30,40,50};

// declaring and Initializing array in C

//To initialize all array elements to 0, use int arr[5]={0};

/* Above array can be initialized as below also

arr[0] = 10;
arr[1] = 20;
arr[2] = 30;
arr[3] = 40;
arr[4] = 50; */
for (i=0;i<5;i++)
{
// Accessing each variable
printf("value of arr[%d] is %d \n", i, arr);
}
}
 
Your first code will not work because at the end of the loop you are accessing an element outside the bounds of the array.

The second code doesn't work because you are incrementing i with each step in the ++i call. So once again you are accessing elements outside the bounds of the array.
 
Back
Top