Filling an array with command line parameters





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







1















I need to fill an array[2][8] with some words passed as command line parameters (words should be separated with whitespace). I don't know how to connect CLP with my array. What if I have more than 17 letters?



I wrote code just for counting and printing parameters.



#define ROWS 2
#define COLUMNS 8
int main(int argc, char const *argv)
{
int counter;
if(argc > 17)
{
printf("Too many elements!");
return 0;
}
printf("Command line includes %d parameters:n", argc - 1);
for(counter = 1; counter < argc; counter++)
printf("%d: %sn", counter, argv[counter]);
return 0;
}


EDIT : I have to use pointers. Array [2][8] must be filled with words after which we put whitespace. I think that 2x8 array is storing exactly 16 characters, including whitespaces. I don't know how to show when you exceed 16 letters limit.










share|improve this question

























  • I need to fill an array[2][8] does not make sense in light of your other questions. i.e. that array will hold only two, very short strings. Later you ask about a string of size greater than 17. By the way, your code, as is, will work to print any number of parameters, each with any length.

    – ryyker
    Jan 4 at 13:54











  • Is it a real requirement to limit the command line parameters to 17?

    – ryyker
    Jan 4 at 13:55











  • Because array is 2x8 it can store no more than16 characters

    – Michael
    Jan 4 at 14:04











  • But an array with 2 rows of 8 columns each would be limited to holding only 8 char per section. As written, your example does not reference the #defines of ROWS and COLUMNS. Were these included with the intent of creating an array?

    – ryyker
    Jan 4 at 14:08













  • I see your edit, but it still does not provide an example of a set of command line parameters you are expected to capture, or how you are expected to store them. Can you please show the rows and columns of parameters as you are required to store them.?

    – ryyker
    Jan 4 at 14:39




















1















I need to fill an array[2][8] with some words passed as command line parameters (words should be separated with whitespace). I don't know how to connect CLP with my array. What if I have more than 17 letters?



I wrote code just for counting and printing parameters.



#define ROWS 2
#define COLUMNS 8
int main(int argc, char const *argv)
{
int counter;
if(argc > 17)
{
printf("Too many elements!");
return 0;
}
printf("Command line includes %d parameters:n", argc - 1);
for(counter = 1; counter < argc; counter++)
printf("%d: %sn", counter, argv[counter]);
return 0;
}


EDIT : I have to use pointers. Array [2][8] must be filled with words after which we put whitespace. I think that 2x8 array is storing exactly 16 characters, including whitespaces. I don't know how to show when you exceed 16 letters limit.










share|improve this question

























  • I need to fill an array[2][8] does not make sense in light of your other questions. i.e. that array will hold only two, very short strings. Later you ask about a string of size greater than 17. By the way, your code, as is, will work to print any number of parameters, each with any length.

    – ryyker
    Jan 4 at 13:54











  • Is it a real requirement to limit the command line parameters to 17?

    – ryyker
    Jan 4 at 13:55











  • Because array is 2x8 it can store no more than16 characters

    – Michael
    Jan 4 at 14:04











  • But an array with 2 rows of 8 columns each would be limited to holding only 8 char per section. As written, your example does not reference the #defines of ROWS and COLUMNS. Were these included with the intent of creating an array?

    – ryyker
    Jan 4 at 14:08













  • I see your edit, but it still does not provide an example of a set of command line parameters you are expected to capture, or how you are expected to store them. Can you please show the rows and columns of parameters as you are required to store them.?

    – ryyker
    Jan 4 at 14:39
















1












1








1








I need to fill an array[2][8] with some words passed as command line parameters (words should be separated with whitespace). I don't know how to connect CLP with my array. What if I have more than 17 letters?



I wrote code just for counting and printing parameters.



#define ROWS 2
#define COLUMNS 8
int main(int argc, char const *argv)
{
int counter;
if(argc > 17)
{
printf("Too many elements!");
return 0;
}
printf("Command line includes %d parameters:n", argc - 1);
for(counter = 1; counter < argc; counter++)
printf("%d: %sn", counter, argv[counter]);
return 0;
}


EDIT : I have to use pointers. Array [2][8] must be filled with words after which we put whitespace. I think that 2x8 array is storing exactly 16 characters, including whitespaces. I don't know how to show when you exceed 16 letters limit.










share|improve this question
















I need to fill an array[2][8] with some words passed as command line parameters (words should be separated with whitespace). I don't know how to connect CLP with my array. What if I have more than 17 letters?



I wrote code just for counting and printing parameters.



#define ROWS 2
#define COLUMNS 8
int main(int argc, char const *argv)
{
int counter;
if(argc > 17)
{
printf("Too many elements!");
return 0;
}
printf("Command line includes %d parameters:n", argc - 1);
for(counter = 1; counter < argc; counter++)
printf("%d: %sn", counter, argv[counter]);
return 0;
}


EDIT : I have to use pointers. Array [2][8] must be filled with words after which we put whitespace. I think that 2x8 array is storing exactly 16 characters, including whitespaces. I don't know how to show when you exceed 16 letters limit.







c arrays command line






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 4 at 14:33







Michael

















asked Jan 4 at 13:16









MichaelMichael

155




155













  • I need to fill an array[2][8] does not make sense in light of your other questions. i.e. that array will hold only two, very short strings. Later you ask about a string of size greater than 17. By the way, your code, as is, will work to print any number of parameters, each with any length.

    – ryyker
    Jan 4 at 13:54











  • Is it a real requirement to limit the command line parameters to 17?

    – ryyker
    Jan 4 at 13:55











  • Because array is 2x8 it can store no more than16 characters

    – Michael
    Jan 4 at 14:04











  • But an array with 2 rows of 8 columns each would be limited to holding only 8 char per section. As written, your example does not reference the #defines of ROWS and COLUMNS. Were these included with the intent of creating an array?

    – ryyker
    Jan 4 at 14:08













  • I see your edit, but it still does not provide an example of a set of command line parameters you are expected to capture, or how you are expected to store them. Can you please show the rows and columns of parameters as you are required to store them.?

    – ryyker
    Jan 4 at 14:39





















  • I need to fill an array[2][8] does not make sense in light of your other questions. i.e. that array will hold only two, very short strings. Later you ask about a string of size greater than 17. By the way, your code, as is, will work to print any number of parameters, each with any length.

    – ryyker
    Jan 4 at 13:54











  • Is it a real requirement to limit the command line parameters to 17?

    – ryyker
    Jan 4 at 13:55











  • Because array is 2x8 it can store no more than16 characters

    – Michael
    Jan 4 at 14:04











  • But an array with 2 rows of 8 columns each would be limited to holding only 8 char per section. As written, your example does not reference the #defines of ROWS and COLUMNS. Were these included with the intent of creating an array?

    – ryyker
    Jan 4 at 14:08













  • I see your edit, but it still does not provide an example of a set of command line parameters you are expected to capture, or how you are expected to store them. Can you please show the rows and columns of parameters as you are required to store them.?

    – ryyker
    Jan 4 at 14:39



















I need to fill an array[2][8] does not make sense in light of your other questions. i.e. that array will hold only two, very short strings. Later you ask about a string of size greater than 17. By the way, your code, as is, will work to print any number of parameters, each with any length.

– ryyker
Jan 4 at 13:54





I need to fill an array[2][8] does not make sense in light of your other questions. i.e. that array will hold only two, very short strings. Later you ask about a string of size greater than 17. By the way, your code, as is, will work to print any number of parameters, each with any length.

– ryyker
Jan 4 at 13:54













Is it a real requirement to limit the command line parameters to 17?

– ryyker
Jan 4 at 13:55





Is it a real requirement to limit the command line parameters to 17?

– ryyker
Jan 4 at 13:55













Because array is 2x8 it can store no more than16 characters

– Michael
Jan 4 at 14:04





Because array is 2x8 it can store no more than16 characters

– Michael
Jan 4 at 14:04













But an array with 2 rows of 8 columns each would be limited to holding only 8 char per section. As written, your example does not reference the #defines of ROWS and COLUMNS. Were these included with the intent of creating an array?

– ryyker
Jan 4 at 14:08







But an array with 2 rows of 8 columns each would be limited to holding only 8 char per section. As written, your example does not reference the #defines of ROWS and COLUMNS. Were these included with the intent of creating an array?

– ryyker
Jan 4 at 14:08















I see your edit, but it still does not provide an example of a set of command line parameters you are expected to capture, or how you are expected to store them. Can you please show the rows and columns of parameters as you are required to store them.?

– ryyker
Jan 4 at 14:39







I see your edit, but it still does not provide an example of a set of command line parameters you are expected to capture, or how you are expected to store them. Can you please show the rows and columns of parameters as you are required to store them.?

– ryyker
Jan 4 at 14:39














1 Answer
1






active

oldest

votes


















2














What if I have more than 17 letters?



argc is an abbreviation of argument count, so the statement: if(argc > 17) is in effect testing the number of command line arguments, not the count of characters in each one.



Additionally, the argument char *argv ( where argv is short for argument vector ) is typed to accommodate any number of char for each command line argument.



Filling an array with command line parameters

If you wanted to capture the contents of your command line arguments into an array, either a dynamically allocated set of buffers, or preferably a variable length array (available from C99 on) are suitable for the task. The dimensions of the array (of either type) can be obtained using argc (the number of arguments) and strlen of argv[i] in a loop to get the longest length of all of the arguments. An example of this technique addresses your title question below.



Example of using a VLA:



int main(int argc, char const *argv)
{
int counter;
int len, maxLen=0;

// find the longest length parameter
for(counter = 0; counter < argc; counter++)
{
len = strlen(argv[counter]);
if(len > maxLen) maxLen = len;
}

//using a variable length array, create a container for all parameters
char array[argc][maxLen + 1];// +1 to allow room for null terminator

// transfer contents of argv to VLA array
for(counter = 0; counter < argc; counter++)
{
strcpy(array[counter], argv[counter]);//copy CLPs into array
}

printf("Command line includes %d parameters:n", argc - 1);
for(counter = 1; counter < argc; counter++)
printf("%d: %sn", counter, array[counter]);
return 0;
}


Example output for the arguments" this that and the other thing andareallylongargumenttoshowthatargvcanaccomodate.
enter image description here



Example output Using arguments with white space:

"this that" and the "other thing" andareallylongargumenttoshowthatargvcanaccomodate
enter image description here



EDIT to address clarifications in comments.



The following test for (and limits) max number of arguments to ROWS , and allows length limits of arguments to exceed COLUMNS-1, but trims to length if too long. If string is less than COLUMNS-1 long, pads remaining space with &. If string contains any white space, replaces it with &...



#define ROWS 2
#define COLUMNS 8
int main(int argc, char const *argv)
{
int counter, i;

char array[ROWS][COLUMNS];

if(argc > 3)
{
printf("Too many arguments. 2 max.n(Hit any character to exit.)");
getchar();
return 0;
}


for(counter = 0; counter < argc-1; counter++)
{ //trim to legal string length.
strncpy(array[counter], argv[counter+1], COLUMNS-1);
array[counter][COLUMNS-1]=0;//set last char to null
for(i=0;i<COLUMNS-1;i++)
{ //test for any white space or NULL
//character within legal string length
if((isspace(array[counter][i])) || array[counter][i] == NULL) array[counter][i] = '&';
}
}

printf("Command line includes %d parameters:n", argc - 1);
for(counter = 0; counter < argc-1; counter++)
printf("%d: %sn", counter, array[counter]);
return 0;
}


Results for example parameters - echo.c "c language is ok":
enter image description here






share|improve this answer


























  • But still I can fill array with as many words and letters as I want to.

    – Michael
    Jan 4 at 14:23











  • I wanted to fill 1 gap in array with 1 letter

    – Michael
    Jan 4 at 14:23











  • @Michael - can you please edit your question above with an example of what you want your output to look like. I will try to address the question once I can see that edit, and understand what you need.

    – ryyker
    Jan 4 at 14:25











  • Basically my task is to create array[2][8] and fill it with words

    – Michael
    Jan 4 at 14:36






  • 1





    That's all I needed. I'm very grateful you wanted to spend time on helping me.

    – Michael
    Jan 4 at 16:01












Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54039687%2ffilling-an-array-with-command-line-parameters%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














What if I have more than 17 letters?



argc is an abbreviation of argument count, so the statement: if(argc > 17) is in effect testing the number of command line arguments, not the count of characters in each one.



Additionally, the argument char *argv ( where argv is short for argument vector ) is typed to accommodate any number of char for each command line argument.



Filling an array with command line parameters

If you wanted to capture the contents of your command line arguments into an array, either a dynamically allocated set of buffers, or preferably a variable length array (available from C99 on) are suitable for the task. The dimensions of the array (of either type) can be obtained using argc (the number of arguments) and strlen of argv[i] in a loop to get the longest length of all of the arguments. An example of this technique addresses your title question below.



Example of using a VLA:



int main(int argc, char const *argv)
{
int counter;
int len, maxLen=0;

// find the longest length parameter
for(counter = 0; counter < argc; counter++)
{
len = strlen(argv[counter]);
if(len > maxLen) maxLen = len;
}

//using a variable length array, create a container for all parameters
char array[argc][maxLen + 1];// +1 to allow room for null terminator

// transfer contents of argv to VLA array
for(counter = 0; counter < argc; counter++)
{
strcpy(array[counter], argv[counter]);//copy CLPs into array
}

printf("Command line includes %d parameters:n", argc - 1);
for(counter = 1; counter < argc; counter++)
printf("%d: %sn", counter, array[counter]);
return 0;
}


Example output for the arguments" this that and the other thing andareallylongargumenttoshowthatargvcanaccomodate.
enter image description here



Example output Using arguments with white space:

"this that" and the "other thing" andareallylongargumenttoshowthatargvcanaccomodate
enter image description here



EDIT to address clarifications in comments.



The following test for (and limits) max number of arguments to ROWS , and allows length limits of arguments to exceed COLUMNS-1, but trims to length if too long. If string is less than COLUMNS-1 long, pads remaining space with &. If string contains any white space, replaces it with &...



#define ROWS 2
#define COLUMNS 8
int main(int argc, char const *argv)
{
int counter, i;

char array[ROWS][COLUMNS];

if(argc > 3)
{
printf("Too many arguments. 2 max.n(Hit any character to exit.)");
getchar();
return 0;
}


for(counter = 0; counter < argc-1; counter++)
{ //trim to legal string length.
strncpy(array[counter], argv[counter+1], COLUMNS-1);
array[counter][COLUMNS-1]=0;//set last char to null
for(i=0;i<COLUMNS-1;i++)
{ //test for any white space or NULL
//character within legal string length
if((isspace(array[counter][i])) || array[counter][i] == NULL) array[counter][i] = '&';
}
}

printf("Command line includes %d parameters:n", argc - 1);
for(counter = 0; counter < argc-1; counter++)
printf("%d: %sn", counter, array[counter]);
return 0;
}


Results for example parameters - echo.c "c language is ok":
enter image description here






share|improve this answer


























  • But still I can fill array with as many words and letters as I want to.

    – Michael
    Jan 4 at 14:23











  • I wanted to fill 1 gap in array with 1 letter

    – Michael
    Jan 4 at 14:23











  • @Michael - can you please edit your question above with an example of what you want your output to look like. I will try to address the question once I can see that edit, and understand what you need.

    – ryyker
    Jan 4 at 14:25











  • Basically my task is to create array[2][8] and fill it with words

    – Michael
    Jan 4 at 14:36






  • 1





    That's all I needed. I'm very grateful you wanted to spend time on helping me.

    – Michael
    Jan 4 at 16:01
















2














What if I have more than 17 letters?



argc is an abbreviation of argument count, so the statement: if(argc > 17) is in effect testing the number of command line arguments, not the count of characters in each one.



Additionally, the argument char *argv ( where argv is short for argument vector ) is typed to accommodate any number of char for each command line argument.



Filling an array with command line parameters

If you wanted to capture the contents of your command line arguments into an array, either a dynamically allocated set of buffers, or preferably a variable length array (available from C99 on) are suitable for the task. The dimensions of the array (of either type) can be obtained using argc (the number of arguments) and strlen of argv[i] in a loop to get the longest length of all of the arguments. An example of this technique addresses your title question below.



Example of using a VLA:



int main(int argc, char const *argv)
{
int counter;
int len, maxLen=0;

// find the longest length parameter
for(counter = 0; counter < argc; counter++)
{
len = strlen(argv[counter]);
if(len > maxLen) maxLen = len;
}

//using a variable length array, create a container for all parameters
char array[argc][maxLen + 1];// +1 to allow room for null terminator

// transfer contents of argv to VLA array
for(counter = 0; counter < argc; counter++)
{
strcpy(array[counter], argv[counter]);//copy CLPs into array
}

printf("Command line includes %d parameters:n", argc - 1);
for(counter = 1; counter < argc; counter++)
printf("%d: %sn", counter, array[counter]);
return 0;
}


Example output for the arguments" this that and the other thing andareallylongargumenttoshowthatargvcanaccomodate.
enter image description here



Example output Using arguments with white space:

"this that" and the "other thing" andareallylongargumenttoshowthatargvcanaccomodate
enter image description here



EDIT to address clarifications in comments.



The following test for (and limits) max number of arguments to ROWS , and allows length limits of arguments to exceed COLUMNS-1, but trims to length if too long. If string is less than COLUMNS-1 long, pads remaining space with &. If string contains any white space, replaces it with &...



#define ROWS 2
#define COLUMNS 8
int main(int argc, char const *argv)
{
int counter, i;

char array[ROWS][COLUMNS];

if(argc > 3)
{
printf("Too many arguments. 2 max.n(Hit any character to exit.)");
getchar();
return 0;
}


for(counter = 0; counter < argc-1; counter++)
{ //trim to legal string length.
strncpy(array[counter], argv[counter+1], COLUMNS-1);
array[counter][COLUMNS-1]=0;//set last char to null
for(i=0;i<COLUMNS-1;i++)
{ //test for any white space or NULL
//character within legal string length
if((isspace(array[counter][i])) || array[counter][i] == NULL) array[counter][i] = '&';
}
}

printf("Command line includes %d parameters:n", argc - 1);
for(counter = 0; counter < argc-1; counter++)
printf("%d: %sn", counter, array[counter]);
return 0;
}


Results for example parameters - echo.c "c language is ok":
enter image description here






share|improve this answer


























  • But still I can fill array with as many words and letters as I want to.

    – Michael
    Jan 4 at 14:23











  • I wanted to fill 1 gap in array with 1 letter

    – Michael
    Jan 4 at 14:23











  • @Michael - can you please edit your question above with an example of what you want your output to look like. I will try to address the question once I can see that edit, and understand what you need.

    – ryyker
    Jan 4 at 14:25











  • Basically my task is to create array[2][8] and fill it with words

    – Michael
    Jan 4 at 14:36






  • 1





    That's all I needed. I'm very grateful you wanted to spend time on helping me.

    – Michael
    Jan 4 at 16:01














2












2








2







What if I have more than 17 letters?



argc is an abbreviation of argument count, so the statement: if(argc > 17) is in effect testing the number of command line arguments, not the count of characters in each one.



Additionally, the argument char *argv ( where argv is short for argument vector ) is typed to accommodate any number of char for each command line argument.



Filling an array with command line parameters

If you wanted to capture the contents of your command line arguments into an array, either a dynamically allocated set of buffers, or preferably a variable length array (available from C99 on) are suitable for the task. The dimensions of the array (of either type) can be obtained using argc (the number of arguments) and strlen of argv[i] in a loop to get the longest length of all of the arguments. An example of this technique addresses your title question below.



Example of using a VLA:



int main(int argc, char const *argv)
{
int counter;
int len, maxLen=0;

// find the longest length parameter
for(counter = 0; counter < argc; counter++)
{
len = strlen(argv[counter]);
if(len > maxLen) maxLen = len;
}

//using a variable length array, create a container for all parameters
char array[argc][maxLen + 1];// +1 to allow room for null terminator

// transfer contents of argv to VLA array
for(counter = 0; counter < argc; counter++)
{
strcpy(array[counter], argv[counter]);//copy CLPs into array
}

printf("Command line includes %d parameters:n", argc - 1);
for(counter = 1; counter < argc; counter++)
printf("%d: %sn", counter, array[counter]);
return 0;
}


Example output for the arguments" this that and the other thing andareallylongargumenttoshowthatargvcanaccomodate.
enter image description here



Example output Using arguments with white space:

"this that" and the "other thing" andareallylongargumenttoshowthatargvcanaccomodate
enter image description here



EDIT to address clarifications in comments.



The following test for (and limits) max number of arguments to ROWS , and allows length limits of arguments to exceed COLUMNS-1, but trims to length if too long. If string is less than COLUMNS-1 long, pads remaining space with &. If string contains any white space, replaces it with &...



#define ROWS 2
#define COLUMNS 8
int main(int argc, char const *argv)
{
int counter, i;

char array[ROWS][COLUMNS];

if(argc > 3)
{
printf("Too many arguments. 2 max.n(Hit any character to exit.)");
getchar();
return 0;
}


for(counter = 0; counter < argc-1; counter++)
{ //trim to legal string length.
strncpy(array[counter], argv[counter+1], COLUMNS-1);
array[counter][COLUMNS-1]=0;//set last char to null
for(i=0;i<COLUMNS-1;i++)
{ //test for any white space or NULL
//character within legal string length
if((isspace(array[counter][i])) || array[counter][i] == NULL) array[counter][i] = '&';
}
}

printf("Command line includes %d parameters:n", argc - 1);
for(counter = 0; counter < argc-1; counter++)
printf("%d: %sn", counter, array[counter]);
return 0;
}


Results for example parameters - echo.c "c language is ok":
enter image description here






share|improve this answer















What if I have more than 17 letters?



argc is an abbreviation of argument count, so the statement: if(argc > 17) is in effect testing the number of command line arguments, not the count of characters in each one.



Additionally, the argument char *argv ( where argv is short for argument vector ) is typed to accommodate any number of char for each command line argument.



Filling an array with command line parameters

If you wanted to capture the contents of your command line arguments into an array, either a dynamically allocated set of buffers, or preferably a variable length array (available from C99 on) are suitable for the task. The dimensions of the array (of either type) can be obtained using argc (the number of arguments) and strlen of argv[i] in a loop to get the longest length of all of the arguments. An example of this technique addresses your title question below.



Example of using a VLA:



int main(int argc, char const *argv)
{
int counter;
int len, maxLen=0;

// find the longest length parameter
for(counter = 0; counter < argc; counter++)
{
len = strlen(argv[counter]);
if(len > maxLen) maxLen = len;
}

//using a variable length array, create a container for all parameters
char array[argc][maxLen + 1];// +1 to allow room for null terminator

// transfer contents of argv to VLA array
for(counter = 0; counter < argc; counter++)
{
strcpy(array[counter], argv[counter]);//copy CLPs into array
}

printf("Command line includes %d parameters:n", argc - 1);
for(counter = 1; counter < argc; counter++)
printf("%d: %sn", counter, array[counter]);
return 0;
}


Example output for the arguments" this that and the other thing andareallylongargumenttoshowthatargvcanaccomodate.
enter image description here



Example output Using arguments with white space:

"this that" and the "other thing" andareallylongargumenttoshowthatargvcanaccomodate
enter image description here



EDIT to address clarifications in comments.



The following test for (and limits) max number of arguments to ROWS , and allows length limits of arguments to exceed COLUMNS-1, but trims to length if too long. If string is less than COLUMNS-1 long, pads remaining space with &. If string contains any white space, replaces it with &...



#define ROWS 2
#define COLUMNS 8
int main(int argc, char const *argv)
{
int counter, i;

char array[ROWS][COLUMNS];

if(argc > 3)
{
printf("Too many arguments. 2 max.n(Hit any character to exit.)");
getchar();
return 0;
}


for(counter = 0; counter < argc-1; counter++)
{ //trim to legal string length.
strncpy(array[counter], argv[counter+1], COLUMNS-1);
array[counter][COLUMNS-1]=0;//set last char to null
for(i=0;i<COLUMNS-1;i++)
{ //test for any white space or NULL
//character within legal string length
if((isspace(array[counter][i])) || array[counter][i] == NULL) array[counter][i] = '&';
}
}

printf("Command line includes %d parameters:n", argc - 1);
for(counter = 0; counter < argc-1; counter++)
printf("%d: %sn", counter, array[counter]);
return 0;
}


Results for example parameters - echo.c "c language is ok":
enter image description here







share|improve this answer














share|improve this answer



share|improve this answer








edited Jan 4 at 16:14

























answered Jan 4 at 13:24









ryykerryyker

12.7k22959




12.7k22959













  • But still I can fill array with as many words and letters as I want to.

    – Michael
    Jan 4 at 14:23











  • I wanted to fill 1 gap in array with 1 letter

    – Michael
    Jan 4 at 14:23











  • @Michael - can you please edit your question above with an example of what you want your output to look like. I will try to address the question once I can see that edit, and understand what you need.

    – ryyker
    Jan 4 at 14:25











  • Basically my task is to create array[2][8] and fill it with words

    – Michael
    Jan 4 at 14:36






  • 1





    That's all I needed. I'm very grateful you wanted to spend time on helping me.

    – Michael
    Jan 4 at 16:01



















  • But still I can fill array with as many words and letters as I want to.

    – Michael
    Jan 4 at 14:23











  • I wanted to fill 1 gap in array with 1 letter

    – Michael
    Jan 4 at 14:23











  • @Michael - can you please edit your question above with an example of what you want your output to look like. I will try to address the question once I can see that edit, and understand what you need.

    – ryyker
    Jan 4 at 14:25











  • Basically my task is to create array[2][8] and fill it with words

    – Michael
    Jan 4 at 14:36






  • 1





    That's all I needed. I'm very grateful you wanted to spend time on helping me.

    – Michael
    Jan 4 at 16:01

















But still I can fill array with as many words and letters as I want to.

– Michael
Jan 4 at 14:23





But still I can fill array with as many words and letters as I want to.

– Michael
Jan 4 at 14:23













I wanted to fill 1 gap in array with 1 letter

– Michael
Jan 4 at 14:23





I wanted to fill 1 gap in array with 1 letter

– Michael
Jan 4 at 14:23













@Michael - can you please edit your question above with an example of what you want your output to look like. I will try to address the question once I can see that edit, and understand what you need.

– ryyker
Jan 4 at 14:25





@Michael - can you please edit your question above with an example of what you want your output to look like. I will try to address the question once I can see that edit, and understand what you need.

– ryyker
Jan 4 at 14:25













Basically my task is to create array[2][8] and fill it with words

– Michael
Jan 4 at 14:36





Basically my task is to create array[2][8] and fill it with words

– Michael
Jan 4 at 14:36




1




1





That's all I needed. I'm very grateful you wanted to spend time on helping me.

– Michael
Jan 4 at 16:01





That's all I needed. I'm very grateful you wanted to spend time on helping me.

– Michael
Jan 4 at 16:01




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54039687%2ffilling-an-array-with-command-line-parameters%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Mossoró

Error while reading .h5 file using the rhdf5 package in R

Pushsharp Apns notification error: 'InvalidToken'