Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I populate a new array with strings passed as command line arguments in argv[]?

I am relatively new to coding, especially in C. I am attempting to create a simple program designed to take a phrase, or list of strings, as command line arguments that populate char* argv[]. I then want to perform simple ASCII based changes to each character (such as making a => b, b => c, … z => a etc.), and then populate a new array with the altered strings. Essentially, the new object would be an array of arrays of characters, just like I believe argv[] to be.

Here is my code, although I have been bombarded with a variety of "pointer incompatible conversion assignment **char to string int char variable…..Segmentation fault" style errors.

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


int main(int argc, char* argv[])  
{  
    
    char* cipher[argc];  
    for (int i = 1; i < argc; i++)  
    {  
        int n = strlen(argv[i]);  
        for (int j = 0; j < n-1; j++)  
        {  
            cipher[i][j] = argv[i][j];  
        }  
    }  
    for (int i = 0; i<argc-1; i++)  
    {  
        printf("%s\n", cipher[i]);  
    }  
}  

The code would be run via command line as follows:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

./secret the man in the middle is guilty

And then return a scrambled form of this. I have omitted the scrambling algorithm because this was not something I was having issues with.

I understand that I could more easily create code that prints the altered message, but I want to be able to store the new message in an array in case of further alterations. I’m not understanding why I can’t just access the values of characters of specific 2d indexes and populate the new "cipher" array with them.

What am I not understanding?

>Solution :

Bunch of issues:


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


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

    // first issue: this is only allocating space for "pointers" to
    // strings [argc many] but no space for actual strings so you have to 
    // allocate them.


    // also this is only really workable with c99 (assuming that's the case)
    char* cipher[argc];  

    // this is okay 
    for (int i = 1; i < argc; i++)  
    {  

       // this is where you would allocate space and 
       // perform the copy
        int n = strlen(argv[i]);  
        for (int j = 0; j < n-1; j++)  
        {  
            cipher[i][j] = argv[i][j];  
        }  
    }

   
    for (int i = 0; i<argc-1; i++)  
    {  
        printf("%s\n", cipher[i]);  
    }  
}  


So at least minimally:

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

int main(int argc, char* argv[])  
{  
   
    char* cipher[argc];

    // this is okay 
    for (int i = 1; i < argc; i++)  
    {  
         // strdup will return a newly allocated copy
         // and since cipher[] is an array of pointers 
         // you are good to go.
         cipher[i-1] = strdup(argv[i]);
    }

   
    for (int i = 0; i<argc-1; i++)  
    {  
        printf("%s\n", cipher[i]);  
    }  

    // later on after you are finished with cipher 
    // assuming that you still have other things to do
    // you can de-allocate the memory (although not sure if
    // that will be required)

    for(int i = 0; i < argc - 1; i++) {
         free(cipher[i]); // strdup allocated memory cleanup
    }

    // you can no longer use cipher[i] here because it's contents
    // have been destroyed by the free above.
}  
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading