Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. How to check whether a char pointer is an empty string?

How to check whether a char pointer is an empty string?

Scheduled Pinned Locked Moved C / C++ / MFC
tutorialquestion
7 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    dipuks
    wrote on last edited by
    #1

    Hey I have a variable, char * p which could sometimes have a string or sometimes it will be empty. How to check whether "p" has a string in it or not? I see that its not possible to do something like, if(p != "") { } Thanks

    K C F 3 Replies Last reply
    0
    • D dipuks

      Hey I have a variable, char * p which could sometimes have a string or sometimes it will be empty. How to check whether "p" has a string in it or not? I see that its not possible to do something like, if(p != "") { } Thanks

      K Offline
      K Offline
      krmed
      wrote on last edited by
      #2

      Perhaps you could use

      if(*p = 0);

      But then this assumes you initialize the pointer when it is emptied.

      char* p = NULL;
      // use your pointer
      p = &some_char_array;
      // do whatever with it
      p = NULL;

      Hope that helps.

      Karl - WK5M PP-ASEL-IA (N43CS) PGP Key: 0xDB02E193 PGP Key Fingerprint: 8F06 5A2E 2735 892B 821C 871A 0411 94EA DB02 E193

      L T 2 Replies Last reply
      0
      • K krmed

        Perhaps you could use

        if(*p = 0);

        But then this assumes you initialize the pointer when it is emptied.

        char* p = NULL;
        // use your pointer
        p = &some_char_array;
        // do whatever with it
        p = NULL;

        Hope that helps.

        Karl - WK5M PP-ASEL-IA (N43CS) PGP Key: 0xDB02E193 PGP Key Fingerprint: 8F06 5A2E 2735 892B 821C 871A 0411 94EA DB02 E193

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        krmed wrote:

        if(*p = 0);

        Don't you mean == there?

        K 1 Reply Last reply
        0
        • D dipuks

          Hey I have a variable, char * p which could sometimes have a string or sometimes it will be empty. How to check whether "p" has a string in it or not? I see that its not possible to do something like, if(p != "") { } Thanks

          C Offline
          C Offline
          CPallini
          wrote on last edited by
          #4

          you may check for an empty string with

          if (strlen(p)==0)
          {
          //..
          }

          the above check, of course, assumes p pointing to a valid string (empty string is a valid one). i.e. such code doesn't check if p is a valid pointer, to spot the difference, try:

          char *p, *q;
          p="";
          q=NULL;

          printf("strlen(p)=%d\n", strlen(p));
          printf("strlen(q)=%d\n", strlen(q));

          If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
          This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
          [My articles]

          1 Reply Last reply
          0
          • L Lost User

            krmed wrote:

            if(*p = 0);

            Don't you mean == there?

            K Offline
            K Offline
            krmed
            wrote on last edited by
            #5

            Yes, of course! Just typed too fast.

            Karl - WK5M PP-ASEL-IA (N43CS) PGP Key: 0xDB02E193 PGP Key Fingerprint: 8F06 5A2E 2735 892B 821C 871A 0411 94EA DB02 E193

            1 Reply Last reply
            0
            • K krmed

              Perhaps you could use

              if(*p = 0);

              But then this assumes you initialize the pointer when it is emptied.

              char* p = NULL;
              // use your pointer
              p = &some_char_array;
              // do whatever with it
              p = NULL;

              Hope that helps.

              Karl - WK5M PP-ASEL-IA (N43CS) PGP Key: 0xDB02E193 PGP Key Fingerprint: 8F06 5A2E 2735 892B 821C 871A 0411 94EA DB02 E193

              T Offline
              T Offline
              Tim Craig
              wrote on last edited by
              #6

              If you set p equal to NULL and then dereference it with *p, you'll get a crash. Maybe if you set *p = '\0' or *p = "\0".

              You measure democracy by the freedom it gives its dissidents, not the freedom it gives its assimilated conformists.

              1 Reply Last reply
              0
              • D dipuks

                Hey I have a variable, char * p which could sometimes have a string or sometimes it will be empty. How to check whether "p" has a string in it or not? I see that its not possible to do something like, if(p != "") { } Thanks

                F Offline
                F Offline
                Fatbuddha 1
                wrote on last edited by
                #7

                Why not use a string type? There you can check easily. However, if you have to use a char *, then you have to care about setting the pointer to NULL or to a '\0' char. this would be the end of a string. As you like. The cleaner version, I would say, would be to use a string object. Cheers

                You have the thought that modern physics just relay on assumptions, that somehow depends on a smile of a cat, which isn’t there.( Albert Einstein)

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups