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

Xp0nential Xp0nential Xp0nential at root-core.com
Sun Feb 3 09:52:59 EST 2002


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

true 



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

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

wrong wrong.
it is only in function parameters out of convenience that the compiler when it sees char a[] for example, will  treat it as  if it was char a*; <-- notice will be treated not converted...
and thats only in function parameters !!

example

#include <foo.h>

void Xp0nential(char a[], int b); 
//the char a[] being a function formal parameter in this case will be treated by the compiler as if it was char *a;

now if you go to the function definition itself....

void Xp0nential(char a[], int b) //here too treated as pointer
{
 char b[]; <--- here will not because its not a function formal paramater its being declared inside the body of the function
}

and all of that happens out of convenience.!

so in general char a[] is not the same as char *a;
"The array declaration char a[3] requests that space for six characters be set aside, to be known by the name ``a.'' That is, there is a location named ``a'' at which six characters can sit. 

The pointer declaration char *p, on the other hand, requests a place which holds a pointer, to be known by the name ``p.'' This pointer can point almost anywhere: to any char, or to any contiguous array of chars, or nowhere." (from the C FAQ)

example

char a[]="blank";
char *b="world";

the way they would look is like the following:

           +---+---+---+---+---+---+
	a: | b | l | a | n | k |\0 |
	   +---+---+---+---+---+---+

           +-----+     +---+---+---+---+---+---+
	b: |  *======> | w | o | r | l | d |\0 |
	   +-----+     +---+---+---+---+---+---+

now what you heard about char a[] being = char *a; has to do with formal parameters and what I explained before.



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

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

only if you declare as char *a;
then you malloc yes..

but not when you declare as char a[];




This is all in the C FAQ . 
you can look at it at:
http://www.eskimo.com/~scs/C-faq/top.html
especially section 6 Arrays and Pointers

I emailed a computer Science professor in my uni asking why isn't this fact explained to us. The professor said something along the lines: ew do it for simplification purposes. He did admit it was an oversimplification of the ANSI C standard (which 
I am not really sure is that simple)



regards,
Xp0nential

_____________________________________________________________
[Root-Core Network] - [www.root-core.org] - Free E-mail



More information about the Courses mailing list