Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Character Pointers and Array Pointers in C

Tech 1

Character Pointer Variables

A char* is a pointer type that holds the address of a character. It can point to a single character or the first element of a null-terminated string.

For example:

int main() {
    char ch = 'w';
    char *ptr_to_ch = &ch;
    *ptr_to_ch = 'w'; // modifies ch via pointer
    return 0;
}

Another common usage involves string literals:

int main() {
    const char *msg_ptr = "hello bit.";
    printf("%s\n", msg_ptr);
    return 0;
}

Here, msg_ptr does not store the entire string. Instead, it stores the memory address of the first character 'h'. The string literal "hello bit." resides in a read-only section of memory (typically .rodata), and msg_ptr points to its starting location.

A classic comparison illustrates this behavior:

#include <stdio.h>

int main() {
    char arr1[] = "hello bit.";
    char arr2[] = "hello bit.";
    const char *lit1 = "hello bit.";
    const char *lit2 = "hello bit.";

    if (arr1 == arr2)
        printf("arr1 and arr2 are same\n");
    else
        printf("arr1 and arr2 are not same\n");

    if (lit1 == lit2)
        printf("lit1 and lit2 are same\n");
    else
        printf("lit1 and lit2 are not same\n");

    return 0;
}

This outputs:

arr1 and arr2 are not same
lit1 and lit2 are same

Explanation: arr1 and arr2 are distinct arrays allocated on the stack — each gets its own copy of the string. In contrast, lit1 and lit2 both point to the same string literal stored once in read-only memory, so their addresses compare equal.

Array Pointer Variables

An array pointer is a pointer that points to a entire array—not to individual elements, but to the array object itself.

Contrast with pointer arrays, which are arrays whose elements are pointers:

int *ptr_array[5]; // array of 5 int pointers

Where as an array pointer is declared as:

int (*arr_ptr)[5]; // pointer to an array of 5 ints

The parentheses around *arr_ptr are essential: they indicate that arr_ptr is a pointer, and the [5] specifies it points to an array of five integers.

Example usage:

int main() {
    int data[5] = {10, 20, 30, 40, 50};
    int (*p)[5] = &data; // p points to the whole array

    printf("Address of data: %p\n", (void*)data);
    printf("Address held by p: %p\n", (void*)*p);
    printf("First element via p: %d\n", (*p)[0]); // equivalent to data[0]

    return 0;
}
Tags: c

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.