1

I am practicing loops in arrays, does the dynamic array's loop can't be terminated with null character.

for (int i=0; arr[i]!='\0'; i++)

And how can I terminate these loops without using this format:

for (const auto& value : arr)
2
  • You have to know the size of the array somehow. There's no magic way to do it. Commented May 21, 2020 at 18:09
  • 1
    Plus for(const auto& value : arr) doesn't work on a dynamic array, for the simple reason that there is no way to know the size of a dynamic array from the array itself. Commented May 21, 2020 at 18:10

4 Answers 4

4

For an array allocated on the free store

int* arr = new int[size];

there are two ways to know where the end is. The most straightforward is to remember how big it is:

for (int i = 0; i < size; ++i)
    // do something with arr[i]

The other way is to set the last element of the array to a sentinel value -- a value that won't show up in the data that you're working with, so when you see it you know that you're at the end of the array:

for (int i = 0; i < size; ++i)
    arr[i] = i;
arr[size - 1] = -1;

Now you can loop through the array looking at the values:

for (int i = 0; arr[i] != -1; ++i)
    // do something with arr[i]

That's how literal strings work (although they're not allocated on the free store):

const char *str = "Hello, world!";
for (int i = 0; str[i] != '\0'; ++i)
    std::cout << str[i];
std::cout << '\n';

The tradeoff here is that with the first approach you have to pass an extra parameter around to tell other code how large the array is:

void do_something(int* arr, int size) {
    for (int i = 0; i < size; ++i)
        // whatever
}

on the other hand, having a sentinel means that you waste the last element:

void do_something(int* arr) {
    for (int i = 0; arr[i] != -1; ++i)
        // whatever
}

the last element doesn't have any of the data you're working with; it's only there to mark the end.

And, of course, with a sentinel there's the risk that you haven't picked a good sentinel value. If you find that value somewhere in the middle of your data the code will stop looping, and won't process the rest of the data.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, this helped me not only in rectifying my mistake but also in understanding the concept clearly.
1

you can do like this:

int* arr = new int[size];
for(int i=0; i< size; ++i)
{
    //do somtiong;
}

but dont foget to delete the array at the end:

delete[] arr;

also as @john wrote at the comment range base loops work only on STL containers and statically created one like that: int arr[size]; not on dynamically allocate arrays.

2 Comments

I think they also work on static arrays, but definitely not on dynamic arrays.
you are right I checkt it now its new to me to :) onlinegdb.com/BJSZ8S4i8
0

Null character will work based on your application purposes. For example if you are given an array, and you know beforehand, that the last element in the array is a null character, then you can use null character to terminate the loop.

Note that, if you use null character termination, the array cannot hold value 0, as the null character has ASCII value 0, and whenever you array contains 0, the loop will end. In standard C/C++, null termination is used for strings. Strings are character arrays with a null character marking the end of the array.

In general case, working with arrays, you need to know the size of the array. If you are given a STL container like a vector, then you can access its size using the size() operator. But with the good old arrays, you need to know the size somehow.

Comments

0

If you have a dynamic char array:

char* str = new char[size];

there in no null terminator unless you put it there.

You can only rely on null terminator if you know there is one:

char[] str1 = "Hello world"; // null terminated by default
char* str2 = new char[16]; // enough space for copy
strcpy(str2, str1); // strcpy also copies '\0'

// now you can iterate
for(int i = 0; str2[i] != '\0'; i++)
{
    std::cout << str2[i];
}

But all of the said above applies only to char arrays and cannot be applied to other data types. So, to iterate over all the elements of an array you need to know their count beforehand:

int* arr = new int[size];
for(int i = 0; i < size; i++)
{
    // do something
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.