Thread: Bad? Pointer question

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    27

    Bad? Pointer question

    Code:
    #include <iostream>
    
    void test(char *str);
    
    int main ( void )
    {
    	char a[] = "asdf";
    
    	test(a);
    
    	return 0;
    }
    
    void test(char *str)
    {
    	while(*str != NULL)
    	{
    		std::cout << *str;
    		str++;
    	}
    }
    Is that bad? I was just playing around, and I thought about trying something like that. It works, from atleast what I can tell.

  2. #2
    Registered User
    Join Date
    Aug 2003
    Posts
    127
    not bad and not good, use 0 probably
    Nana C++ Library is a GUI framework that designed to be C++ style, cross-platform and easy-to-use.

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    27
    The more I think about it, the more it seems okay, but I'm not 100% positive

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    It would be better like this
    Code:
    #include <iostream>
    
    using namespace std;
    
    void test(char *str);
    
    int main ( void )
    {
    	char a[] = "asdf";
    
    	test(a);
    
    	return 0;
    }
    
    void test(char *str)
    {
    	while(*str != NULL)
    	{
    		cout << *str;
    		str++;
    	}
    }

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    27
    Quote Originally Posted by Quantum1024
    It would be better like this
    Code:
    #include <iostream>
    
    using namespace std;
    
    void test(char *str);
    
    int main ( void )
    {
    	char a[] = "asdf";
    
    	test(a);
    
    	return 0;
    }
    
    void test(char *str)
    {
    	while(*str != NULL)
    	{
    		cout << *str;
    		str++;
    	}
    }
    I honestly don't see anything aside from namespace usage, I apoligize if I'm wrong.

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    That is the only difference. If your going to be using cout a lot then you may as well just use namespace to save having to do stud::cout all the time.

  7. #7
    Registered User
    Join Date
    Mar 2002
    Posts
    27
    Quote Originally Posted by Quantum1024
    That is the only difference. If your going to be using cout a lot then you may as well just use namespace to save having to do stud::cout all the time.

    Ahh thank you, I'm kind of bad on using name spaces, I know I should consider them more often.

    I think all of you for your help

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    Of course you could always just do this with a char pointer.

    Code:
    void test(char *str)
    {
       cout << str;
    
    }

  9. #9
    Registered User
    Join Date
    Aug 2003
    Posts
    127
    i think there are 2 spots in your code
    1) NULL
    pointed 0 instead of NULL
    2) void test(char *str);
    in the case of test would not change the elements which str is refered, void test(const char* str) is better.
    for example
    test("asdf"); it works if the type of parameter is const char*

    at last, i think introducing namespace std into global scope is a bad idea. you can do it like this
    Code:
    void test(const char *str)
    {
                   using namespace std;
    	while(*str != 0)
    	{
    		cout << *str;
    		str++;
    	}
    }
    Nana C++ Library is a GUI framework that designed to be C++ style, cross-platform and easy-to-use.

  10. #10
    *this
    Join Date
    Mar 2005
    Posts
    498
    ^^^ there is nothing wrong with making it global, you make it global so you can use it over and over, doesnt really matter. Ive never seen it used in local scope before.

  11. #11
    Registered User
    Join Date
    Aug 2003
    Posts
    127
    hehe, it's just nothing wrong HERE ^_^, using it in local scope avoids name pollution
    Nana C++ Library is a GUI framework that designed to be C++ style, cross-platform and easy-to-use.

  12. #12
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    I've never seen it used localy either but what jinhao said dose make sense if you intend to use other namespaces which contain conflicting names.

  13. #13
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    i think there are 2 spots in your code
    1) NULL
    pointed 0 instead of NULL
    For all intents and purposes, "NULL" is 0 in C++. Or rather, it is safe to, and indented that you should, use 0 for null comparison in C++.
    Code:
    if( ptr == 0 )
        ...
    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Easy pointer question
    By Edo in forum C++ Programming
    Replies: 3
    Last Post: 01-19-2009, 10:54 AM
  2. char pointer to pointer question
    By Salt Shaker in forum C Programming
    Replies: 3
    Last Post: 01-10-2009, 11:59 AM
  3. a pointer to a function question..
    By transgalactic2 in forum C Programming
    Replies: 17
    Last Post: 10-21-2008, 11:47 AM
  4. Simple pointer question
    By jayznz in forum C Programming
    Replies: 2
    Last Post: 04-04-2006, 11:36 PM
  5. question about pointer?
    By pnxi in forum C Programming
    Replies: 2
    Last Post: 02-13-2002, 01:45 PM