Advanced · C PROGRAMMING

Pointers and addresses

What you will learn

Use an address to access an existing object.

Understand the concept

The & operator obtains an object address. A pointer stores an address of a compatible type. Applying * to a valid pointer accesses the pointed-to object. Changing *p here changes value, because p points to value.

Read the example

The complete program appears below. Read it first, predict the output, then run it.

Expected output

Value: 25

Practise

Create a second integer and redirect p to it before assigning through *p.

Common mistake

Do not dereference an uninitialized, null or expired pointer. The pointed-to object must still exist.

Lesson discussion

Log in to participate.

#include <stdio.h>

int main(void) {
    int value = 10;
    int *p = &value;
    *p = 25;
    printf("Value: %d\n", value);
    return 0;
}
PRACTICE LAB

Read. Edit. Run.

C source · .c

Experiment with the program below. Your code and input are sent to the selected compiler only when you run it.

Tab indents · Shift+Tab unindents · Esc then Tab leaves the editor · Ctrl/Cmd+Enter runs · Ctrl/Cmd+S saves a draft

Changes save automatically to your account.

Input / output example

Example input
No input required.
Expected output for the original program
Value: 25
Output will appear here.

Log in to save your progress

Ready when you are

Need help with your laptop or computer?

Send us the model and fault details. Start your repair request online or contact TCS Pune directly.

WA