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

Dreaming Kat dreamingkat at ivillage.com
Mon Feb 4 00:26:49 EST 2002


hello all,

maybe I shouldn't be writting this now, cuz I'm a bit tired, but here's my shot at it.  I'll post the program I promised yesterday and it's output in a seperatly.

> >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.
 
> that is what I was saying.

> >You can pretend that myArray is a pointer to an integer unless your dealing with one of the 'special cases'.
 
> wrong wrong.

OK, I just said the same thing, in two different ways.  one of the times you said "That is what I am saying" the other time you said "wrong wrong".  

> now for example if  a is defined as  char a[];
> and then in printf("print 3rd element in a %c",a[3]);
> in here for example when the compiler sees a[3], it emits code to start at a moves 3 past it and look for the value there.
> 
> now if b is defined as char *b;
> and then a printf like printf("print 3rd element in %c,b[3]);
> when it sees b[3] the code starts at b, take the pointer value there add three to the pointer and then lookup the value that is pointed to.
> So depending if it is declared as char a[], or char *a the compiler goes there differently......

The fact that
printf("print 3rd element in a %c", a[3]);
and 
printf("print 3rd element in b %c, b[3]);
are identical, and one of them is using a pointer while the other is using an array is an example of pretending that one is the other.  For the most part (with one of the major exceptions being optimization), we don't care what the compiler does to our code.  We're writting in C, not using C to write assembly. 

> >What is myArray really?  It's a pointer to a block of memory.  
> 
> only if you declare as char *a;
> then you malloc yes..
> 
> but not when you declare as char a[];
> 

I should have worded that "it's the name of a block of memory". (wich, btw, is the definition of a variable).

For everyone who's hopelessly confused at this point, I apologize, hopefully the program I'm about to post will help it make sense.

-- 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