Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Basic C Pointer

Tags:

c

pointers

I'm a beginner in C and have been trying to figure out what has gone wrong with my code code for the past hour or two. I have been following through K&Rs book and I keep looking through it but still do not understand my logic mistake.

while (*argv>0){
    while (**argv>0){
        printf("%c\n",**argv);
        **argv++;
    }
    argv++;
}

Task:Print out all the arguments being fed to my program using argv.

To my understanding, argv is a pointer to an array that contains further pointers to arrays of character pointers. So, I said that while *argv>0 or while the first array still has elements, we should follow the pointers from the first array to the next array. Then we should print out all the elements in the next array.

like image 742
user1431282 Avatar asked Jun 02 '12 00:06

user1431282


People also ask

What is pointer in C with example?

A pointer is a variable that stores the address of another variable. Unlike other variables that hold values of a certain type, pointer holds the address of a variable. For example, an integer variable holds (or you can say stores) an integer value, however an integer pointer holds the address of a integer variable.

What is pointer in C and its types?

There are majorly four types of pointers, they are: Null Pointer. Void Pointer. Wild Pointer. Dangling Pointer.

What are the three types of pointers?

Dangling, Void , Null and Wild Pointers.

What is the syntax of pointers in C?

// General syntax datatype *var_name; // An example pointer "ptr" that holds // address of an integer variable or holds // address of a memory whose value(s) can // be accessed as integer values through "ptr" int *ptr; Using a Pointer: To use pointers in C, we must understand below two operators.


1 Answers

Too many * in this line:

**argv++;

It should be like this:

*argv++;

Plus additional braces, because the ++ operation has higher priority:

(*argv)++;

And it will work.

like image 122
user1431729 Avatar answered Sep 22 '22 00:09

user1431729