🔥Limited Offer: Get 50% OFFon AI & Full Stack Courses🔥
Back to C Notes
Topic #82

C Access Memory


Access Dynamic Memory

Dynamic memory behaves like an array, with its data type specified by the type of the pointer.

As with arrays, to access an element in dynamic memory, refer to its index number:

ptr[0] = 12;

You can also dereference the pointer to access the first element:

*ptr = 12;

Example

  // Allocate memory
int *ptr;
ptr = calloc(4, sizeof(*ptr));

//
  Write to the memory
*ptr = 2;
ptr[1] = 4;
ptr[2] = 6;

// Read
  from the memory
printf("%d\n", *ptr);
printf("%d %d %d", ptr[1], ptr[2],
  ptr[3]);

A note about data types

Dynamic memory does not have its own data type, it is just a sequence of bytes. The data in the memory can be interpreted as a type based on the data type of the pointer.

In this example a pointer to four bytes can be interpreted as one int value (4 bytes) or as an array of 4 char values (1 byte each).

Example

  int *ptr1 = malloc(4);
char *ptr2 = (char*) ptr1;
ptr1[0] = 1684234849;
  printf("%d is %c %c %c %c", *ptr1, ptr2[0], ptr2[1], ptr2[2], ptr2[3]);

Want to go beyond the notes?

Join CodingNow 2.0's C course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

C Access Memory – FAQs

Quick answers about learning C Access Memory in C.

This free note from CodingNow 2.0 explains C Access Memory in C — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every C topic on CodingNow 2.0, including C Access Memory, is 100% free with no signup required.
With focused practice, most students grasp C Access Memory in 1–3 days from these notes; pairing it with CodingNow 2.0's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the CodingNow 2.0 Community (/community) — expert instructors answer within 24 hours.
WhatsApp
Call NowEnroll Now