# Marco Dorantes' WebLog

"Computer science is no more about computers than astronomy is about telescopes" -Edsger W. Dijkstra

# What are pointers?

### What are pointers?

Today, my young nephew came to me with the question "What is that thing called 'pointers' in the C programming language?" from his first programming course at school.

The time has come to see if he was born with that part of the brain that understands pointers. After some minutes of explanation, I presented to him the following 4 programs asking him "What is displayed in the screen for each program?"

He answered correctly all of them, and I concluded that such part of the brain is present indeed.

Program 0:

```#include <stdio.h>

void main()
{
char n;
n=65;
printf("%d\n",n);

char* p;
p= &n;

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

char* g=p;
printf("%d\n", *g );

*g=14;

printf("%d\n", n );
printf("%d\n", *p );
printf("%d\n", *g );
}
```
Program 1:
```#include <stdio.h>

void f(int x)
{
x= x * 2;
}

void g(int* x)
{
*x= (*x) * 2;
}

void main()
{
int n=30;

f(n);
printf("n=%d\n",n);

g(&n);
printf("n=%d\n",n);
}
```
Program 2:
```#include <stdio.h>

float* f(float* x, float* y)
{
*x= (*x) * (*y);
return y;
}

void main()
{
float n = 4;
float m = 2;
float* r = f(&n,&m);
printf("result=%f \n", n);
printf("result=%f \n", *r);
}
```
Program 3:
```#include <stdio.h>

char f(char* x, char y)
{
char result=( (*x) + y ) / 2;
return result;
}

char* g(char* s, char* t)
{
*s = *t;
return s;
}

void main()
{
char a=65;
char b=90;
char c=f(&a,b);
printf("%c \n", c);
void* n=(void*)g(&a, &b);
printf("%x \n", n);
}
```

• I've always found it strange that pointers simply make sense for some people, and others struggle with them no matter how long they try. Wonderful that your nephew is one of those who "gets it" :-)
• if one variable contains the address of other variable,the first variable is said point to the second
Page 1 of 1 (2 items)