(C ++) Very simple questions regarding syntax

C ++ starter here. I have some basic questions. In int main( int argc, char *argv[] )

  • How is char *argv[] supposed to read (or tell people)?
  • Is it possible to clear / delete specific content (s), a symbol in this case, such an array? If so, how?
  • Can I resize an array? If so, how?
  • How to copy all argv[] content into one std::string variable?
  • Are there other ways to determine the number of words / parameters in argv[] without argc ? If so, how? (*)

I would be grateful (not a code) for the numbers 2-5. I will figure out the code myself (I find out so quickly).

Thanks in advance.

(*) I know that main(char *argv[]) is illegal. I mean, is there at least one way that does not include argc at all, as in the following expressions:

 for( int i = 0; i < argc; ++i ) { std::cout << argv[i] << std::endl; } 

and

 int i = 0; while( i < argc ) { std::cout << argv[i] << std::endl; ++i; } 

or

 int i = 0; do { std::cout << argv[i] << std::endl; ++i; } while( i < argc ); 
+7
source share
7 answers

1) It is assumed that char **argv or char *argv[] , which a pointer to an array of characters better known as an array of strings

2) CString is a std library for managing C strings (character arrays). You cannot resize an array without redistributing, but you can resize the contents of elements by specifying it by index:

 for(int i = 0; i < argc; ++i) { //set all the strings to have a null character in the //first slot so all Cstring operations on this array, //will consider it a null (empty) string argv[i] = 0; } 


3) Technically not, however they can be removed and then redistributed:

 int *array = new int[15]; //array of size 15 delete[] array; array = new int[50]; //array of size 50 

4) This is one way:

 string *myString; if(argc > 0) { myString = new string(argv[0]); for(int i = 1; i < argc; ++i) myString->append(argv[i]); } 

5) Yes, according to Cubby:

POSIX points to a trailing null pointer for argv, see, for example, "The application must ensure that the last member of this array is a null pointer." pubs.opengroup.org/onlinepubs/9699919799/functions/exec.html

What can you do:

 char *val = NULL; int i = 0; do { val = argv[i++]; //access argv[i], store it in val, then increment i //do something with val } while(val != NULL); //loop until end of argv array 
0
source
  • This is an array of pointers to char.

  • Sort - you can overwrite them.

  • Only by copying to a new array.

  • Write a loop and add each argv [i] to the C ++ line.

  • Most implementations terminate the array with a NULL pointer. I don’t remember whether this is standard or not.

+2
source
 char **argv[] 

Wrong. It should be either char **argv or char *argv[] , and not a mixture of both. And then it becomes a pointer to a pointer to characters, or rather a pointer to c-lines, i.e. An array of c-lines. :) cdecl.org is also quite useful in such a thing.
Then, for access, of course. Easy, well, access to it. :) argv[0] will be the first line, argv[3] will be the fourth line. But I totally do not recommend replacing material in an array that does not belong to you, or that you know its insides.
When resizing an array, as you write C ++, use std::vector , which makes all the distribution difficult for you and really safe. This usually depends on the type of array. Dynamically allocated arrays ( int* int_arr = new int[20] ) can, static arrays ( int int_arr[20] ) cannot.
To copy everything in argv into one std::string , go through the array and add each c-string to your std::string . I would not recommend this, rather, have std::vector<std::string> , i.e. An array from std::strings , each of which contains one of the arguments.

 std::vector<std::string> args; for(int i=0; i < argc; ++i){ args.push_back(argv[i]); } 

In the last paragraph, since the standard requires argv to complete with a NULL pointer, this is pretty simple.

 int myargc = 0; char** argv_copy = argv; while(++argv_copy) ++myargc; 

while(++argv_copy) first increment the array pointer, pointing to the next element (for example, after the first iteration, it will point to c-string # 2 ( argv[1] )). After that, if the pointer evaluates to false (if it's NULL ), then the loop stops and you have myargc . :)

+1
source
  • Several parameters: an array of pointer to char OR an array of C-string.

  • You can assign specific characters to clear them, or you can move the rest of the array forward to β€œerase” characters / elements.

  • Regular C-style arrays cannot be modified. If you need to resize the array in C ++, you should use std::vector .

  • You will have to iterate over all the elements and add them to the string. This can be done using C ++ algorithms such as copy in combination with ostream_iterator used in ostringstream .

  • Not. If there was such a method, argc would not be necessary. EDIT: Apparently, for argv only the final element of the array is a null pointer.

+1
source
  • char *argv[] can be read as: "an array of pointers to char"

    char **argv can be read as: "pointer to a pointer to char"

  • Yes, you can change the argv array. For example, argv[0][0] = 'H' will change the first character of the first parameter. If you mean "erase / clear", you will delete the character from the array and everything will automatically switch: there is no need to do this automatically β€” you will need to copy all the characters one by one from the left (including NULL)

  • No, arrays cannot be changed. You will need to create a new one and copy the contents

  • How do you want to represent ALL parameter strings as 1 std :: string? It would be more appropriate to copy it to an array of std :: strings

  • No, there is no special indication of the last record in the array. you need to use argc

0
source
  • They say it as "an array of pointers to pointers to a character" (note that this is not the signature of the main function, which is either int argc, char **argv , or int argc, char *argv[] ), which is equivalent).
  • The argv array is being modified (missing const). It is impossible to write at the end of one of the lines or expand the array; if you need to expand a string, create a copy of it and save the pointer in an array; if you need to expand the array, create a copy of the array.
  • They cannot be changed on their own, but are reinterpreted as a smaller array (which explains the answer to the last question).
  • You will lose information this way - argv is an array of arrays because the individual arguments are already allocated to you. You can create a list of strings using std::list<std::string> args(&argv[1], &argv[argc]); .
  • Not really. On most systems, argv NULL complete, but this is not a guarantee.
0
source

An array in C / C ++ is not an object, but simply a pointer to the first element of the array, so you cannot just delete or insert values.

Answering your questions:

  • char *argv[] can be read as "an array of pointers to char "
  • This is possible, but is associated with direct manipulation of data in memory, such as copying and / or moving bytes.
  • Not. But you can select a new array and copy the necessary data.
  • By manually copying each element to an std::string object
  • Not.

As a summary: C ++ is a much lower level language that you consider.

-one
source

All Articles