Class 6 Exam  >  Class 6 Notes  >  C Programming for Beginners  >  C Memory Address & Pointers

C Memory Address & Pointers | C Programming for Beginners - Class 6 PDF Download

Memory Address

  • When a variable is created in C, a memory address is assigned to the variable.
  • The memory address is the location of where the variable is stored on the computer.
  • When we assign a value to the variable, it is stored in this memory address.
  • To access it, use the reference operator (&), and the result will represent where the variable is stored:

Example

int myAge = 43;

printf("%p", &myAge); // Outputs 0x7ffe5367e044

Note: The memory address is in hexadecimal form (0x..). You probably won't get the same result in your program.

You should also note that &myAge is often called a "pointer". A pointer basically stores the memory address of a variable as its value. To print pointer values, we use the %p format specifier. You will learn much more about pointers the next chapter.

Why is it useful to know the memory address?

Pointers are important in C, because they give you the ability to manipulate the data in the computer's memory - this can reduce the code and improve the performance.

Pointers are one of the things that make C stand out from other programming languages, like Python and Java.

Creating Pointers

You learned from the previous chapter, that we can get the memory address of a variable with the reference operator &:

Example

int myAge = 43; // an int variable


printf("%d", myAge);  // Outputs the value of myAge (43)

printf("%p", &myAge); // Outputs the memory address of myAge (0x7ffe5367e044)

  • In the example above, &myAge is also known as a pointer.
  • A pointer is a variable that stores the memory address of another variable as its value.
  • A pointer variable points to a data type (like int) of the same type, and is created with the * operator. The address of the variable you're working with is assigned to the pointer:

Example

int myAge = 43;     // An int variable

int* ptr = &myAge;  // A pointer variable, with the name ptr, that stores the address of myAge


// Output the value of myAge (43)

printf("%d\n", myAge);


// Output the memory address of myAge (0x7ffe5367e044)

printf("%p\n", &myAge);


// Output the memory address of myAge with the pointer (0x7ffe5367e044)

printf("%p\n", ptr);

Example explained

  • Create a pointer variable with the name ptr, that points to an int variable (myAge). Note that the type of the pointer has to match the type of the variable you're working with.
  • 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.

Dereference

  • In the example above, we used the pointer variable to get the memory address of a variable (used together with the & reference operator).
  • However, you can also get the value of the variable the pointer points to, by using the * operator (the dereference operator):

Example

int myAge = 43;     // Variable declaration

int* ptr = &myAge;  // Pointer declaration


// Reference: Output the memory address of myAge with the pointer (0x7ffe5367e044)

printf("%p\n", ptr);


// Dereference: Output the value of myAge with the pointer (43)

printf("%d\n", *ptr);

The document C Memory Address & Pointers | C Programming for Beginners - Class 6 is a part of the Class 6 Course C Programming for Beginners.
All you need of Class 6 at this link: Class 6
10 videos|13 docs|15 tests

Top Courses for Class 6

10 videos|13 docs|15 tests
Download as PDF
Explore Courses for Class 6 exam

Top Courses for Class 6

Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev
Related Searches

Free

,

Exam

,

past year papers

,

Extra Questions

,

Semester Notes

,

Summary

,

Important questions

,

mock tests for examination

,

C Memory Address & Pointers | C Programming for Beginners - Class 6

,

practice quizzes

,

C Memory Address & Pointers | C Programming for Beginners - Class 6

,

pdf

,

MCQs

,

shortcuts and tricks

,

study material

,

video lectures

,

ppt

,

Objective type Questions

,

C Memory Address & Pointers | C Programming for Beginners - Class 6

,

Viva Questions

,

Previous Year Questions with Solutions

,

Sample Paper

;