Difference between revisions of "C programming/pointers"

From Teknologisk videncenter
Jump to: navigation, search
m (added Category:C using HotCat)
m
Line 1: Line 1:
 +
=Basic pointer=
 +
The following sourcecode demonstrates the basic pointer:
 +
<source lang=c>
 +
#include <stdio.h>
 +
 +
int main( void ) {
 +
  int x = 100;
 +
  int *pointer;
 +
 +
  pointer = &x;  // Set pointer to address of <x>
 +
 +
  printf("Value of int <pointer> points to: %d\n", *pointer);
 +
  printf("The address contained in <pointer> %p\n", pointer);
 +
  printf("The address of <x> %p - Should be the same as the above address!\n", &x);
 +
  printf("The <pointer> has it's own address also, which is %p\n", &pointer);
 +
  return(0);
 +
}
 +
 
=Review of C Pointer Declarations=
 
=Review of C Pointer Declarations=
 
{|
 
{|

Revision as of 13:11, 26 May 2015

Basic pointer

The following sourcecode demonstrates the basic pointer: <source lang=c>

  1. include <stdio.h>

int main( void ) {

 int x = 100;
 int *pointer;
 pointer = &x;  // Set pointer to address of <x>
 printf("Value of int <pointer> points to: %d\n", *pointer);
 printf("The address contained in <pointer> %p\n", pointer);
 printf("The address of <x> %p - Should be the same as the above address!\n", &x);
 printf("The <pointer> has it's own address also, which is %p\n", &pointer);
 return(0);

}

Review of C Pointer Declarations

int *p p is a pointer to int
int *p[13] p is an array[13] of pointer to int
int *(p[13]) p is an array[13] of pointer to int
int **p p is a pointer to a pointer to an int
int (*p)[13] p is a pointer to an array[13] of int
int *f() f is a function returning a pointer to int
int (*f)() f is a pointer to a function returning int
int (*(*f())[13])() f is a function returning ptr to an array[13] of pointers to functions returning int
int (*(*x[3])())[5] x is an array[3] of pointers to functions returning pointers to array[5] of ints