What I am trying to do is add a command line argument into an array as indvidual characters.
So when the user runs the program ./program bacon “bacon” is stored in an array as
array k[]= {'b', 'a', 'c', 'o', 'n'};
I hope I explained it well enough I am new to programming.
So, I understand you’re new to programming, but surely this looks familiar, no?
char **argvis a pointer to a char pointer, but for your purposes, you can consider it to be the equivalent ofchar *argv[]. The difference is subtle, but worth noting, since this caveat is vital to understanding the way strings work in C.char *argv[]is explicitly typed as an array of char pointers, whilechar **argvcould be an array but you wouldn’t know until you try to access it as such. Given that it’s your main function, it’s safe to assume this will always be instantiated appropriately.Anyway, moving past the tangent, we have an array of null-terminated strings in
char **argvin our main function. From your question, I can see a simple path we should follow. I will assume that only one argument is expected (you should otherwise be capable of implementing cases which deal with different circumstances).argv[1]) passed to our program.argv[1].In our
mainfunction, we store the length ofargv[1]ton, and declare our array of sizen. We then iterate through the first string, character-by-character, and store each character into the next open slot of our array. At the end, we repeat our loop and print out each item of our array so we can verify it works.Hope this helps. Cheers.