'Extracting elements off an array of pointers
Suppose we have this main:
int main(int argc,char *argv[]){
}
How could I manage to get the elements of *argv[],in order to extract some statistics off of each one(e.g letter count,numbers ,etc.)?
I have tried couple of things but didn't work(using pointers ).Eventually I tried a work-around,using strcpy() to copy each element of the array and that worked. So my question is,are there other ways to achieve that?
Here's an example of my code that works:
int main(int argc,char *argv[]){
char temp[50];
strcpy(temp,argv[1]); //extracting the first element of the array.
Solution 1:[1]
Another way of doing so is as below:
int main(int argc,char argv[]){
char *temp = argv;
}
This code will create a string named "temp" which is a copy of "argv[]". You can use to extract any stats you like without having to use the "strcopy" function.
Also, take note that I have changed the argument from
int main(int argc, char *argv[])
to
int main(int argc, char argv[])
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Om Kaushik |
