UMBC CS 201, Fall 06
UMBC CMSC 201
Fall '06

CSEE | 201 | 201 F'06 | lectures | news | help

Pointers vs. Arrays

There is a definitely a difference between pointers and arrays. When an integer pointer is declared, the compiler sets aside 4 bytes of memory to hold an address.

When an integer array, say with 10 elements, is declared the compiler sets aside 40 bytes of memory to hold 10 integers and 4 bytes of memory to hold the base address of those 40 bytes.

We can have a pointer hold the address of an array, but this does not set aside any memory to hold the array.

Also, we cannot assign the address of an array to another array, this would be a syntax error as you can see below.

The Program

/********************************************* * File: syntaxerror.c * Author: R. Chang * Modified by: S. Evans * Date: 3/5/04 * Section: 01XX & 02XX * EMail: bogar@cs.umbc.edu * * A sample program to show array vs pointer syntax * - illustrates that the use of an array name is * restricted and cannot be modified. **********************************************/ #include <stdio.h> int main() { int a[10], b[10], *ptr ; ptr = b ; a = b ; return 0; }

The Sample Run

linux3[82] % gcc -Wall -ansi syntax.c syntax.c: In function `main': syntax.c:18: incompatible types in assignment linux3[83] %
Last Modified - Tuesday, 22-Aug-2006 07:14:18 EDT


CSEE | 201 | 201 F'06 | lectures | news | help