argv[] problem

S

SysSpider

Hi guys,

I made a program in C (command-line) that prints a character the
number of times indicated by the user. But, when I insert '*'
(asterisk) as character, it says to me that I've insert more than one
character. With '&' (ampersand), it gives me an error. I have no clue
with '*', but I think the problem with '&' is related to pointers.

---Source code of chars2.c---
#include <stdio.h>
#include <string.h>

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

if(argc < 3)
{
printf("\nSyntax: chars2 <n> <char>\n\n");
printf("<n> - number of characters to be printed\n");
printf("<char> - character to be printed\n");
exit(0);
}

n = atoi(argv[1]);

if(strlen(argv[2]) > 1)
{
printf("Insert only *one* character, please\n");
exit(0);
}

for(i = 1; i <= n; i++)
{
printf("%s", argv[2]);
}
printf("\n");
}
---End of source code---

If anyone can help me, please reply.

Thanks,
SysSpider
 
M

Michael Mair

SysSpider said:
Hi guys,

I made a program in C (command-line) that prints a character the
number of times indicated by the user. But, when I insert '*'
(asterisk) as character, it says to me that I've insert more than one
character. With '&' (ampersand), it gives me an error. I have no clue
with '*', but I think the problem with '&' is related to pointers.

---Source code of chars2.c---
#include <stdio.h>
#include <string.h>

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

if(argc < 3)
{
printf("\nSyntax: chars2 <n> <char>\n\n");
printf("<n> - number of characters to be printed\n");
printf("<char> - character to be printed\n");
exit(0);
}

n = atoi(argv[1]);

if(strlen(argv[2]) > 1)
{
printf("Insert only *one* character, please\n");
exit(0);
}

for(i = 1; i <= n; i++)
{
printf("%s", argv[2]);
}
printf("\n");
}
---End of source code---

If anyone can help me, please reply.

Thanks,
SysSpider

This is not a problem of your program but of your environment.
To your shell/console, * and & probably are special characters,
so you either have to escape or quote them.

Try
chars2 10 "*"
or
chars2 10 \*
and read the manual for your shell.


Cheers
Michael
 
E

Eric Sosman

SysSpider said:
Hi guys,

I made a program in C (command-line) that prints a character the
number of times indicated by the user. But, when I insert '*'
(asterisk) as character, it says to me that I've insert more than one
character. With '&' (ampersand), it gives me an error. I have no clue
with '*', but I think the problem with '&' is related to pointers.

It's probably the command-line interpreter ("shell" in
Unixspeak). Characters like '*' and '&' and others are
"special" to many shells, and your program probably never
gets to see them at all. If they are special to the shell
you're using, you'll need to quote them or otherwise remove
their, er, "speciality." Consult your shell's documentation
for how this can be done (if it can be); it's not a C problem.

That's not to say there aren't any problems in your C ...
---Source code of chars2.c---
#include <stdio.h>
#include <string.h>

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

if(argc < 3)
{
printf("\nSyntax: chars2 <n> <char>\n\n");
printf("<n> - number of characters to be printed\n");
printf("<char> - character to be printed\n");
exit(0);

You should have included said:
}

n = atoi(argv[1]);

You should have included <stdlib.h> to use this function.
Also, this function requires a good deal of trust; if the
user gives you a non-number like "goozle" or a mixed string
like "23skiddoo" atoi() provides no way for you to discover
the problem.
if(strlen(argv[2]) > 1)
{
printf("Insert only *one* character, please\n");
exit(0);
}

for(i = 1; i <= n; i++)
{
printf("%s", argv[2]);
}
printf("\n");

You have defined main() (correctly) as returning an `int'
value -- well, what value are you returning? (Some may point
out that the latest Standard grants special dispensation for
"falling off the end" of main(), but most implementations still
obey the older Standard. Also, it's a sign of sloppiness.)
 
D

dandelion

SysSpider said:
Hi guys,

I made a program in C (command-line) that prints a character the
number of times indicated by the user. But, when I insert '*'
(asterisk) as character, it says to me that I've insert more than one
character. With '&' (ampersand), it gives me an error. I have no clue
with '*', but I think the problem with '&' is related to pointers.

Let me guess...

You are on a Unixoid platform. This make sthe problem simple. Your program
is not at fault here, but the shell interprets '*' and '&' in a special way.

'*' is a wildcards and gets expanded to every file in your current $PWD
(working directory), which usually results in (many) more than one
character.

'&' terminates a command and put's it in the background. In this case your
program does not receive any characters at all.

The solution is to simply 'escape' these characters using a backslash. Try
'\*' and '\&' respectively and signal your shell to keep it's fingers *off*
these characters.

HTH

dandelion.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,157
Messages
2,570,879
Members
47,413
Latest member
KeiraLight

Latest Threads

Top