Vous êtes sur la page 1sur 2

bmm

Table of Contents

1 A Day
.. 1.1 Pointer lessons
..... 1.1.1 Definitions
..... 1.1.2 Common notions
1 A Day

Today is another day of my earthly adventure. This period in general


is one of the most challenging i have ever experienced. Nevertheless,
it is a period of meaning. I will forever be changed by the lessons
accrued during this period.
With that said, let's continue with business.
C is a beautiful language. Reading the white book has rekindled hope
in me.
C is hugely attached to the Unix OPerating system, meaning learning C
exposes one to many Unix OS interfaces, and eventually one becomes a
programmer.
Some interesting C facts:
Arrays and pointers don't really have differences.
indexing an array bearly adds the index to the pointer as an offset.
int *ip;
int a[ 10 ];
ip = &a;
Note that *ip is a variable that stores the address of an integer.
Similarly, for any variable var, &var is a pointer to itself or
contents.
We shall understand more about this in a moment.
a[ 2 ] `= *(a+2) =' *(ip+2)CE
The compiler literally translates array indexing to a pointer
arithmetic operation.
Any array is actually a pointer to it's first element. Subsequent
indexing just adds the index as an offset to this pointer. > a[i] ==
*(a+i)
int strlen(char *s){ int n; (for n = 0; *s != '\0'; s++) n++; return
n; }

Now back to the lesson of &var is a pointer to var.


strlen("What is a pointer will also work")
l[] = "What is this";
strlen(&l); Will work. strlen(char *s) takes a pointer. Passing the
address of a variable such as &l works because &l is a pointer to
l. And char *cp = &l is just an alias for pointer to l.
Whenever an array is passed to a function or manipulated with its
variable name, know that all you have done is pass along a pointer to
the first element of that array.
This explain:
printf("Hello World") or strlen("Hello world") works. The arguments
are string constants which are actually arrays of characters. So
array/pointer rules apply.
1.1 Pointer lessons

1.1.1 Definitions

1. Value: A value is anything that can be stored and manipulated by a


computational process. A value can also be seen as a piece of
information
2. Type: A type is a set of values together with the permissible
operations that can be performed on that type.
3. Variable: A variable is a storage environment that stores
appropriate values. Each variable has a location in computing space
and time, and the values it stores.
4. Address: An address is the actual location of any environment or
variable in computing space and time. Navigating to an address
gives you access to the content of that location or variable.
5. Pointer: A pointer is a variable that stores the address of other
variables. Pointers also have types. Which means a pointer stores
only the addresses of variables with same types as itself.
1.1.2 Common notions

1. Assigment changes the variables involved. a = b; > Store the


value stored by the variable b in a.

P.S. Watching an episode of Naruto, and just saw the wonders of


teamwork

Vous aimerez peut-être aussi