4

I'm new to programming, and at present I am learning about pointers in C.

  1. I know that pointers are the variables which contain or hold address of another variable. Today when I was more learning about it with K&R I got confused in a line which says "&a is a pointer to a" in a given function swap(&a,&b) in Page No. 80. How &a is a pointer? It is not a variable, it is the address of the variable. Am I right?
  2. I know that arguments can be passed to a function in two ways: call by value and call by reference. The caller's value of the argument is not changed in the first one, but it can be changed in the second one.

My question is I had read that if we want to change the value of the variable we have to pass the pointer to it (i.e. the location of the value we want to modify). What is meant by that? I mean, do we have to pass pointers to the function? And what is the meaning of the statement, "we have to pass the pointers to the location we want to modify".

John Bollinger
  • 160,171
  • 8
  • 81
  • 157
Yogesh Tripathi
  • 703
  • 5
  • 16
  • 4
    Some people use “pointer” for “pointer variable”. In particular “… pointer are the variables” is only true if you use “pointer” as shorthand for “pointer variable”. Any **expression** can be of type pointer. `&a` is an expression (and it has a pointer type). – Pascal Cuoq Oct 29 '14 at 15:03

7 Answers7

8

A pointer is not a variable. A pointer is a value.

A variable, in C, designates a storage location, and a value can be stored in that location. Thus, if you have a variable a declared with int a, then a is a variable in which an integer value can be stored. If you have a variable int *x, then x is a variable in which a pointer to an integer value can be stored.

The address of a storage location can be obtained using the & operator. E.g., &a is the address of the storage location designated by a, or the address of a, and can be stored in (among other things) a variable of the corresponding type. Thus you can have:

int  a = 42;  /* a is a variable of type int,  and has value 42 */
int* x = &a;  /* x is a variable of type int*, and has value &a */

Although analogies in programming are often dangerous, you might think of things like page numbers in a book. The page number of a page is not the same thing as the page, but page numbers can still be written down on pages. E.g., the table of contents page has lots of page numbers written down on it. Thus:

actual_page p = ...; /* a page structure */
page_number n = &p;  /* a page number    */
Joshua Taylor
  • 84,998
  • 9
  • 154
  • 353
  • thanks alot for your great answer .As far as i understand is that pointer and pointer variables are two different things right? One last question if pointers are address of variable then why in some book for example K.N King at one place it says that p is pointer to i( for example) .Doea the author used pointer in place of pointer variable? – Yogesh Tripathi Oct 29 '14 at 15:55
  • @user4156958 Yes, it's an abuse of language to say that p is a pointer to i; it's actually the case that the *value* of p is a pointer to i. It's a technically a bit inaccurate, but it's very common language, because context usually makes it clear what is meant. Since a pointer variable isn't a pointer, if we say "p [a pointer variable] is a pointer to i", we must actually mean "[the value of] o is a pointer i". Just like when we say (after `int a=2`) that a is 2, we actually mean that the *value* of a is 2. – Joshua Taylor Oct 29 '14 at 16:01
4

A pointer is an address. A pointer variable is a variable holding an address.

It is perhaps analogous to the difference between a literal integer 1 and an integer variable int a. One might refer to both a and 1 as integers, just as you might refer to &a and int* p as pointers.

Note also that &a is not an lvalue and cannot be assigned - so it is as you say not a variable, but it is a pointer nonetheless, and can itself be assigned to a pointer variable.

Clifford
  • 88,407
  • 13
  • 85
  • 165
  • 1
    So pointer and pointer variables are two different things? pointer variable holds the pointers right? – Yogesh Tripathi Oct 29 '14 at 15:20
  • @user4156958 : Yes. A pointer variable is a variable that holds a pointer value (address), just as an integer variable is a variable that holds an integer value. Both have type - *pointer type* may refer to either a value or a variable. `&a` where `a` in an `int` has type `int* const`, and is a value not a variable. – Clifford Oct 29 '14 at 15:24
  • well i want to say that it helps me alot.I think more sometime in many book i have seen that the author say that "p is a pointer to i " (for example) so what it means .Do they mean pointer variable or pointer ? – Yogesh Tripathi Oct 29 '14 at 15:31
  • 1
    @user4156958 it is unfortunate for you the vernacular tossed about in this language by those that use it is so short-handed. Indeed many times you see people saying "pointer" when they really mean "address". The language standard goes to reasonably great lengths to avoid this confusion by using terms like "returns a value of type "pointer-to-type", etc. but it does little good to you, the beginner, when parsing the nomenclature thrown about by most C programmers. I can only hope it eventually gels (and hopefully better than it apparently has for some other people trying to answer this). – WhozCraig Oct 29 '14 at 15:45
  • @Clifford and I up ticked this, but I feel it would be vaulting to the up vote lead if you expanded on the second part of the OP's question regarding parameters, drilling home the simple fact that in C *all* parameters are pass-by-value, and that a function intent on modifying something from the caller must be provided a "value" that will allow it to do so; namely an address passed as a formal parameter declared as a "pointer-to-type". +1 anyway. – WhozCraig Oct 29 '14 at 15:51
  • @user4156958, phrases of the form "p is a pointer to i", where "p" is the name of a variable, should be interpreted as "*p's value* is a pointer to i", or "'p' *represents* a pointer to i". This sort of language is extremely common, and by no means limited to variables whose values are pointers. – John Bollinger Oct 29 '14 at 16:02
  • Thankyou all of you who gave there precious time for clearing by doubts.It really helped me alot.Whatever i am a good or a slow learner but that day is not far when i help other peoples like you all have done for me.May be you all think that i'm a stupid this site is made for this all purpose but for me it is whole other think.So thankyou ! :) – Yogesh Tripathi Oct 29 '14 at 16:15
2

If a is a variable of some type T, then &a is an expression which evaluates to the address of that variable, also known as a pointer to that variable—the terms are interchangeable. &a has the type T*, which is pronounced “pointer to T”.

int x = 4;   // 4 has type int, so we can assign it to an int variable.
int *p = &x; // &x has type int*, so we can assign it to an int* variable.

The reason that you can modify function arguments passed by pointer is that a pointer introduces a sort of alias for a variable. You can alter it from multiple locations, either directly (via the variable) or indirectly (via the pointer).

// modifying x modifies *p.
++x;
printf("%d %d\n", x, *p);

// modifying *p modifies x.
++*p;
printf("%d %d\n", x, *p);
Jon Purdy
  • 53,300
  • 8
  • 96
  • 166
  • Thanks for your awesome answer :) As far as i understand by it is that a pointer and a pointer variable are two different things.A pointer vaiable points to the pointer or a pointer variable points to the pointer object.Right? and yes a pointer is the variable address. – Yogesh Tripathi Oct 29 '14 at 15:46
  • @user4156958: A pointer (value) and a pointer variable are different, yes. You store a pointer value in a pointer variable. Suppose we had a “memory” of a few boxes, #1, #2, #3, and so on. We call #1 `x` and we store `4` in it. `&x` is “the number of the box where `x` is”, #1. So we call #2 `p` and we store `1` in it. Using `p`, we can now look up and alter the value of `x`—`*p` is “the value at box number `p`”. Furthermore, we could make a variable in box #3, `int **pp`, and assign `&p` (`2`) to it. We could then modify `p` by modifying `*pp`, or modify `x` by modifying `**pp`. – Jon Purdy Oct 29 '14 at 16:25
2

As others have observed, a pointer is a particular kind of value, not a particular kind of variable.

With regard to calling functions:

  1. You cannot "pass a variable to a function" in C. If you put a variable name in the argument list of a function call then the value stored in that variable is passed, not the variable itself.

  2. Although in a general sense programming languages may provide for passing function arguments either by value or by reference, C provides only pass by value. You can emulate pass by reference in C, however, by passing a pointer (value) to the location where another value is stored. That's what swap(&a, &b) does: you are passing the locations where the values of variables a and b are stored (that is, pointers to those values).

John Bollinger
  • 160,171
  • 8
  • 81
  • 157
1

How &a is a pointer to a if it generates the address of a?

The terms pointer and address in C are synonymous.

When you create a variable:

int a = 0; 

It is created in memory at a specific memory location, or address, with sufficient memory to hold 1 int.

&a does not generate the address, it simply provides the address.

The & operator can be used to set the address of a pointer to a specific location, such as this:

int a = 0;//a now exists in memory at a specific location;
int *b = {0}; // b is created in memory as a pointer, and can be assigned a location

b = &a; //b is assigned the location (address) of the variable a  

Stated a little differently, in the previous line, & is referred to as the address-of operator, so:

b = &x; Can be Read: Assign to b (a pointer) the address of a.

ryyker
  • 22,849
  • 3
  • 43
  • 87
1
  1. Pointer contains the address - so synonymous.
  2. When you pass by reference, you are passing in the address - to directly modify the value.
Adam Lee
  • 2,983
  • 7
  • 35
  • 47
  • 2
    Technically, passing a pointer is *not* passing by reference. – Scott Hunter Oct 29 '14 at 15:04
  • @ScottHunter: Previously, OP rightly mentioned `address` in brackets, like pointer(address). Don't know, how this confusion came. I upvoted for that reason only. – nIcE cOw Oct 29 '14 at 15:06
  • @ScottHunter How both yhe things are different . Is there any difference b/w pointer and reference then .As the upper answer says that address and pointers are synonyms – Yogesh Tripathi Oct 29 '14 at 15:07
  • With pass by reference, the argument would be the variable in question, and the compiler/interpretter would handle the referencing (look at how C++ does it). In C, passing and using the address is explicit. – Scott Hunter Oct 29 '14 at 15:10
  • @user4156958 Pointer contains an address of a memory location. The reference of a normally declared variable (eg. int, double, and etc...) is the address of the variable. In Joshua Taylor's answer, '&a' is the reference to the variable 'a'. – Adam Lee Oct 29 '14 at 15:13
1

A pointer is a type of variable that stores the address to an object.

Basically, the pointer is the address.

Think of it as a piece of paper. When it has a number printed on it, it's an integer (or other numeric type).
A pointer is a piece of paper that says "the data is on the peice of paper in location x", where "location x" is the address of the object.

Baldrickk
  • 4,291
  • 1
  • 15
  • 27
  • if pointer is a address then why you have wriiten pointer is a variable.Do you mean pointer variable by that? – Yogesh Tripathi Oct 29 '14 at 15:33
  • If you think of a variable as something that holds a value, the pointer holds a value (the address) that points at another variable, which contains the value you are interested in (your data) – Baldrickk Oct 29 '14 at 15:36
  • Now i again i'm confused in your answer you have written poinyer is basically the address .How address can store any value afterall a address is a value. – Yogesh Tripathi Oct 29 '14 at 15:59