0

I wanted to create a vector (dynamically allocated) where every element of the vector is taken from the command line starting from the 3rd parameter.

I wrote this:

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

int main(int argn, char* argc[]) {
    if (argn < 4) {
        printf("Error: invalid parameter number. \n");
        return 0;
    } 
    float min = atof(argc[1]), max = atof(argc[2]), *v, *k;
    int dim = argn-3;
    char **p;
    v = malloc(dim*sizeof(float));
    k = v;
    for (p = argc+3; p < argc+dim; p++) {
        *k = atof(*p);
        k++;
    }
    for (k = v; k < v+dim; k++) {
        printf("%3.f ",*k);
    }
    return 0;
}

The problem is that only the first parameter seems to be taken from the command line, while others not.

Example: I launch [ProgramName] 25 30 27 28 29 32

It returns me 27.0 0.0 0.0 0.0, but it should return me 27.0 28.0 .29.0 32.0

Why isn't my code working?

Sorry for eventual grammar mistakes, I'm not english.

  • 1
    what is so better about `argn` over `argc`? Crap, there's `argc` but it's `char* []`...hmmm – Sourav Ghosh May 02 '16 at 17:13
  • 1
    @SouravGhosh Hey, think out of the box :) – Eugene Sh. May 02 '16 at 17:14
  • @EugeneSh. heh, and confuse the hell out of people, eh? :P – Sourav Ghosh May 02 '16 at 17:15
  • 1
    Oh, and this is why you shouldn't use nonstandard names for `argc` and `argv`. When I saw `p = argc + 3` I just immediately assumed it was completely wrong. (You can't make a pointer point to the *count*!) But it might be almost right. – Steve Summit May 02 '16 at 17:16
  • 1
    Again a perfect opportunity to learn how to use a debugger, to nicely be able to step through the code line by line, inspecting all relevant variables to learn what is really going on! :-) – alk May 02 '16 at 17:19
  • @SteveSummit Because the exercise requested to use pointers to navigate through arrays instead of indexing – Andrew J. May 02 '16 at 17:22
  • Why the downvote? This is a complete question. – alk May 02 '16 at 17:29
  • @AndrewJ. My main complaint was that you used the name `argc` in a super confusing way. As to the exercise: for the first few years I programmed in C, I tried to use a `char **` to manipulate `argv`, because I assumed that was how Real C Programmers did it. But eventually I realized, "This is like wearing too-small boots all day, and relishing how good it feels to take them off at night." Now I always use `argv[i]` to step through `argv`, and I've never regretted it, or felt like less of a man for doing so. – Steve Summit May 02 '16 at 17:35
  • @alk People are stupid sometimes – Andrew J. May 02 '16 at 17:42

1 Answers1

3

You write:

int dim = argn-3;
...
for (p = argc+3; p < argc+dim; p++) {
...
}

Suppose argn==5 , then dim==2 , and the loop boils down to:

for (p = argc+3; p < argc+2; p++) {
...
}

which would never run.

On a side note, it is customary to use argc and argv as parameters of main. If you follow this, your code will be more readable to others, and others' code will be more readable to you. Besides, you forgot to print newline.

user31264
  • 6,557
  • 3
  • 26
  • 40
  • I thought the standard names for main's parameters were argn and argc[] – Andrew J. May 02 '16 at 17:28
  • @AndrewJ.: You could in fact name them as you like, but naming a chair a table just leads to irritation, at least ;-) The common sense here is given by the C Standard: http://port70.net/~nsz/c/c11/n1570.html#5.1.2.2.1 – alk May 02 '16 at 17:31
  • You got it! The for condition was wrong. Thank you very much! – Andrew J. May 02 '16 at 17:34
  • 2
    @AndrewJ. The standard names are `argc` and `argv`. You can rename them if you want, but it's a questionable idea. We were discussing this just the other day. See http://stackoverflow.com/questions/36945552/is-it-safe-to-rename-argc-and-argv-in-main-function – Steve Summit May 02 '16 at 17:37