One small doubt with char pointer

This is a discussion on One small doubt with char pointer within the C Programming forums, part of the General Programming Boards category; I have a very small doubt(might sound silly..). consider a char pointer that is undefined. Code: char *p; now, if ...

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

    One small doubt with char pointer

    I have a very small doubt(might sound silly..).

    consider a char pointer that is undefined.

    Code:
        char *p;
    now, if i try to access the undefined pointer, i will get a run time error.

    Code:
        char ch = p[0];
    This much i understand.

    I could initialize the pointer to NULL. But let's say i didn't do that.
    Is there a way to check whether a pointer is undefined?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,823
    > Is there a way to check whether a pointer is undefined?
    No.

    And the correct term is uninitialised.
    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
    May 2011
    Posts
    11
    Uninitialized.. I will remember that. Thanks.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    You are writing the code, you are responsible for it's behaviour... if your code is working correctly why on earth would you need to check if something is uninitialized?

  5. #5
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Quote Originally Posted by Salem View Post
    > Is there a way to check whether a pointer is undefined?
    ....
    And the correct term is uninitialised.
    ..Or unitialized.......I guess depending on what side of the water your on.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,628
    Quote Originally Posted by CommonTater View Post
    You are writing the code, you are responsible for it's behaviour... if your code is working correctly why on earth would you need to check if something is uninitialized?
    Usually the reason is sloppiness: creating something before it is needed, then having to work out later if it has a usable value.

    The better approach is to ensure it is initialised before doing anything else with it. That can mean initialising it when creating it, only creating it immediately before it is needed, and/or having sensible flow control so that you know the pointer is initialised before any code that tries to use it.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Sunshine, and read this, this, and this before posting again.

  7. #7
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    You will have to excuse me on this one, but why on earth would you not initialize it to NULL when you create it if you you can't use it right away? I know the new standard allows you to create variables willy-nilly, but still there is wisdom in the "old ways".
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Doubt about char in C
    By SkyKnight in forum C Programming
    Replies: 2
    Last Post: 01-21-2010, 09:51 AM
  2. pointer doubt....
    By roaan in forum C Programming
    Replies: 14
    Last Post: 07-29-2009, 12:20 PM
  3. Doubt in pointer.
    By shwetha_siddu in forum C Programming
    Replies: 5
    Last Post: 03-21-2009, 02:28 AM
  4. Doubt regarding pointer
    By karthik537 in forum C Programming
    Replies: 7
    Last Post: 01-21-2009, 09:53 AM

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