argc question

B

Bill Cunningham

main's first argument is of course an int argc. What all is a programmer
to do with this initialization? The only uses I can find with it is the
example in this program and uses with != and a number of ints?

That is my question. Also I have this code far from complete.

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

int main(int argc, char **argv)
{
if (argc > 3) {
puts("too many strings");
exit(EXIT_FAILURE);
}
size_t t = strlen(argv[1] + 1);
printf("%i\n", t);
}

I know I am going to be needing malloc, for or while for looping constraints
also to do this permutations question. There are two argument vectors
allowed argv[1] and argv[2]. Not counting argv[0] of course. Here's the
result I want from this program. It's not very useful but it should teach
incrementing and loops.

argv[1] == malloc

m a l l o c
a l l l c m
l l oc m a
l o c m a l
o c m a l l
c m o l l o

I hope this is write and the spacing needs correction.

Bill
 
I

Ian Collins

Bill said:
main's first argument is of course an int argc. What all is a programmer
to do with this initialization? The only uses I can find with it is the
example in this program and uses with != and a number of ints?

What initialisation? argc is a parameter passed to main. It tells the
function how many entries the array argv has.
That is my question. Also I have this code far from complete.

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

int main(int argc, char **argv)
{
if (argc > 3) {
puts("too many strings");
exit(EXIT_FAILURE);
}
size_t t = strlen(argv[1] + 1);
printf("%i\n", t);
}

I know I am going to be needing malloc, for or while for looping constraints
also to do this permutations question. There are two argument vectors
allowed argv[1] and argv[2]. Not counting argv[0] of course. Here's the
result I want from this program. It's not very useful but it should teach
incrementing and loops.

argv[1] == malloc

This doesn't make any sense.
 
B

Bill Cunningham

What initialisation? argc is a parameter passed to main. It tells the
function how many entries the array argv has.

OK. Is that all it does? I've used it several ways.
argc>3 argc!=3 and so on. But what if there is no calling of argc? Can it be
there and not used?

Bill
 
B

Ben Bacarisse

Bill Cunningham said:
main's first argument is of course an int argc. What all is a programmer
to do with this initialization? The only uses I can find with it is the
example in this program and uses with != and a number of ints?

You use it to find out how many command-line arguments your program
has been provided with.
That is my question. Also I have this code far from complete.

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

int main(int argc, char **argv)
{
if (argc > 3) {
puts("too many strings");
exit(EXIT_FAILURE);
}
size_t t = strlen(argv[1] + 1);
printf("%i\n", t);
}

I know I am going to be needing malloc, for or while for looping constraints
also to do this permutations question. There are two argument vectors
allowed argv[1] and argv[2]. Not counting argv[0] of course. Here's the
result I want from this program.

If you can, it is better say what the program should do as well as
give an example. Examples are often too specific.
It's not very useful but it should teach
incrementing and loops.

argv[1] == malloc

m a l l o c
a l l l c m
l l oc m a
l o c m a l
o c m a l l
c m o l l o

You can't mean this literally. Each line has different spaces; the
second has three 'l's in it and the last has no 'a'. Do you want a
program that prints all the cyclic permutations of given string? Do
you want/need the extra spaces?

If so, why do think you need argv[2]? Why do you think you need
malloc? If you like, I'll write it for you, but that would not help
you much.

Do you know what

printf("%s", argv[1] + i);

will do? Do you know what

printf("%.*s", i, argv[1]);

does? Try them.
 
I

Ian Collins

Bill said:
OK. Is that all it does? I've used it several ways.
argc>3 argc!=3 and so on. But what if there is no calling of argc? Can it be
there and not used?

How does one call and int?

There isn't any rule that says a function parameter has to be used.
 
B

Bill Cunningham

Ben Bacarisse said:
Bill Cunningham said:
main's first argument is of course an int argc. What all is a
programmer
to do with this initialization? The only uses I can find with it is the
example in this program and uses with != and a number of ints?

You use it to find out how many command-line arguments your program
has been provided with.
That is my question. Also I have this code far from complete.

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

int main(int argc, char **argv)
{
if (argc > 3) {
puts("too many strings");
exit(EXIT_FAILURE);
}
size_t t = strlen(argv[1] + 1);
printf("%i\n", t);
}

I know I am going to be needing malloc, for or while for looping
constraints
also to do this permutations question. There are two argument vectors
allowed argv[1] and argv[2]. Not counting argv[0] of course. Here's the
result I want from this program.

If you can, it is better say what the program should do as well as
give an example. Examples are often too specific.
It's not very useful but it should teach
incrementing and loops.

argv[1] == malloc

m a l l o c
a l l l c m
l l oc m a
l o c m a l
o c m a l l
c m o l l o

You can't mean this literally. Each line has different spaces; the
second has three 'l's in it and the last has no 'a'. Do you want a
program that prints all the cyclic permutations of given string? Do
you want/need the extra spaces?

I goofed the above up. I was thinking I would have to allocate memory
for space for strlen.
If so, why do think you need argv[2]? Why do you think you need
malloc? If you like, I'll write it for you, but that would not help
you much.

Do you know what

printf("%s", argv[1] + i);

The above gives me a segmentation fault.
will do? Do you know what

printf("%.*s", i, argv[1]);

does? Try them.
I don't know about printf("%.*s",i,argv[1]); But I did try

printf("%i\n",t); And got exactly what was expected. The strlen minus the
'\0' character.
 
B

Bill Cunningham

Hi Bill,

I have seen you post here for a while. I wonder if you have ever
considered
using some computer-based training (CBT) for C. Have you ever tried that?
I struggled with C before I used an interactive CBT course, and that
course
helped me understand C in a more visual and interactive way. I think it
was one of the better ways to learn C. I then combined that with the K&R
book (revision 2), and worked through many of the exercises. That was now
many years ago. My only complaint would be that the CBT program wasn't
entirely accurate at times, but it still taught me a lot at the time.

Many people wouldn't learn C if you gave them a copy of the standard and
said "start reading!" It's too dense, and there needs to some incentive
or
feedback for understanding and motivation. K&R is a good start, but I
think it doesn't hurt to use every available accurate way to learn a
subject.

There is a saying I like: "if you spend 2 hours a day, for 2 years on any
subject, you can pass as an expert in that subject." That 2 hours may be
thought, active research, or application of learned knowledge. In reality
it sometimes takes a lot longer with some subjects, and some subjects like
programming are multifaceted. You can take 2 years with assembly
programming, 2 years with a specific language, 2 years with graphics, and
it won't exactly be 2 years with some subjects, because they require
multiple disciplines. I view my career as a software engineer as more of
a
trade. It's not like you go to college, learn a lot, and you're done. I
am continually learning new technologies, and new ways of doing things,
and
new ways of doing old things. The puzzles are different almost every
day/week. It's quite fun from my perspective. :)

-George

I think I use clc to reinforce things and sometimes to just go for an
answer. When I say re-inforce I mean take something I learned from kandr2
and bounce it around to the experts here and see in real time what goes on.
As far as college courses they have things around here like how to be MS
certified and Net+ and A+. If you're lucky you might learn some C++
somewhere. But I don't know of any modern CS classes that teach C. Sometimes
things even start to flow nicely until I come to the * and & operators
again. I can't seem to get down when to use pointers and when not too.
"Dereferencing" is to me a pretty complex concept.

int a=40;
int *pa;
pa=&a;
//so now pa is pointing to the virtual address where int a resides in
virtual memory.
printf("%p\n",pa); or maybe thats printf("%p\n",*ps);
will give me an address. Somehow I can actually with the pointer pa change
the int a's value from 40 to something else.

Bill
 
A

Andy

    I think I use clc to reinforce things and sometimes to just go for an
answer. When I say re-inforce I mean take something I learned from kandr2
and bounce it around to the experts here and see in real time what goes on.
As far as college courses they have things around here like how to be MS
certified and Net+ and A+. If you're lucky you might learn some C++
somewhere. But I don't know of any modern CS classes that teach C. Sometimes

Really? are you involved in CS department? I am in college, too... As
far as I know, C might not be the first language taught in college...
But definitely it is a "core" course...
 
B

Ben Bacarisse

Bill Cunningham said:
Do you know what

printf("%s", argv[1] + i);

The above gives me a segmentation fault.

Lots of code can do that.

Do you know what it will do if everything it properly defined?
Do you know what

printf("%.*s", i, argv[1]);

does? Try them.
I don't know about printf("%.*s",i,argv[1]); But I did try

printf("%i\n",t); And got exactly what was expected. The strlen minus the
'\0' character.

But how does that help you? The both of the printf calls I wrote are
helpful if you can work out what they do. You will have to read the
documentation for printf. You can only get so far by trying things
out. Alternatively, ask and I will post a solution for you to look
at.
 
I

Ian Collins

Richard said:
Wow. No one wanted to point out the obvious mistake to get another main
thread going .... I give up.

The obvious mistake had nothing to do with main!
 
B

Ben Bacarisse

Richard said:
It's generally bad practice to stick all on same line. And you forgot
and newline and you forgot to return the correct indicator.

Hey, a post about C!
if(argc <3){
printf ("usage: copy <src> <dest>\n");
exit EXIT_FAILURE;

What? Too much Perl?
 
R

Richard Bos

Richard said:
Wow. No one wanted to point out the obvious mistake to get another main
thread going .... I give up.

Usenet is not, and has not been for the last couple of decades, IRC.
Do keep up at the back.

Richard
 
G

guiwen1158

Bill said:
    main's first argument is of course an int argc. What all is a
    programmer
to do with this initialization? The only uses I can find with it is the
example in this program and uses with != and a number of ints?
That is my question. Also I have this code far from complete.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char **argv)
{
    if (argc > 3) {
        puts("too many strings");
        exit(EXIT_FAILURE);
    }
    size_t t = strlen(argv[1] + 1);
    printf("%i\n", t);
}
I know I am going to be needing malloc, for or while for looping
constraints also to do this permutations question. There are two argument
vectors allowed argv[1] and argv[2]. Not counting argv[0] of course.
Here's the result I want from this program. It's not very useful but it
should teach incrementing and loops.
argv[1] == malloc
m a l l o c
a  l  l l c m
l  l  oc m a
l  o c m a l
o c m a l l
c m o l l o
I hope this is write and the spacing needs correction.

Hi Bill,

I have seen you post here for a while.  I wonder if you have ever considered
using some computer-based training (CBT) for C.  Have you ever tried that?
I struggled with C before I used an interactive CBT course, and that course
helped me understand C in a more visual and interactive way.  I think it
was one of the better ways to learn C.  I then combined that with the K&R
book (revision 2), and worked through many of the exercises.  That was now
many years ago.  My only complaint would be that the CBT program wasn't
entirely accurate at times, but it still taught me a lot at the time.

Many people wouldn't learn C if you gave them a copy of the standard and
said "start reading!"  It's too dense, and there needs to some incentive or
feedback for understanding and motivation.  K&R is a good start, but I
think it doesn't hurt to use every available accurate way to learn a
subject.

There is a saying I like: "if you spend 2 hours a day, for 2 years on any
subject, you can pass as an expert in that subject."  That 2 hours may be
thought, active research, or application of learned knowledge.  In reality
it sometimes takes a lot longer with some subjects, and some subjects like
programming are multifaceted.  You can take 2 years with assembly
programming, 2 years with a specific language, 2 years with graphics, and
it won't exactly be 2 years with some subjects, because they require
multiple disciplines.  I view my career as a software engineer as more of a
trade.  It's not like you go to college, learn a lot, and you're done.  I
am continually learning new technologies, and new ways of doing things, and
new ways of doing old things.  The puzzles are different almost every
day/week.  It's quite fun from my perspective. :)

-George

Thank you very much!
 
B

Barry Schwarz

main's first argument is of course an int argc. What all is a programmer
to do with this initialization? The only uses I can find with it is the

When you write a function that takes parameters, you use the
parameters in a manner you choose. In main, the common use is to
determine how many elements of argv exist.
example in this program and uses with != and a number of ints?

That is my question. Also I have this code far from complete.

Since you haven't told us what you want to do with the arguments to
main other than argv[1], we have no idea what you question is.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char **argv)
{
if (argc > 3) {
puts("too many strings");
exit(EXIT_FAILURE);
}
size_t t = strlen(argv[1] + 1);

As has been mentioned to your dozens of times, if you want help from
those of us without C99 compilers, you are going to have to learn to
place your declarations prior to your statements.
printf("%i\n", t);

As has been mentioned to you dozens of times, if you want to see
correct output from printf you need to make sure that your argument
has the type that corresponds to your conversion specification.
}

I know I am going to be needing malloc, for or while for looping constraints

There are algorithms which will do permutations in place and therefore
will not need malloc. Since we know nothing about the algorithm you
intend to use, the accuracy of your assessment is indeterminate.

for and while loop structures are not your only choices. Again,
without the algorithm, who can say which would be best?
also to do this permutations question. There are two argument vectors

Did you mean strings instead of vectors?
allowed argv[1] and argv[2]. Not counting argv[0] of course. Here's the
result I want from this program. It's not very useful but it should teach
incrementing and loops.

argv[1] == malloc

Are you going to tell us what purpose argv[2] is supposed to serve?
m a l l o c
a l l l c m
l l oc m a
l o c m a l
o c m a l l
c m o l l o

Aside from the typos, you should realize that you listed only the
circular rotations of the word malloc. For example, you never have m
followed by an l or an a followed by an m.
 

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
473,770
Messages
2,569,583
Members
45,072
Latest member
trafficcone

Latest Threads

Top