char *> string conversion , env[]
-
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 :)
-
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 :)
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
-
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
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?
-
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?
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
-
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
-
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?
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] -
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.
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
-
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
-
Trying to figure out how to get the outpuf of env[i] without putting all that other stuff into main()'s signature.
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 inmain()
's signature, you will not have access to them. That said, what isenv
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
-
Trying to figure out how to get the outpuf of env[i] without putting all that other stuff into main()'s signature.
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] -
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?
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 thatmain()
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