algorithm by eratosthenos

M

Michael Mair

Bill said:
CBFalconer said:
Bill Pursell wrote:

... snip ...
...
getopt(argc, argv, optstring)
...
and was rejected because the 2nd parameter to getopt is prototyped
as char *const argv[]. So I had to change the declaration of
parse_args to match, and that got me thinking that the argument
list for main might as well match.

Which is just plain wrong. You can modify **argv. You can't
modify *argv. You can't lengthen the string at *argv either.


What is wrong? The prototype for getopt, or prototyping main as taking
(char *const*argv)? It seems that you 2nd and 3rd sentence, are
semantically equivalent to prototyping main as (int argc, char
*const*argv), so I'm not sure what you're saying. Obviously, if we
protoype main as taking a char **, then we CAN change *argv (and I
think you are claiming that we shouldn't):

#include <stdio.h>

int main (int argc, char **argv)

{
printf("argv[0] = %s\n", argv[0]);
*argv += 1;
printf("argv[0] = %s\n", argv[0]);

return 0;
}
From C99 5.2.2.1:
"The parameters argc and argv and the strings pointed to by the argv
array shall be modifiable by the program, and retain their last-stored
values between program startup and program termination."

This especially does not mention *argv.
I.e. you are allowed to do argv++ and argv[0][0] = 42 but I'd not rely
on argv[0]++ to work. I guess that we do not have "char * const *"
type for the second type only for historical reasons.


Cheers
Michael
 
J

Joe Smith

I know this post is a mess and to read it annoys even myself. My[snips everyone but himself]
/* sieve3.c */

#define whatever 20
#define N whatever
#include <stdio.h>

int main(void)
{
int i, A[2*N + 1], index, sum;

I endeavor to take keystrokes and have the prog create the memory at
runtime.
My first problem is that I'm unaware of how to get an integer without having
to build them from chars and then first principles. I can recall no such
K&R
exercise. Let's assume, however that we have an N from the keyboard that
happens to equal 20.

p = malloc( (N + 1) * sizeof(int));
is what I want to write.

Well, it doesn't look like this sieve is going to work tonight nearly as
well
as that 4 by 6 region behind the Redwing's goalie did. One thing that
would help me to know what this code says is having a debugger that is aware
of any executable. Any help is appreciated. Joe
 

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,183
Messages
2,570,966
Members
47,514
Latest member
AdeleGelle

Latest Threads

Top