๐ŸŒ
W3Schools
w3schools.com โ€บ c โ€บ c_pointers.php
C Pointers
Use the & operator to store the memory address of the myAge variable, and assign it to the pointer. Now, ptr holds the value of myAge's memory address. In the example above, we used the pointer variable to get the memory address of a variable (used together with the & reference operator). You can also get the value of the variable the pointer points to, by using the * operator (the dereference operator):
๐ŸŒ
W3Schools
w3schools.com โ€บ c โ€บ c_structs_pointers.php
C Structs and Pointers
With pointers, you can use malloc() to create structs while the program is running. You will learn more about memory management in a later chapter. Tip: If you're working with big programs or many values, struct pointers can help make your code cleaner and more efficient.
๐ŸŒ
W3Schools
w3schools.com โ€บ c โ€บ index.php โ€บ c_pointers.php
C Tutorial
Many chapters in this tutorial end with an exercise where you can check your level of knowledge. ... Check your understanding with a short quiz and see how well you know the basics of C. ... Learn by examples! This tutorial supplements all explanations with clarifying examples. ... This is an optional feature. You can study at W3Schools without creating an account. You will also find complete keyword and function references:
๐ŸŒ
W3Schools
w3schoolsua.github.io โ€บ c โ€บ c_pointers_en.html
C Language Tutorial. Pointers. Lessons for beginners. W3Schools in English
Use the & operator to store the memory address of the myAge variable, and assign it to the pointer. Now, ptr holds the value of myAge's memory address. In the example above, we used the pointer variable to get the memory address of a variable (used together with the & reference operator). You can also get the value of the variable the pointer points to, by using the * operator (the dereference operator):
๐ŸŒ
W3Schools
w3schools.com โ€บ c โ€บ c_pointer_to_pointer.php
C Pointer to Pointer (Double Pointer)
C Functions C Function Parameters C Scope C Function Declaration C Functions Challenge C Math Functions C Inline Functions C Recursion C Function Pointers ... C Structures C Structs Challenge C Nested Structures C Structs & Pointers C Unions C typedef C Struct Padding ... C Allocate Memory C Access Memory C Reallocate Memory C Deallocate Memory C Structs and Memory C Memory Example
๐ŸŒ
W3Schools
w3schools.com โ€บ c โ€บ c_pointers_arrays.php
C Pointers and Arrays
Well, in C, the name of an array, is actually a pointer to the first element of the array. Confused? Let's try to understand this better, and use our "memory address example" above again.
๐ŸŒ
W3Schools
w3schools.com โ€บ cpp โ€บ cpp_pointers.asp
C++ Pointers
Use the & operator to store the memory address of the variable called food, and assign it to the pointer. Now, ptr holds the value of food's memory address. Tip: There are three ways to declare pointer variables, but the first way is preferred: string* mystring; // Preferred string *mystring; string * mystring; ... If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail: sales@w3schools.com
๐ŸŒ
W3Schools
w3schools.com โ€บ c โ€บ c_functions_pointers.php
C Function Pointer
The general syntax for declaring a function pointer is: returnType (*pointerName)(parameterType1, parameterType2, ...); ... This means ptr is a pointer to a function that takes two int values and returns an int.
Find elsewhere
๐ŸŒ
NTU
www3.ntu.edu.sg โ€บ home โ€บ ehchua โ€บ programming โ€บ cpp โ€บ cp4_PointerReference.html
C++ Pointers and References
The indirection operator (*) can be used in both the RHS (temp = *pNumber) and the LHS (*pNumber = 99) of an assignment statement. Take note that the symbol * has different meaning in a declaration statement and in an expression. When it is used in a declaration (e.g., int * pNumber), it denotes that the name followed is a pointer variable. Whereas when it is used in a expression (e.g., *pNumber = 99; temp << *pNumber;), it refers to the value pointed to by the pointer variable.
๐ŸŒ
W3Schools
w3schools.com โ€บ c โ€บ c_pointers_arithmetic.php
C Pointer Arithmetic
An int* moves in 4-byte steps, but a char* moves 1 byte at a time. Mixing them up will point to the wrong memory locations. Uninitialized pointers: Always make sure a pointer is pointing to something real before you use it. Using a pointer that doesn't point anywhere can crash your program. Going out of bounds: Never move a pointer past the end of an array or before it starts. The only safe "outside" position is one step past the end, and that's only for comparing pointers - not for accessing values.
๐ŸŒ
W3Schools
w3schools.com โ€บ c โ€บ c_memory_address.php
C Memory Address
Pointers are important in C, because they allow us to manipulate the data in the computer's memory - this can reduce the code and improve the performance.
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ explain-reference-and-pointer-in-c-programming
Explain reference and pointer in C programming?
Problem Explain the concept of reference and pointer in a c programming language using examples. Reference It is the alternate name for the variable that we declared. It can be accessed by
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ c++ โ€บ pointers-vs-references-cpp
Pointers vs References in C++ - GeeksforGeeks
2 weeks ago - A reference acts as an alias (another name) for an existing variable. Both are commonly used for parameter passing and efficient data manipulation. A pointer is a variable that stores the memory address of another variable.
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ cprogramming โ€บ c_pointers.htm
Pointers in C
The only difference between pointers of different data types is the data type of the variable or constant that the pointer points to. After declaring a pointer variable, you need to initialize it with the address of another variable using the ...
Top answer
1 of 9
31

Here is a memory map; a representation of memory as a sequence of blocks:

    address    01   02   03
             +----+----+----+...
data within  | 23 | 6f | 4a |
             +----+----+----+...

Now suppose we create a character:

char c = 'z';  // 'z' is 7a in hex

Further suppose c is stored at address 01, so our memory looks like so:

    address    01   02   03
             +----+----+----+...
data within  | 7a | 6f | 4a |
             +----+----+----+...

Now, let's create a pointer:

char* p = &c;  // point at c

p may be stored at address 02:

    address    01   02   03
             +----+----+----+...
data within  | 7a | 01 | 4a |
             +----+----+----+...

Here the pointer p is at address 02 and it points at address 01. That's the meaning of p = &c;. When we dereference the pointer p (at address 02) we look at what's in the address pointed at by p. That is, p points at address 01, and so dereferencing p means looking inside address 01.

Finally, lets create a reference:

char& r = c;

Here the memory layout doesn't change. That is, no memory is used to store r. r is a sort of alias for c, so when we refer to r we practically refer to c. r and c are conceptually one. Changing r means changing c, and changing c means changing r.

When you create a reference you must initialize, and once initialized you cannot re-initialize it with another target. That is, above the reference r means and forever will mean c.

Also related are const references. These are the same as a reference, except they are immutable:

const char& r = c;
r = 'y';  // error; you may not change c through r
c = 'y'   // ok. and now r == 'y' as well

We use const references when we are interested in reading the data but frown upon changing it. By using a const reference the compiler will not copy the data, so this gives us ideal performance, but also forbid us from changing the data, for correctness.

In a sense, you can say that references are a compile-time feature, whereas pointers are a runtime feature. So references are faster and cheaper than pointers, but come with certain constraints and implications. Like other compile-time-vs-runtime alternatives, we sometimes pick one over the other for performance, sometimes for static analysis and sometimes for flexibility.

2 of 9
7

Time to go on a term-bashing spree, because these things always cause confusion.

  • A pointer is a memory address in its own right. Cue fancy diagram for how it happens in memory:

    | Address  | Value          |
    |----------|----------------|
    |0x1111    |0x1112          | <-- Pointer!
    |0x1112    |42              | <-- Pointed value
    |0x1113    |42              | <-- Some other value
    

    I've used a much smaller address size just for simplicity. Basically, 0x1111 is a pointer because its contents are the address of another value.

  • Dereferencing means examining the value of the address held in the pointer's value. Such fancy language can be confusing; basically, if I dereference 0x1111 I look at 0x1112 and get the value out of that address. Why? Because it's really useful and because assembly lets us do it too,

    mov rax, [r8]
    

    Is nasm/intel syntax for "look in r8, find that memory address, follow it and find the value at that memory address and put that in rax".

  • Pass by value. Pass by value means that when you create a function stack frame, which is the stack contents around a function, you copy every value that is an argument to wherever it goes. Registers, stack, wherever. Of course, if you copy a pointer's value, you're copying a memory address and thus creating another pointer pointing to the same memory. This is how functions like this:

    void add(int* x)
    {
        *x = *x + 7;
    }
    

    Work.

  • Pass by reference. What that function above does is essentially pass by reference semantics as you will see them in say C++. The crucial and possibly only difference as the implementation is likely identical at the assembly level is that a reference is something the C++ compiler understands. Since the compiler is the language this is important. C understands pointers and manipulating memory, and so do C compilers, but they'll let you do whatever you like. You can't re-assign a reference, for example,

    void cppadd(int& x)
    {
        int a = 7;
        x = &a; // doesn't work.
    }
    

So, to sum it up, references are on one level a language feature where the compiler understands where the source memory is and prevents modification of that source memory address. It understands you want to play with the value. Pointers are just that, memory addresses holding other memory addresses.

Wikipedia summarises it pretty well:

In the C++ programming language, a reference is a simple reference datatype that is less powerful but safer than the pointer type inherited from C. The name C++ reference may cause confusion, as in computer science a reference is a general concept datatype, with pointers and C++ references being specific reference datatype implementations.

Yes, I have mentioned C++ when this question is only C, but I feel it is prudent to clarify how a term has become somewhat confused with the addition of later languages.

๐ŸŒ
W3Schools
w3schools.com โ€บ CPP โ€บ cpp_references.asp
C++ References
Arrays Arrays and Loops Omit Array Size Get Array Size Real-Life Example Multidimensional Arrays C++ Structures C++ Enums C++ References ยท Create References Memory Address C++ Pointers ยท Create Pointers Dereferencing Modify Pointers C++ Memory Management ยท
๐ŸŒ
University of Toronto
dgp.toronto.edu โ€บ ~patrick โ€บ csc418 โ€บ wi2004 โ€บ notes โ€บ PointersVsRef.pdf pdf
C/C++ Pointers vs References Consider the following code: Pointers References
The main effect of this is that the address can directly be manipulated if it is a pointer. ... References are the preferred way of indirectly accessing a variable. They are also a little ยท safer than pointers and, in some cases, are the only way to achieve a particular result such
๐ŸŒ
Cplusplus
cplusplus.com โ€บ doc โ€บ tutorial โ€บ pointers
Pointers
Each assignment operation includes a comment on how each line could be read: i.e., replacing ampersands (&) by "address of", and asterisks (*) by "value pointed to by". Notice that there are expressions with pointers p1 and p2, both with and without the dereference operator (*). The meaning of an expression using the dereference operator (*) is very different from one that does not. When this operator precedes the pointer name, the expression refers to the value being pointed, while when a pointer name appears without this operator, it refers to the value of the pointer itself (i.e., the address of what the pointer is pointing to).