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. char *> string conversion , env[]

char *> string conversion , env[]

Scheduled Pinned Locked Moved C / C++ / MFC
linuxhelptutorialquestionlearning
11 Posts 4 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.
  • A Offline
    A Offline
    ALLERSLIT
    wrote on last edited by
    #1

    Hey, Iam reading this book and Iam writing my own program aside. Iam currently reading about some functions related to linux.

    #include <stdio.h>
    #include <iostream>

    using namespace std;

    int main(int argc,char *env[])
    {
    int i=0;
    cout << env[i] << endl;
    return 0;
    }

    This is the program, now how would I actually put the output of env[i] into a string? In my own program Iam playing around with conversions and string editing and stuff, now I need to convert the output of env[i] into a string though, not sure how to do that. Thankfull for any help, greetins :)

    D 1 Reply Last reply
    0
    • A ALLERSLIT

      Hey, Iam reading this book and Iam writing my own program aside. Iam currently reading about some functions related to linux.

      #include <stdio.h>
      #include <iostream>

      using namespace std;

      int main(int argc,char *env[])
      {
      int i=0;
      cout << env[i] << endl;
      return 0;
      }

      This is the program, now how would I actually put the output of env[i] into a string? In my own program Iam playing around with conversions and string editing and stuff, now I need to convert the output of env[i] into a string though, not sure how to do that. Thankfull for any help, greetins :)

      D Offline
      D Offline
      David Crow
      wrote on last edited by
      #2

      ALLERSLIT wrote:

      int main(int argc,char *env[])

      This should probably be:

      int main( int argc, char *argv[], char *env[] )

      ALLERSLIT wrote:

      how would I actually put the output of env[i] into a string?

      Have you tried:

      std::string str = env[i];

      "One man's wage rise is another man's price increase." - Harold Wilson

      "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

      "Man who follows car will be exhausted." - Confucius

      A 1 Reply Last reply
      0
      • D David Crow

        ALLERSLIT wrote:

        int main(int argc,char *env[])

        This should probably be:

        int main( int argc, char *argv[], char *env[] )

        ALLERSLIT wrote:

        how would I actually put the output of env[i] into a string?

        Have you tried:

        std::string str = env[i];

        "One man's wage rise is another man's price increase." - Harold Wilson

        "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

        "Man who follows car will be exhausted." - Confucius

        A Offline
        A Offline
        ALLERSLIT
        wrote on last edited by
        #3

        Oh thanks, gotcha! let's say I wouldn't put

        char *env[]

        there into the main() thingy, how would I be able to get the output of env[i] then? Like

        #include <stdio.h>
        #include <iostream>
        #include <string>

        using namespace std;

        int main()
        {
        char *env[];
        string str = env[9];
        cout << str << endl;
        return 0;
        }

        I get this error: |14|error: storage size of ‘env’ isn't known Makes sense to me, but how would I avoid that error?

        D C L 3 Replies Last reply
        0
        • A ALLERSLIT

          Oh thanks, gotcha! let's say I wouldn't put

          char *env[]

          there into the main() thingy, how would I be able to get the output of env[i] then? Like

          #include <stdio.h>
          #include <iostream>
          #include <string>

          using namespace std;

          int main()
          {
          char *env[];
          string str = env[9];
          cout << str << endl;
          return 0;
          }

          I get this error: |14|error: storage size of ‘env’ isn't known Makes sense to me, but how would I avoid that error?

          D Offline
          D Offline
          David Crow
          wrote on last edited by
          #4

          ALLERSLIT wrote:

          Makes sense to me, but how would I avoid that error?

          By declaring env as:

          char *env[10];

          "One man's wage rise is another man's price increase." - Harold Wilson

          "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

          "Man who follows car will be exhausted." - Confucius

          A 1 Reply Last reply
          0
          • A ALLERSLIT

            Oh thanks, gotcha! let's say I wouldn't put

            char *env[]

            there into the main() thingy, how would I be able to get the output of env[i] then? Like

            #include <stdio.h>
            #include <iostream>
            #include <string>

            using namespace std;

            int main()
            {
            char *env[];
            string str = env[9];
            cout << str << endl;
            return 0;
            }

            I get this error: |14|error: storage size of ‘env’ isn't known Makes sense to me, but how would I avoid that error?

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

            That makes no sense to me, even if you fix the error the way David (correctly) suggested (the array content is still uninitialised). :)

            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
            • D David Crow

              ALLERSLIT wrote:

              Makes sense to me, but how would I avoid that error?

              By declaring env as:

              char *env[10];

              "One man's wage rise is another man's price increase." - Harold Wilson

              "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

              "Man who follows car will be exhausted." - Confucius

              A Offline
              A Offline
              ALLERSLIT
              wrote on last edited by
              #6

              If I declare it the way you did, I can not put it into a string the same way i learned in the last post.

              D 1 Reply Last reply
              0
              • A ALLERSLIT

                If I declare it the way you did, I can not put it into a string the same way i learned in the last post.

                D Offline
                D Offline
                David Crow
                wrote on last edited by
                #7

                You seem to be misunderstanding main()'s signature and it's various formats. That said, what exactly is it that you are trying/wanting to do?

                "One man's wage rise is another man's price increase." - Harold Wilson

                "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                "Man who follows car will be exhausted." - Confucius

                A 1 Reply Last reply
                0
                • D David Crow

                  You seem to be misunderstanding main()'s signature and it's various formats. That said, what exactly is it that you are trying/wanting to do?

                  "One man's wage rise is another man's price increase." - Harold Wilson

                  "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                  "Man who follows car will be exhausted." - Confucius

                  A Offline
                  A Offline
                  ALLERSLIT
                  wrote on last edited by
                  #8

                  Trying to figure out how to get the outpuf of env[i] without putting all that other stuff into main()'s signature.

                  D C 2 Replies Last reply
                  0
                  • A ALLERSLIT

                    Trying to figure out how to get the outpuf of env[i] without putting all that other stuff into main()'s signature.

                    D Offline
                    D Offline
                    David Crow
                    wrote on last edited by
                    #9

                    Whether you declare them in the signature or not, the number of command-line arguments, the command-line arguments themselves, and the environment variables are "sent" to main() regardless. Obviously, if you opt to not declare them in main()'s signature, you will not have access to them. That said, what is env and how is it declared?

                    "One man's wage rise is another man's price increase." - Harold Wilson

                    "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                    "Man who follows car will be exhausted." - Confucius

                    1 Reply Last reply
                    0
                    • A ALLERSLIT

                      Trying to figure out how to get the outpuf of env[i] without putting all that other stuff into main()'s signature.

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

                      You may access the _environ variable. :)

                      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
                      • A ALLERSLIT

                        Oh thanks, gotcha! let's say I wouldn't put

                        char *env[]

                        there into the main() thingy, how would I be able to get the output of env[i] then? Like

                        #include <stdio.h>
                        #include <iostream>
                        #include <string>

                        using namespace std;

                        int main()
                        {
                        char *env[];
                        string str = env[9];
                        cout << str << endl;
                        return 0;
                        }

                        I get this error: |14|error: storage size of ‘env’ isn't known Makes sense to me, but how would I avoid that error?

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

                        ALLERSLIT wrote:

                        how would I avoid that error?

                        By declaring env properly according to the rules thus:

                        int main(int argc, char* argv[], char *env[])
                        {
                        }

                        Your sample above declares env as a local variable that is never initialised so you cannot get anything out of it. Remember that main() is simply a function that is called by the framework with the three parameters as I have described.

                        Just say 'NO' to evaluated arguments for diadic functions! Ash

                        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