0

This is a code from geeksforgeeks counting sort implementation:

The have used the below for loop to iterate through a string -

char arr[] = "geeksforgeeks";

for(i = 0; arr[i]; ++i)
   ++count[arr[i]];

From google search I could understand that this condition implicitly evaluates to arr[i]!='\0'. Have I understood it correctly ? If I were to use a similar for loop to iterate through an array of integers, would it evaluate to arr[i]!=0 ?

Or is there any better way to iterate through an array of integers when we do not know its size. I am a beginner and please try to provide suggestions which do not involve advanced data structures in C.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
john
  • 45
  • 1
  • 6
  • 1
    That depends: is 0 a valid value in your array? If so you oviously can't use it as a marker for the end of the array – Ingo Leonhardt Feb 28 '17 at 09:21
  • 1
    This is an array of characters, not integers. The reason it works is because the data is a null terminated string. You have basically implemented the `strlen` function. So it is unclear what you mean with "is there any better way to iterate through an array of integers when we do not know its size". – Lundin Feb 28 '17 at 09:23
  • For example I have an array, arr[] = {1,2,3,4}; and I do not want to compute the array size to iterate through the array. How can i do it like they have done it in the example I quoted. – john Feb 28 '17 at 09:31
  • @john if you have `arr[] = {1,2,3,4};` then the size of the array in _bytes_ is `sizeof(arr)` and the number of elements in the array is `sizeof(arr) / sizeof(arr[0])`. – Jabberwocky Feb 28 '17 at 09:36
  • Not sure how this question was a duplicate. The question I am asking is about iterating though an array when the size is "unknown". I did not ask a question on "how to calculate the size of the array". Anyway, I thank everyone that have provided the explanation. It has given me a good direction by mentioning sentinels :) thanks all ! – john Feb 28 '17 at 09:39
  • @MichaelWalz thanks Michael, I probably should have mentioned it as the number of elements in array. I understand how the calculation part works :) thanks ! I was trying to understand how the condition part of for loop worked for character arrays and how the same condition would work in case of integer arrays. – john Feb 28 '17 at 09:42
  • @john obviously if the size is unknown it is unknown, period. The only way to determine the end of an array of unknown size is using a guard value, some special value meaning "end of the array", for example the value 0 which is used for strings. – Jabberwocky Feb 28 '17 at 09:45

2 Answers2

3

Unless you know for sure that there is some sort of sentinel value at the end of the array, you cannot iterate over an array unless you know its size.

An example of a sentinel value is '\0' in the case of strings.

When you want to work with integer arrays you should keep around both the array and the size. This is why functions that take arrays as input parameters take a pointer and an int:

void example(int* array, int size) { /* ... */ }
Vittorio Romeo
  • 90,666
  • 33
  • 258
  • 416
  • This question has been asked many times before and has many duplicates, including highly (+500) upvoted ones. You are a senior member with a lot of reputation, and thus have been given privilege and understanding to do the following. Improving StackOverflow by reducing duplicates by voting to close as a duplicate instead of answering. Such action will greatly improve the community at the negligible disadvantage to yourself. Thank you. – 2501 Feb 28 '17 at 09:30
  • @2501: I thought this was a good opportunity to explain the OP the concept of *sentinel values*. The duplicate you've closed the question with has no mention of that concept, which was relevant to the OP's question. – Vittorio Romeo Feb 28 '17 at 09:33
  • There are many duplicates for this question that include answers that talk about sentinel values. You are well within your power to find them and vote to close. – 2501 Feb 28 '17 at 10:12
3

Yes, an integer expression that evaluates to zero will be considered false by the for construct and end the loop.

Since strings are terminated by a '\0' character with the value 0, this works to detect the end of the string.

Yes, you can use this to iterate over an array of any integer (or pointer, where NULL is considered false) array, but of course terminating such arrays with zero is less common (since zero is a rather useful integer).

If you can use some other value to mark the end (this is called a "sentinel") you can course use that, rather than the length or zero. There's nothing magical about zero, except that for strings it's the convention and thus well supported.

unwind
  • 391,730
  • 64
  • 469
  • 606
  • This question has been asked many times before and has many duplicates, including highly (+500) upvoted ones. You are a senior member with a lot of reputation, and thus have been given privilege and understanding to do the following. Improving StackOverflow by reducing duplicates by voting to close as a duplicate instead of answering. Such action will greatly improve the community at the negligible disadvantage to yourself. Thank you. – 2501 Feb 28 '17 at 09:30
  • Thanks for the detailed answer. I will look at how we can come up with a sentinel for array of integers. – john Feb 28 '17 at 09:34