Thread: integer null terminator

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    99

    integer null terminator

    How do we terminate integer array in while loop

    Code:
    #include <stdio.h>int main()
    {
    	int i;
    	int array[5] = {0,2,3,4,0};
       
            while (array[i] != 0)
    	{
    		printf("print array %d", array[i]);
    		
    		i++;
    	}
       			
        return 0;
    
    
    }
    array[0] = 0
    array[1] = 2
    array[2] = 3
    array[3] = 4
    array[4] = 0

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,121
    Unlike char arrays which have a universally accepted end of string marker '\0' to make them strings, there is no such value for integers.

    You have to decide on a case by case basis which integer value means 'no data'.
    They're called 'sentinel values' if you want to read up on the subject (you should do it now).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Apr 2012
    Posts
    99
    Quote Originally Posted by Salem View Post
    Unlike char arrays which have a universally accepted end of string marker '\0' to make them strings, there is no such value for integers.

    You have to decide on a case by case basis which integer value means 'no data'.
    They're called 'sentinel values' if you want to read up on the subject (you should do it now).
    I will do this

    Code:
    #include <stdio.h>int main()
    {
        int i = 0;
        int array[5] = {0,2,3,4,0};
        
            while (array[i] != 5)
        {
            printf("\n print array : %d", array[i]);
             
            i++;
        }
                 
        return 0;
      
    }

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,616
    In order for sentinel values to work, they need to be unique and exist somewhere in the array. Preferably they would be at the actual end of the data.

  5. #5
    Registered User
    Join Date
    Apr 2012
    Posts
    99
    Quote Originally Posted by whiteflags View Post
    In order for sentinel values to work, they need to be unique and exist somewhere in the array. Preferably they would be at the actual end of the data.
    so we don't need integer array terminator in c

  6. #6
    Registered User
    Join Date
    May 2009
    Posts
    3,874
    Quote Originally Posted by vead View Post
    so we don't need integer array terminator in c
    It is not common to have a terminator in an integer array; but, it some cases it might make sense to have one.
    But, the "sentinel value" needs to be a value that is not in the normal integer data.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  7. #7
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,875
    To expand on what others have said:

    I noticed in your example above that you have a signed integer as your data type, but all the numbers that you have put into the array are positive.

    If there is no way that a negative number can be put into the array (via checks) and no reason for it to be put in there (e.g. each element is the number of boxes in a row), perhaps you could use -1 as a nul character...

    That being said, there are better ways to manage a variable length set of integers (malloc, linked lists, ...)
    Fact - Beethoven wrote his first symphony in C

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple Program, null terminator
    By dexstar in forum C Programming
    Replies: 2
    Last Post: 08-01-2013, 08:34 AM
  2. newline and null terminator
    By dunsta in forum C Programming
    Replies: 2
    Last Post: 04-22-2010, 07:13 AM
  3. integer null question
    By transgalactic2 in forum C Programming
    Replies: 6
    Last Post: 01-06-2009, 01:22 PM
  4. Removing null terminator from a string
    By Canadian0469 in forum C Programming
    Replies: 3
    Last Post: 10-27-2007, 02:03 PM
  5. Looking for null terminator
    By MiamiCuse in forum C Programming
    Replies: 3
    Last Post: 10-22-2005, 11:22 PM

Tags for this Thread