2 added 1892 characters in body
source | link

Pointers in C serve three main purposes:

Faking pass-by-reference

C passes all function arguments by value; the formal parameters and the actual parameters are different objects in memory, so changes to the formal parameter are not reflected in the actual parameter. If you want a function to change the value of something in the caller, you must pass the pointer to that object:

void swap(int *a, int *b)
{
  int tmp = *a;
  *a = *b;
  *b = tmp;
}

int main(void)
{
  int x = 3, y = 4;
  swap(&x, &y);
  return 0;
}

The expressions *a and *b in swap correspond to the objects x and y in main, so writing to *a updates x and writing to *b updates y.

Tracking dynamically allocated memory

The C memory allocation functions malloc, calloc, and realloc all return pointers to the first element of the dynamically allocated buffer.

int *arr = malloc(sizeof *arr * N); // allocates a block of memory large
                                    // enough to hold N ints

You can apply the subscript operator to a pointer as though it were an array (the subscript operation a[i] is equivalent to *(a + i); that is, offset i elements from a and dereference the result).

Building self-referential data structures

Although generations of Fortran programmers were building lists, trees, queues, stacks, etc., without them, pointers make building self-referential structures very easy, such as this binary tree node:

struct tnode {
  K key;                // for some arbitrary type K
  T data;               // for some arbitrary type T
  struct tnode *left;   // explicitly points to left subtree
  struct tnode *right;  // explicitly points to right subtree
};

I'm not familiar enough with PHP to say how you would do these things in that language. I would point out that C predates PHP by at least a couple of decades, and that PHP is pretty specific to a particular domain.

As to why C doesn't handle all this automagically...

One of the guiding philosophies of C is to keep the language as simple as possible, making it relatively easy to implement. Automatic memory management would add some complexity to the language (adding the threading library in C11 certainly did). Not to mention that automatic memory management can also play hell with performance-critical code.

Pointers in C serve three main purposes:

Faking pass-by-reference

C passes all function arguments by value; the formal parameters and the actual parameters are different objects in memory, so changes to the formal parameter are not reflected in the actual parameter. If you want a function to change the value of something in the caller, you must pass the pointer to that object:

void swap(int *a, int *b)
{
  int tmp = *a;
  *a = *b;
  *b = tmp;
}

int main(void)
{
  int x = 3, y = 4;
  swap(&x, &y);
  return 0;
}

The expressions *a and *b in swap correspond to the objects x and y in main, so writing to *a updates x and writing to *b updates y.

Tracking dynamically allocated memory

The C memory allocation functions malloc, calloc, and realloc all return pointers to the first element of the dynamically allocated buffer.

int *arr = malloc(sizeof *arr * N); // allocates a block of memory large
                                    // enough to hold N ints

You can apply the subscript operator to a pointer as though it were an array (the subscript operation a[i] is equivalent to *(a + i); that is, offset i elements from a and dereference the result).

Building self-referential data structures

Although generations of Fortran programmers were building lists, trees, queues, stacks, etc., without them, pointers make building self-referential structures very easy, such as this binary tree node:

struct tnode {
  K key;                // for some arbitrary type K
  T data;               // for some arbitrary type T
  struct tnode *left;   // explicitly points to left subtree
  struct tnode *right;  // explicitly points to right subtree
};

I'm not familiar enough with PHP to say how you would do these things in that language. I would point out that C predates PHP by at least a couple of decades, and that PHP is pretty specific to a particular domain.

Pointers in C serve three main purposes:

Faking pass-by-reference

C passes all function arguments by value; the formal parameters and the actual parameters are different objects in memory, so changes to the formal parameter are not reflected in the actual parameter. If you want a function to change the value of something in the caller, you must pass the pointer to that object:

void swap(int *a, int *b)
{
  int tmp = *a;
  *a = *b;
  *b = tmp;
}

int main(void)
{
  int x = 3, y = 4;
  swap(&x, &y);
  return 0;
}

The expressions *a and *b in swap correspond to the objects x and y in main, so writing to *a updates x and writing to *b updates y.

Tracking dynamically allocated memory

The C memory allocation functions malloc, calloc, and realloc all return pointers to the first element of the dynamically allocated buffer.

int *arr = malloc(sizeof *arr * N); // allocates a block of memory large
                                    // enough to hold N ints

You can apply the subscript operator to a pointer as though it were an array (the subscript operation a[i] is equivalent to *(a + i); that is, offset i elements from a and dereference the result).

Building self-referential data structures

Although generations of Fortran programmers were building lists, trees, queues, stacks, etc., without them, pointers make building self-referential structures very easy, such as this binary tree node:

struct tnode {
  K key;                // for some arbitrary type K
  T data;               // for some arbitrary type T
  struct tnode *left;   // explicitly points to left subtree
  struct tnode *right;  // explicitly points to right subtree
};

I'm not familiar enough with PHP to say how you would do these things in that language. I would point out that C predates PHP by at least a couple of decades, and that PHP is pretty specific to a particular domain.

As to why C doesn't handle all this automagically...

One of the guiding philosophies of C is to keep the language as simple as possible, making it relatively easy to implement. Automatic memory management would add some complexity to the language (adding the threading library in C11 certainly did). Not to mention that automatic memory management can also play hell with performance-critical code.

1
source | link

Pointers in C serve three main purposes:

Faking pass-by-reference

C passes all function arguments by value; the formal parameters and the actual parameters are different objects in memory, so changes to the formal parameter are not reflected in the actual parameter. If you want a function to change the value of something in the caller, you must pass the pointer to that object:

void swap(int *a, int *b)
{
  int tmp = *a;
  *a = *b;
  *b = tmp;
}

int main(void)
{
  int x = 3, y = 4;
  swap(&x, &y);
  return 0;
}

The expressions *a and *b in swap correspond to the objects x and y in main, so writing to *a updates x and writing to *b updates y.

Tracking dynamically allocated memory

The C memory allocation functions malloc, calloc, and realloc all return pointers to the first element of the dynamically allocated buffer.

int *arr = malloc(sizeof *arr * N); // allocates a block of memory large
                                    // enough to hold N ints

You can apply the subscript operator to a pointer as though it were an array (the subscript operation a[i] is equivalent to *(a + i); that is, offset i elements from a and dereference the result).

Building self-referential data structures

Although generations of Fortran programmers were building lists, trees, queues, stacks, etc., without them, pointers make building self-referential structures very easy, such as this binary tree node:

struct tnode {
  K key;                // for some arbitrary type K
  T data;               // for some arbitrary type T
  struct tnode *left;   // explicitly points to left subtree
  struct tnode *right;  // explicitly points to right subtree
};

I'm not familiar enough with PHP to say how you would do these things in that language. I would point out that C predates PHP by at least a couple of decades, and that PHP is pretty specific to a particular domain.