C: Store parameter passed when calling main
I would like to know how to store parameters in C when I compile:
For example: I'd like to store 2 user-input string variables.
The main is called like this:
./main "Hello World!" World
We should store "Hello World!" in string1
and "World" in string2
(supposing we can only use the main function and no head function), without using pointers.
edit: here is my code that still doesn't work:
#include <stdio.h>
#include <err.h>
int
main (int argc, char *argv)
{
if (argc != 2)
{
errx(1, "Error");
}
printf("Number of arguments = %in", argc);
for (int k = 0; k < argc; k += 1)
{
printf("argv[%i] = %sn", i, argv[i]);
}
return 0;
}
c string variables
|
show 2 more comments
I would like to know how to store parameters in C when I compile:
For example: I'd like to store 2 user-input string variables.
The main is called like this:
./main "Hello World!" World
We should store "Hello World!" in string1
and "World" in string2
(supposing we can only use the main function and no head function), without using pointers.
edit: here is my code that still doesn't work:
#include <stdio.h>
#include <err.h>
int
main (int argc, char *argv)
{
if (argc != 2)
{
errx(1, "Error");
}
printf("Number of arguments = %in", argc);
for (int k = 0; k < argc; k += 1)
{
printf("argv[%i] = %sn", i, argv[i]);
}
return 0;
}
c string variables
1
Can you post any code that you have tried? Or any error messages you get when you run your code?
– ahota
Nov 19 '18 at 15:17
I cannot test it since I cannot store the variables
– quiasdesamis2
Nov 19 '18 at 15:18
Is it possible to do this without using pointers (at all)
– quiasdesamis2
Nov 19 '18 at 15:19
Mr Bowling I have seen this but didn't understand how the two strings would be stored, how ?
– quiasdesamis2
Nov 19 '18 at 15:20
1
You can't do this in C without using pointers. You'll need them to pass arguments from the command line and for the strings.
– sergiopm
Nov 19 '18 at 15:42
|
show 2 more comments
I would like to know how to store parameters in C when I compile:
For example: I'd like to store 2 user-input string variables.
The main is called like this:
./main "Hello World!" World
We should store "Hello World!" in string1
and "World" in string2
(supposing we can only use the main function and no head function), without using pointers.
edit: here is my code that still doesn't work:
#include <stdio.h>
#include <err.h>
int
main (int argc, char *argv)
{
if (argc != 2)
{
errx(1, "Error");
}
printf("Number of arguments = %in", argc);
for (int k = 0; k < argc; k += 1)
{
printf("argv[%i] = %sn", i, argv[i]);
}
return 0;
}
c string variables
I would like to know how to store parameters in C when I compile:
For example: I'd like to store 2 user-input string variables.
The main is called like this:
./main "Hello World!" World
We should store "Hello World!" in string1
and "World" in string2
(supposing we can only use the main function and no head function), without using pointers.
edit: here is my code that still doesn't work:
#include <stdio.h>
#include <err.h>
int
main (int argc, char *argv)
{
if (argc != 2)
{
errx(1, "Error");
}
printf("Number of arguments = %in", argc);
for (int k = 0; k < argc; k += 1)
{
printf("argv[%i] = %sn", i, argv[i]);
}
return 0;
}
c string variables
c string variables
edited Nov 19 '18 at 16:14
asked Nov 19 '18 at 15:12
quiasdesamis2
32
32
1
Can you post any code that you have tried? Or any error messages you get when you run your code?
– ahota
Nov 19 '18 at 15:17
I cannot test it since I cannot store the variables
– quiasdesamis2
Nov 19 '18 at 15:18
Is it possible to do this without using pointers (at all)
– quiasdesamis2
Nov 19 '18 at 15:19
Mr Bowling I have seen this but didn't understand how the two strings would be stored, how ?
– quiasdesamis2
Nov 19 '18 at 15:20
1
You can't do this in C without using pointers. You'll need them to pass arguments from the command line and for the strings.
– sergiopm
Nov 19 '18 at 15:42
|
show 2 more comments
1
Can you post any code that you have tried? Or any error messages you get when you run your code?
– ahota
Nov 19 '18 at 15:17
I cannot test it since I cannot store the variables
– quiasdesamis2
Nov 19 '18 at 15:18
Is it possible to do this without using pointers (at all)
– quiasdesamis2
Nov 19 '18 at 15:19
Mr Bowling I have seen this but didn't understand how the two strings would be stored, how ?
– quiasdesamis2
Nov 19 '18 at 15:20
1
You can't do this in C without using pointers. You'll need them to pass arguments from the command line and for the strings.
– sergiopm
Nov 19 '18 at 15:42
1
1
Can you post any code that you have tried? Or any error messages you get when you run your code?
– ahota
Nov 19 '18 at 15:17
Can you post any code that you have tried? Or any error messages you get when you run your code?
– ahota
Nov 19 '18 at 15:17
I cannot test it since I cannot store the variables
– quiasdesamis2
Nov 19 '18 at 15:18
I cannot test it since I cannot store the variables
– quiasdesamis2
Nov 19 '18 at 15:18
Is it possible to do this without using pointers (at all)
– quiasdesamis2
Nov 19 '18 at 15:19
Is it possible to do this without using pointers (at all)
– quiasdesamis2
Nov 19 '18 at 15:19
Mr Bowling I have seen this but didn't understand how the two strings would be stored, how ?
– quiasdesamis2
Nov 19 '18 at 15:20
Mr Bowling I have seen this but didn't understand how the two strings would be stored, how ?
– quiasdesamis2
Nov 19 '18 at 15:20
1
1
You can't do this in C without using pointers. You'll need them to pass arguments from the command line and for the strings.
– sergiopm
Nov 19 '18 at 15:42
You can't do this in C without using pointers. You'll need them to pass arguments from the command line and for the strings.
– sergiopm
Nov 19 '18 at 15:42
|
show 2 more comments
1 Answer
1
active
oldest
votes
Actually the main
declaration is int main(int argc, char **argv);
or int main(int argc, char *argv);
, where:
argc
is the number of arguments passed
**argv
or*argv
contains the arguments to pass
So you have to use pointers to pass the arguments (as main is declared), there is no other way. In your example (./main "Hello World!" World
), the program will receive:
argc
is 3
argv[0]
is ./main
argv[1]
is Hello World!
argv[2]
is World
Right, thanks. But somehow I get an error when printing... I do printf("argv[%i] = "%s", i, argv[i]") but it gives me an error
– quiasdesamis2
Nov 19 '18 at 16:00
@quiasdesamis2: Apart from the fact you omitted the newline at the end of the format string, you have stray double quotes all over the place. Consider using single quotes, or use"
for each"
that you want to appear in the format string. You need more like:printf("argv[%i] = "%s"n", i, argv[i]);
— note that this removes the trailing double quote shown in your comment where it says:printf("argv[%i] = "%s", i, argv[i]")
.
– Jonathan Leffler
Nov 19 '18 at 16:04
Tryprintf("argv[%u] = %sn", i, argv[i]);
, being i an unsigned in the range [0-2]
– Jose
Nov 19 '18 at 16:04
it still gives me 2 errors, located at > i and at >argv[i]
– quiasdesamis2
Nov 19 '18 at 16:07
I have edited my question please take a look it doesn't compile
– quiasdesamis2
Nov 19 '18 at 16:14
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53377555%2fc-store-parameter-passed-when-calling-main%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
Actually the main
declaration is int main(int argc, char **argv);
or int main(int argc, char *argv);
, where:
argc
is the number of arguments passed
**argv
or*argv
contains the arguments to pass
So you have to use pointers to pass the arguments (as main is declared), there is no other way. In your example (./main "Hello World!" World
), the program will receive:
argc
is 3
argv[0]
is ./main
argv[1]
is Hello World!
argv[2]
is World
Right, thanks. But somehow I get an error when printing... I do printf("argv[%i] = "%s", i, argv[i]") but it gives me an error
– quiasdesamis2
Nov 19 '18 at 16:00
@quiasdesamis2: Apart from the fact you omitted the newline at the end of the format string, you have stray double quotes all over the place. Consider using single quotes, or use"
for each"
that you want to appear in the format string. You need more like:printf("argv[%i] = "%s"n", i, argv[i]);
— note that this removes the trailing double quote shown in your comment where it says:printf("argv[%i] = "%s", i, argv[i]")
.
– Jonathan Leffler
Nov 19 '18 at 16:04
Tryprintf("argv[%u] = %sn", i, argv[i]);
, being i an unsigned in the range [0-2]
– Jose
Nov 19 '18 at 16:04
it still gives me 2 errors, located at > i and at >argv[i]
– quiasdesamis2
Nov 19 '18 at 16:07
I have edited my question please take a look it doesn't compile
– quiasdesamis2
Nov 19 '18 at 16:14
add a comment |
Actually the main
declaration is int main(int argc, char **argv);
or int main(int argc, char *argv);
, where:
argc
is the number of arguments passed
**argv
or*argv
contains the arguments to pass
So you have to use pointers to pass the arguments (as main is declared), there is no other way. In your example (./main "Hello World!" World
), the program will receive:
argc
is 3
argv[0]
is ./main
argv[1]
is Hello World!
argv[2]
is World
Right, thanks. But somehow I get an error when printing... I do printf("argv[%i] = "%s", i, argv[i]") but it gives me an error
– quiasdesamis2
Nov 19 '18 at 16:00
@quiasdesamis2: Apart from the fact you omitted the newline at the end of the format string, you have stray double quotes all over the place. Consider using single quotes, or use"
for each"
that you want to appear in the format string. You need more like:printf("argv[%i] = "%s"n", i, argv[i]);
— note that this removes the trailing double quote shown in your comment where it says:printf("argv[%i] = "%s", i, argv[i]")
.
– Jonathan Leffler
Nov 19 '18 at 16:04
Tryprintf("argv[%u] = %sn", i, argv[i]);
, being i an unsigned in the range [0-2]
– Jose
Nov 19 '18 at 16:04
it still gives me 2 errors, located at > i and at >argv[i]
– quiasdesamis2
Nov 19 '18 at 16:07
I have edited my question please take a look it doesn't compile
– quiasdesamis2
Nov 19 '18 at 16:14
add a comment |
Actually the main
declaration is int main(int argc, char **argv);
or int main(int argc, char *argv);
, where:
argc
is the number of arguments passed
**argv
or*argv
contains the arguments to pass
So you have to use pointers to pass the arguments (as main is declared), there is no other way. In your example (./main "Hello World!" World
), the program will receive:
argc
is 3
argv[0]
is ./main
argv[1]
is Hello World!
argv[2]
is World
Actually the main
declaration is int main(int argc, char **argv);
or int main(int argc, char *argv);
, where:
argc
is the number of arguments passed
**argv
or*argv
contains the arguments to pass
So you have to use pointers to pass the arguments (as main is declared), there is no other way. In your example (./main "Hello World!" World
), the program will receive:
argc
is 3
argv[0]
is ./main
argv[1]
is Hello World!
argv[2]
is World
answered Nov 19 '18 at 15:40
Jose
1,048315
1,048315
Right, thanks. But somehow I get an error when printing... I do printf("argv[%i] = "%s", i, argv[i]") but it gives me an error
– quiasdesamis2
Nov 19 '18 at 16:00
@quiasdesamis2: Apart from the fact you omitted the newline at the end of the format string, you have stray double quotes all over the place. Consider using single quotes, or use"
for each"
that you want to appear in the format string. You need more like:printf("argv[%i] = "%s"n", i, argv[i]);
— note that this removes the trailing double quote shown in your comment where it says:printf("argv[%i] = "%s", i, argv[i]")
.
– Jonathan Leffler
Nov 19 '18 at 16:04
Tryprintf("argv[%u] = %sn", i, argv[i]);
, being i an unsigned in the range [0-2]
– Jose
Nov 19 '18 at 16:04
it still gives me 2 errors, located at > i and at >argv[i]
– quiasdesamis2
Nov 19 '18 at 16:07
I have edited my question please take a look it doesn't compile
– quiasdesamis2
Nov 19 '18 at 16:14
add a comment |
Right, thanks. But somehow I get an error when printing... I do printf("argv[%i] = "%s", i, argv[i]") but it gives me an error
– quiasdesamis2
Nov 19 '18 at 16:00
@quiasdesamis2: Apart from the fact you omitted the newline at the end of the format string, you have stray double quotes all over the place. Consider using single quotes, or use"
for each"
that you want to appear in the format string. You need more like:printf("argv[%i] = "%s"n", i, argv[i]);
— note that this removes the trailing double quote shown in your comment where it says:printf("argv[%i] = "%s", i, argv[i]")
.
– Jonathan Leffler
Nov 19 '18 at 16:04
Tryprintf("argv[%u] = %sn", i, argv[i]);
, being i an unsigned in the range [0-2]
– Jose
Nov 19 '18 at 16:04
it still gives me 2 errors, located at > i and at >argv[i]
– quiasdesamis2
Nov 19 '18 at 16:07
I have edited my question please take a look it doesn't compile
– quiasdesamis2
Nov 19 '18 at 16:14
Right, thanks. But somehow I get an error when printing... I do printf("argv[%i] = "%s", i, argv[i]") but it gives me an error
– quiasdesamis2
Nov 19 '18 at 16:00
Right, thanks. But somehow I get an error when printing... I do printf("argv[%i] = "%s", i, argv[i]") but it gives me an error
– quiasdesamis2
Nov 19 '18 at 16:00
@quiasdesamis2: Apart from the fact you omitted the newline at the end of the format string, you have stray double quotes all over the place. Consider using single quotes, or use
"
for each "
that you want to appear in the format string. You need more like: printf("argv[%i] = "%s"n", i, argv[i]);
— note that this removes the trailing double quote shown in your comment where it says: printf("argv[%i] = "%s", i, argv[i]")
.– Jonathan Leffler
Nov 19 '18 at 16:04
@quiasdesamis2: Apart from the fact you omitted the newline at the end of the format string, you have stray double quotes all over the place. Consider using single quotes, or use
"
for each "
that you want to appear in the format string. You need more like: printf("argv[%i] = "%s"n", i, argv[i]);
— note that this removes the trailing double quote shown in your comment where it says: printf("argv[%i] = "%s", i, argv[i]")
.– Jonathan Leffler
Nov 19 '18 at 16:04
Try
printf("argv[%u] = %sn", i, argv[i]);
, being i an unsigned in the range [0-2]– Jose
Nov 19 '18 at 16:04
Try
printf("argv[%u] = %sn", i, argv[i]);
, being i an unsigned in the range [0-2]– Jose
Nov 19 '18 at 16:04
it still gives me 2 errors, located at > i and at >argv[i]
– quiasdesamis2
Nov 19 '18 at 16:07
it still gives me 2 errors, located at > i and at >argv[i]
– quiasdesamis2
Nov 19 '18 at 16:07
I have edited my question please take a look it doesn't compile
– quiasdesamis2
Nov 19 '18 at 16:14
I have edited my question please take a look it doesn't compile
– quiasdesamis2
Nov 19 '18 at 16:14
add a comment |
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53377555%2fc-store-parameter-passed-when-calling-main%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
1
Can you post any code that you have tried? Or any error messages you get when you run your code?
– ahota
Nov 19 '18 at 15:17
I cannot test it since I cannot store the variables
– quiasdesamis2
Nov 19 '18 at 15:18
Is it possible to do this without using pointers (at all)
– quiasdesamis2
Nov 19 '18 at 15:19
Mr Bowling I have seen this but didn't understand how the two strings would be stored, how ?
– quiasdesamis2
Nov 19 '18 at 15:20
1
You can't do this in C without using pointers. You'll need them to pass arguments from the command line and for the strings.
– sergiopm
Nov 19 '18 at 15:42