[Courses] Re: pointers, array's, and sizeof()

Dreaming Kat dreamingkat at ivillage.com
Sat Feb 2 22:04:24 EST 2002


I found this a bit hard to follow (and I'm pretty sure I understand the topic fairly well), so here's my 2 cents on the subject, in case there are more people who think like me.

Also, if you are new to C (as presumably most of this list is), don't feel bad if you have no idea what I'm talking about in this email.  After all, we're doing a compare and contrast on two things you haven't learned yet.  

the word "decay" means "to break down" (more or less).  In this case, it means  "to eventually become" or "to resolve to".

>  according to the ANSI C standard:
> "
> An lvalue of type array-of-T which 
>appears in an expression decays (with 
>three exceptions) into a pointer to 
>its first element; the type of the 
>resultant pointer is pointer-to-T.

This means, that with three exceptions, (that they tell you about later) you can pretend that the name of an array is a pointer to the first thing in the array.
 
If you declare an array (a list) of 10 integers by saying:

int myArray[ 10 ]

myArray is the name of the array.
int is the type of the array.
You can pretend that myArray is a pointer to an integer unless your dealing with one of the 'special cases'.

What is myArray really?  It's a pointer to a block of memory.  that block of memory is the size of one element multiplied my the number of elements you have.  if an int is 4 bytes (and I really don't remember how big an int is), than myArray points to a block of memory that is 40 bytes big. 

> (The exceptions are when the array is 
>the operand of a sizeof or & operator, 
>or is a string literal initializer for 
>a character array.)"

I'm going to write a little program and post it tomorrow that demonstrates the first two exceptions, because I discovered I can't figure out how to say it clearly.

The last exception, the name of the array being a string literal initializer for a character array, means that you've said something like:

char oldArray[ 10 ];
/* put stuff in oldArray */
char newArray[] = oldArray;

this works exactly like you'd expect, the contents of oldArray are copied into newArray.  I'll include this in my little sample program tomorrow too.

-- mary


_________________________________________________________________
iVillage.com: Solutions for Your Life 
Check out the most exciting women's community on the Web   
http://www.ivillage.com



More information about the Courses mailing list