What is an environment vector? [closed] - c++

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am writing a short program that displays the argument count (argc), argument vector(argv[]), and environment vector. However, I am unsure how to display the "environment vector" or what it is.

The "environment" parameter, traditionally named envp, is a zero-terminated array of char*.
You can display it like this:
int main(int argc, char* argv[], char* envp[])
{
while (*envp)
{
std::cout << *envp << std::endl;
envp++;
}
}
It's not part of POSIX (or any other standard) but supported by many compilers.

Related

How can make a program typing with c++? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
How can make a program typing with c++?
For example with (char) can type a grapheme but i want type word.
char x;
x=='c';
But i want type for example 'equation'.
Someone know how can i do this?
Use a std::string. You can also use char* but i recommend you the first one. Remember that string goes within "" instead of ''.
string x; //You have to define what is 'x'.
x=="equation";
If you use char*, it behaves as an array of chars.
#include <string> // paste this line on top of the file
std::string name = "Bob"; // Use double quotes here.
if(name == "Bob")
{
std::cout << name << std::endl; // works as you expected
}

How do I parse with respect to char array indices (C++) [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
How do I parse a char array such as:
"2019-11-01T17:00:10.000Z"
I want 17:00:10.000 and nothing else. How do I parse with respect to the char array indices?
The simplest and safest way to deal with strings is to use the standard std::string. The std::string has many class member functions that can help with this. One of them is substr that can be used to create a new std::string from a part of a string.
Example:
#include <iostream>
#include <stdexcept>
#include <string>
std::string get_time_str(const std::string& timestamp) {
if(timestamp.size() != 24)
throw std::runtime_error("bad timestamp: "+ timestamp);
return timestamp.substr(11, 12); // start at pos 11 and 12 chars forward
}
int main() {
const char* ts = "2019-11-01T17:00:10.000Z";
std::cout << get_time_str(ts) << '\n';
}
Output:
17:00:10.000

What is the difference between pointers to array of pointers? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I came across these two terms
Int (*q)[3][4] and. int q[ ][3][4].
What's the difference these two terms?
And one more question .
Char a[ ]="abcd";
Char *p="abv";
a="ghj";
p="ajk";
Printf("℅s℅s",a,p);
Why this would not compile?
It won't compile because of the line:
a = "ghi"
This assigns a const char* to a char* directly. You can use strcpy to copy the string intead:
strcpy(a, "ghi")
You will still have warnings at this point, because you have not declared p as a const. You can fix this like so:
const char* p = "abv"

Modify a char* in C++ [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am trying to modify a c string in C++.
void modify(char* s)
{
s[0] = 'a';
}
If I do this, there will be some undefined behavior and can't run.
Let's assume s[0] is valid. I know char* s is immutable. Is there any possibility that I can modify the s[0] in place, which means, without creating a new string. Do the modification on the original string.
I think you might be misunderstanding some other answers you have seen on the web.
It is only undefined behavior to modify a string constant, not any char*.
As long as you strdup the constant string into a non-constant string, you can make whatever changes you want with it, because it is now in a mutable region of memory.
#include <stdio.h>
#include <string.h>
void modString(char* changeMe) {
changeMe[0] = 'g';
}
int main(){
char* foo = strdup("food");
puts(foo);
modString(foo);
puts(foo);
free(foo);
}

type casting the arguments of a function [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
If the code is like the below,
void func(std::string str)
{
...
}
void main()
{
std::string p1 = "abcd";
char p2[SOME_LENGTH] = "abcd";
func(p1); // (1)
func(p2); // (2)
}
which way is efficient between (1) and (2)?
They are equally efficient/inefficient. Both involves copying the string and using the copy as the value of the argument 'str'. A better way would be declaring func as
void func(const std::string &str) {
}
This can avoid copying of the string.