Skip to content
StudyFame

Declaring and Using Pointers

You now know what a pointer is. Before using pointers in real programs, you must declare them correctly, give them a safe starting value, and understand why a pointer has a type.

In this lesson you will learn the exact rules for declaring, initializing and dereferencing pointers, plus the traps that cause most pointer crashes in lab exams.

What You Will Learn

  • How to declare pointers of different types.
  • How to initialize a pointer (with an address or NULL).
  • The int *p, q; trap.
  • Why a pointer needs a data type.
  • The size of a pointer.
  • How to read input through a pointer.

Pointer Declaration and Initialization Explained

Exam definition: Declaring a pointer means creating a variable that can hold the address of a given type: type *name;. Initializing it means giving it a valid address (&variable) or NULL. Dereferencing means accessing the value at that address with *name.

Memory trick: D-I-D: Declare it, Initialize it, then Dereference it. Never skip the middle step. An uninitialized pointer is like a GPS with a random destination.

Syntax of Pointer Declaration

int    *ip;          /* pointer to int    */
float  *fp;          /* pointer to float  */
char   *cp;          /* pointer to char   */
double *dp;          /* pointer to double */

int x = 10;
int *p = &x;         /* declare + initialize with an address */
int *q = NULL;       /* declare + initialize to "points nowhere" */

int *a, *b;          /* two pointers: star on EACH name */
int *c, d;           /* c is a pointer, d is a plain int! */
  • The space around * doesn't matter: int *p, int* p and int * p are the same.
  • But the * belongs to the name, not the type. That's why int* c, d; makes only c a pointer. Writing int *c reminds you of this.
  • NULL (from stdio.h/stddef.h) means "points to nothing". You can test it: if (q == NULL).

Example 1: Pointers of Different Types

#include <stdio.h>

int main(void)
{
    int    marks = 88;
    float  cgpa  = 8.7f;
    char   grade = 'A';

    int   *pm = &marks;
    float *pc = &cgpa;
    char  *pg = &grade;
    int   *none = NULL;

    printf("marks = %d, cgpa = %.1f, grade = %c\n", *pm, *pc, *pg);

    *pm = *pm + 5;          /* grace marks */
    *pc = 9.1f;
    *pg = 'O';
    printf("marks = %d, cgpa = %.1f, grade = %c\n", marks, cgpa, grade);

    if (none == NULL)
        printf("none points to nothing, so we don't dereference it\n");

    printf("size of int*, float*, char*: %zu %zu %zu\n",
           sizeof(pm), sizeof(pc), sizeof(pg));

    return 0;
}

Output

marks = 88, cgpa = 8.7, grade = A
marks = 93, cgpa = 9.1, grade = O
none points to nothing, so we don't dereference it
size of int*, float*, char*: 8 8 8

(The sizes are 8 on a 64-bit system. On an old 32-bit system or Turbo C, you would see 4 or 2.)

Code Explanation

  1. Each pointer's type matches its variable: int * for an int, float * for a float, char * for a char.
  2. *pm, *pc, *pg read the values. Writing to them changes marks, cgpa and grade.
  3. none is initialized to NULL. Checking none == NULL before using it prevents a crash.
  4. All pointers have the same size, whatever they point to. An address is an address; a char * isn't smaller than a double *.

Why Does a Pointer Need a Type?

If all addresses are the same size, why write int * or char *? The type tells C how many bytes to read at that address and how to interpret them.

Address:   1000  1001  1002  1003
Bytes  :   [ 4 bytes of an int value ]

int  *ip = 1000  ->  *ip reads 4 bytes (1000-1003) as one int
char *cp = 1000  ->  *cp reads 1 byte  (1000)      as one char

The type also decides how far p + 1 moves, which you will see in Pointer Arithmetic. Pointing an int * at a float gives a warning and wrong values.

Example 2: Reading Input Through a Pointer

#include <stdio.h>

int main(void)
{
    int qty;
    float price;
    int *pq = &qty;
    float *pp = &price;

    printf("Enter quantity: ");
    scanf("%d", pq);            /* pq already IS an address: no & */
    printf("Enter price: ");
    scanf("%f", pp);

    printf("Total = %d x %.2f = %.2f\n", *pq, *pp, *pq * *pp);

    return 0;
}

Output

Enter quantity: 4
Enter price: 250.50
Total = 4 x 250.50 = 1002.00
  • scanf needs an address. pq holds &qty, so scanf("%d", pq) is the same as scanf("%d", &qty).
  • *pq * *pp: the first and third stars dereference; the middle star is multiplication. Spaces make it readable.

Important Points to Remember

  • Syntax: type *name;
  • Always initialize a pointer: with &var or NULL.
  • Put a star on every name: int *a, *b;
  • Pointer type must match the variable type.
  • All pointers have the same size (8 bytes on 64-bit).
  • Never dereference a NULL or uninitialized pointer.

Common Mistakes Beginners Make

MistakeWhat happensFix
int* a, b; expecting two pointersb is a plain intint *a, *b;
int *p; printf("%d", *p);Garbage or crash (wild pointer)Initialize before use
float *fp = &marks; (marks is int)Warning, wrong valuesMatch the types
scanf("%d", &pq); with a pointerStores into the pointer itself: crashscanf("%d", pq);
*p = &x; to point p at xStores an address as a valuep = &x;

Pointer Declaration Practice Questions

  1. Declare a pointer to a double and make it point to double pi = 3.14;.
  2. In char *s, t;, what are the types of s and t?
  3. What is the size of a char * on a 64-bit system?
  4. Why should a pointer be initialized to NULL if no address is ready?
  5. What is the difference between p = &x; and *p = x;?
Show answers
  1. double *dp = &pi;
  2. s is char *; t is char.
  3. 8 bytes.
  4. So it doesn't hold a random address; a NULL check can then catch it before use.
  5. p = &x; makes p point to x. *p = x; copies x's value into whatever p already points to.

Coding Exercise

Declare int a = 6, b = 9; and one pointer p. Use the same pointer to double a, then point it at b and add 1 to b. Print both.

Show solution
#include <stdio.h>

int main(void)
{
    int a = 6, b = 9;
    int *p = &a;

    *p = *p * 2;      /* a = 12 */
    p = &b;           /* now point to b */
    *p = *p + 1;      /* b = 10 */

    printf("a = %d, b = %d\n", a, b);
    return 0;
}
a = 12, b = 10

Quick Quiz: Declaring Pointers (MCQ)

  1. In int *x, y;:

    A) Both are pointers   B) Only x is a pointer   C) Only y is a pointer   D) Error

  2. A safe value for a pointer with no target yet:

    A) 0.5   B) NULL   C) Any number   D) '\0' string

  3. On a 64-bit system, sizeof(double *) is:

    A) 4   B) 8   C) 16   D) Depends on the double value

  4. Correct way to read into an int through int *p = &n;:

    A) scanf("%d", &p);   B) scanf("%d", *p);   C) scanf("%d", p);   D) scanf("%p", p);

  5. The pointer's data type decides:

    A) Its own size   B) How many bytes *p reads   C) The address value   D) Nothing

Show answers
  1. B: only x.
  2. B: NULL.
  3. B: 8.
  4. C: scanf("%d", p);
  5. B: how many bytes are read (and how they are interpreted).

Quick Revision for Exams and Interviews

Read this before your exam. The highlighted words are the answer.

Must-remember facts

Declaretype *name;
Initialize&var or NULL
int *a, b;Only a is a pointer
Pointer sizeSame for all types (8 bytes on 64-bit)
Why a type?How many bytes to read and how far p + 1 moves
scanf with pointerNo &: scanf("%d", p)

Frequently asked questions

What is the size of a pointer in C?
It depends on the system, not the type: usually 8 bytes on 64-bit and 4 bytes on 32-bit.
Why initialize a pointer to NULL?
An uninitialized pointer holds a random address; NULL is a known value you can check before use.
Why do pointers have data types if all addresses are the same size?
The type tells C how many bytes to read at the address and how pointer arithmetic steps.

Remember

  • D-I-D: declare, initialize, dereference.
  • Star on every pointer name.

Next Topic

Next lesson: Pointer Arithmetic in C. You will add and subtract with pointers and see why p + 1 moves by the size of the type.