how to judge whether what the pointer links to is string or not?

This is a discussion on how to judge whether what the pointer links to is string or not? within the C Programming forums, part of the General Programming Boards category; Hi,all how to judge whether what the pointer links to is string or not? say, I have a struct in ...

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    16

    how to judge whether what the pointer links to is string or not?

    Hi,all

    how to judge whether what the pointer links to is string or not?

    say, I have a struct in program in C.

    typedef struct {
    char *Name;
    ...
    } stuA ;


    main() {

    stuA p;
    ...



    }

    I have to judge whether the member Name has been initialized or not?How to do?

    thanks.

  2. #2
    Registered User dizolve's Avatar
    Join Date
    Dec 2002
    Posts
    68
    Assign it to NULL when you create it and then check that.

      __               &n bsp;      ___ & nbsp;       &nb sp;       &nbsp ;    
     /\ \  __    &nbs p;           /\_ \      &nbsp ;        & nbsp;     
     \_\ \/\_\  ____     _ __\//\ \    __  __&n bsp;    __   
     /'_` \/\ \/\_ ,`\  / __`\\ \ \  /\ \/\ \  /'__`\ 
    /\ \_\ \ \ \/_/  /_/\ \_\ \\_\ \_\ \ \_/ |/\  __/ 
    \ \___,_\ \_\/\____\ \____//\____\\ \___/ \ \____\
     \/__,_ /\/_/\/____/\/___/ \/____/ \/__/   \/____/
            &n bsp; I have a BAD figlet& nbsp;addiction.

  3. #3
    Registered User
    Join Date
    Feb 2003
    Posts
    16
    Originally posted by dizolve
    Assign it to NULL when you create it and then check that.
    Thanks for your answer.

    But the problem is it is not created in my program.
    I just provide a function, and my user will create the strA and use my function, so I think I can just do with the checking inside my function.

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>I just provide a function, and my user will create the strA and use my function,
    Then the user should initialise it. If they don't, it's their fault
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    If they don't initialize it to NULL, then it could point at anything making it impossible to say if it's pointing at something valid. Plus that would make the program highly unstable (iow: It might crash).
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  6. #6
    Registered User
    Join Date
    Feb 2003
    Posts
    16
    Originally posted by Magos
    If they don't initialize it to NULL, then it could point at anything making it impossible to say if it's pointing at something valid. Plus that would make the program highly unstable (iow: It might crash).
    Yes, that is why I want to check it in my program.
    Because I think it is the provider's responsibility to check it,even I can ask the user to do it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointer to String and Pointer to Char
    By vb.bajpai in forum C Programming
    Replies: 3
    Last Post: 06-15-2007, 03:03 PM
  2. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  3. Custom String class gives problem with another prog.
    By I BLcK I in forum C++ Programming
    Replies: 1
    Last Post: 12-18-2006, 02:40 AM
  4. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 10:03 PM
  5. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 03:58 PM

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