Reputation: 1089
I created a code below, where I need to keep track of an array, increase the pointer to array, and stop if the array ends.
I tested using the code below and apparently after 1000 iterations of increasing ptr
and printing *ptr
, I get 0
as a value.
So is it true the last element of array will ALWAYS be 0? How can I tell the ptr to stop at the last element, if the array length is created dynamicaly (and I am not allowed to use linked list instead)? I am afraid that if I use below code, one day I will meet printf i=1000 , and it returns me random memory location (sofar for the code below, it is not true, but who knows)..
#include <stdio.h>
int my_array[] = {1,23,17,4,-5,100};
int *ptr;
int main(void)
{
int i;
ptr = &my_array[0]; /* point our pointer to the first
element of the array */
printf("value of ptr is %d\n", *ptr);
for(i=0; i<1000; i++){
ptr++;
}
printf("value of ptr is now %d", *ptr);
return 0;
}
Upvotes: 2
Views: 1504
Reputation: 3418
I wouldn't depend on the fact that there's 0 after your array, since memory management changes from platform to platform.
Since you're the one declaring the array, you should know how long it is, so you could technically say:
for(i = 0; i < 6; i++)
If you don't know the size of the array, you can always use the sizeof operator like this:
// (sizeof my_array) will return the total size of the array in bytes
// (sizeof my_array[0]) will return the size of one element in bytes
// Dividing the two sizes in bytes will give you the total size.
int array_size = (sizeof my_array) / (sizeof my_array[0]);
for(i = 0; i < array_size; i++)
My C++ is a bit rusty, but should do the trick :)
Upvotes: 1
Reputation: 34625
So is this true the last element of array will ALWAYS be "0"?
No, you are unlucky enough that the code didn't break.
How can I tell the ptr to stop at the last element, if the array length is created dynamicaly
Not possible with the arrays. You need to know the size of the array and iterate over it.
Upvotes: 2
Reputation: 5413
You need to track dynamic array sizes yourself by passing additional arguments. There is no guarantee whatsoever about the memory after the array.
0 termination is only provided for strings.
Upvotes: 1
Reputation: 500733
No, you are not allowed to dereference the element past the end of your array; this is undefined behaviour.
One way to do it is to note that there are sizeof(my_array)/sizeof(my_array[0])
elements in your array.
Upvotes: 2
Reputation: 35069
So is this true the last element of array will ALWAYS be "0"?
No, this is just pure luck. You are reading from a random location in your memory, you could even end up in a segmentation fault, killing your program.
You should always make sure to not read/write over the boundaries of your array, or really bad things might happen (Segmentation faults may be the least bad one).
You can keep track of the number of items in your array by using: sizeof(my_array)/sizeof(int)
Upvotes: 4
Reputation: 5724
you should controlling array size by yourself
sizeof(my_array)/sizeof(int)
you created array with 6 elements. by accessing 7th element (array[6]
) of array you crash program
Upvotes: 0