Jump to content

  • Log In with Google      Sign In   
  • Create Account

Awesome job so far everyone! Please give us your feedback on how our article efforts are going. We still need more finished articles for our May contest theme: Remake the Classics

#Actualfrob

Posted 07 December 2012 - 02:11 PM

What is the difference between a pointer and a variable?

A variable is basically a memory block or data object.  You can read from it, and generally you can write to it.  Variables can be of many different sizes and types.

A pointer is just one of those types of variables.  It is used to point to another object.


Edit: Add an example:

char a;   /* A variable that holds a character */
int b;   /* A variable that holds an integer */
MyStruct c;  /* A variable that holds a structure */

char * x;  /* A variable that points to a character */
int * y;  /* A variable that points to an integer */
MyStruct * z;  /* A variable that points to a structure */


In all cases (because you specified this is the C language) all the variables are uninitialized.  You will need to fill in valid values for a, b, and c.  You will also need to point x, y, and z to valid objects.

a = 12;  /* set a to a value, specifically a numeric constant */
x = &a;   /* set x to a value, specifically the address of another variable */

#2frob

Posted 07 December 2012 - 02:09 PM

What is the difference between a pointer and a variable?

A variable is basically a memory block or data object.  You can read from it, and generally you can write to it.  Variables can be of many different sizes and types.

A pointer is just one of those types of variables.  It is used to point to another object.


Edit: Add an example:

char a;   /* A variable that holds a character */
int b;   /* A variable that holds an integer */
MyStruct c;  /* A variable that holds a structure */

char * x;  /* A variable that points to a character */
int * y;  /* A variable that points to an integer */
MyStruct * z;  /* A variable that points to a structure */


In all cases (because you specified this is the C language) all the variables are uninitialized.  You will need to fill in valid values for a, b, and c.  You will also need to point x, y, and z to valid objects.

#1frob

Posted 07 December 2012 - 02:05 PM

What is the difference between a pointer and a variable?

A variable is basically a memory block or data object.  You can read from it, and generally you can write to it.  Variables can be of many different sizes and types.

A pointer is just one of those types of variables.  It is used to point to another object.

PARTNERS