for loop is not setting element 0 to NULL

Hello - I am trying to set a loop where I set values from one array into another but shifted by one element. This means that my second array (array2) needs to be null for array2[0], and then the following elements are being filled in with my for loop. Does anyone know of a way to set my element[0] to null inside the loop? thank you from a BEGINNER... My array1 size on the right is 10 elements, my new array (array2) on the left is 11 elements because I'm shifting the numbers to the right by one element.

1
2
3
4
	for (int index = 0; index < (size + 1); index++)
            {
         array2[index] = arr[index-1];
	    }
Just FYI, when index is 0 you try to access arr[-1] which is out of bounds.

Why not just set array2[0] = 0/NULL/or whatever before or after the loop?
Hi - Yes, I tried that. Here is the code I put in but it placed a number "48" inside array2[0]. Since I'm new to this, I have no idea where the 48 came from. But it seems to me like it should work, right?


1
2
3
4
5
6
		for (int index = 0; index < (size + 1); index++)
            {
                array2[index] = arr[index-1];
			}
		array2[0]='0';
You are, again, accessing outside the bounds of array2 causing undefined behavior. Assuming size is the number of elements in array2, you access outside the bounds of array2 on each end of the array.

'0' is the character literal for 0. It's value is 48 in ascii.
Topic archived. No new replies allowed.