2

I like pointer notation in C more than I like array notation, but just can't figure it out for some cases. I have the following code, and the body of main

/*converts arguemnt to number using atoi()*/
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv) {
    int i, times;

    if(argc < 2 || (times=atoi(argv[1])) < 1) {
        printf("Usage :%s positive-number\n", argv[0]);
    } else {
        for(i = 0; i < times; i++) {
            puts("Hello");
        }
    }
    return 0;
}

How would I express argv[1] and argv[0] in pointer notation?

1
  • Do you mean pointer arithmetic? Commented Jul 13, 2014 at 17:10

4 Answers 4

6

argv[i] is equivalent to *(argv + i)

I really can't imagine why you would prefer the latter over the former.

Sign up to request clarification or add additional context in comments.

Comments

2

Just to point out a fun fact:

If you have and array (int arr[5]) or a pointer all of the following are equivalent:

*(arr + i);
*(i + arr);
arr[i];
i[arr]; // This last one is rather interesting now isn't it.

1 Comment

That's neat, I never would have thought of the last one actually working
0
p[n]

Is equivalent to:

*(p + n)

1 Comment

Boom, thank you. I'll accept once the time runs out.
0

If you mean pointer arithmetic then argv[1] would be

*(argv + 1)  

but I would go with argv[1].

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.