Zero in int array treat as null not digit

This is a discussion on Zero in int array treat as null not digit within the C++ Programming forums, part of the General Programming Boards category; Zero in int array treat as null not digit 1) how can i use as digit 2) when i remove ...

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    6

    Zero in int array treat as null not digit

    Zero in int array treat as null not digit

    1) how can i use as digit
    2) when i remove zero then it print four digit in array and print some garbage value, how remove this remedies

    Code:
    #include<iostream>
    using namespace std;
    
    void check(int *t){
        while(*t != NULL){
        cout << *t++ << endl;
        t++;
      }
    }
    
    int main(){
    	int a[] = {5,4,7,8,0};
    	check(a);
    }
    print when int array does not contain zero

    Code:
    5
    4
    7
    8
    4636772
    21473443
    4009792
    2293664
    4198619
    1
    4013768
    4009224
    4718596
    2293652
    -1
    2293648

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,333
    First, you should compare with 0 (it's an array of integers), not NULL (which is for pointers).

    Plus, how did you get those results, when you're incrementing t TWICE in each loop?
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    23
    ++ has higher precedence than *, both t++ increment the pointer. Is this your intention

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,261
    Indeed, this cannot be the real code. This code will only output 5 and 7. Remove the extra increment.
    In your real code you need to pass the length of the array, obtained through using sizeof, into the check function.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #5
    Registered User
    Join Date
    May 2011
    Posts
    19
    that's right! the size of the array should be included in the display function not compare with NULL, because the compile just take 0 as NULL, so when *t=0,the loop is over ,and you do twice increment in one loop while once is ok。
    so you can write like this :
    Code:
    #include<iostream>
    using namespace std;
    
    void check(int *t, size_t size){
    	int i = 0;
    	while(i++ < size){
    		cout << *t++ << endl;
    		//t++;
    	}
    }
    
    int main(){
    	int a[] = {5,4,7,8,0};
    	check(a,sizeof(a)/sizeof(*a));
    }
    is this hopeful for you ?

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,165
    Better yet, use std::vector.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Read from file - 1-digit and 2-digit numbers
    By Bonaventura in forum C Programming
    Replies: 8
    Last Post: 03-06-2010, 05:33 AM
  2. Replies: 7
    Last Post: 07-16-2009, 12:56 AM
  3. Adding a Large number digit by digit
    By mejv3 in forum C Programming
    Replies: 23
    Last Post: 09-21-2007, 03:00 PM
  4. Treat members as an array? (opengl)
    By Zen_id3b in forum C++ Programming
    Replies: 12
    Last Post: 01-14-2005, 08:04 AM
  5. Replies: 5
    Last Post: 05-19-2004, 02:53 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21