Professional Documents
Culture Documents
Index
-Pointers.
-What are Pointers?
-Why we need them?
-Using Pointers.
-More on Pointers.
-Arrays.
-How arrays are located in the memory?
-Can we pass an array to a function?
-What the reason of creating such arrays?
-Can we return an array as one object?
-How multidimensional arrays are allocated in the Memory?
-Strings.
-Structures.
-What is a Structure?
-Call by value and Reference, and the differences between them.
-Static and Dynamic Linking.
-Differences between Performance and Speed.
-What is NULL?
-What happens when i press build button?
This tutorial is still under Editing so please, download the last version.
1
Pointers
-What are Pointers?
To reach variables, if you do not know the address of place we will not able to
reach it, right?
Same in programming, A pointer helps you reach locations on the memory but
only the ones that the Operating System allowed you to access.
-Declaring a Pointer:
#include<stdio.h>
#include<conio.h>
void main()
{
int myVariable; //Assume this needs 4 Bytes.
getch(); //to have time to watch outputs.
}
-How to use Pointers and How Pointers are located on the memory?
2
int* myPointer;
Addresses Memory Names of variables
1600
9 myVariable
1604
B3 garbage
1608
U392 myPointer
int
means that this pointer points to an integer number variable, so always
pointers are of the same type of what they point to, there are special cases that
the pointer has a different type, I will talk about it later.
*
The asterisk * character informs the Compiler that you are going to use this
variable as a pointer.
Now you created a pointer, holds a garbage value, points to an invalid address.
myPointer=&myVariable;
1600 9 myVariable
The & operator tells the Compiler, please, put the address of
myVariable (operand) to myPointer.
3
Now forget myVaraible, we are going to practise with myPointer.
Use this line,
printf(“%d”,myPointer);
printf(“%d”,&myPointer);
Here you print the address of myPointer itself, did not we say that any pointer
is a variable has an address.
-So how can i access the value that myPointer points to?
Like that,
printf(“%d”,*myPointer);
Now you knew how to access them, later you will know more in session 2.
-at least one pointer points to a variable, and this refers to the compiler,
So when you write:
int myNum;
after that you use myNum directly,
actually the compiler has an invisible pointer points to myNum which allows you
to access it.
4
More on Pointers
There are some tricks on pointers, like:
printf(“%d”,*&myPointer);
And
printf(“%d”,*&myPointer);
printf(“%d”,*&*&*&*&*&*&myPointer);
We said that any pointer is a special variable that holds a value of an address.
myPointer++;
( )
using the type of myPointer, the Compiler plus the sizeof myPointer to myPointer
value.
5
Arrays
int myArray[4];
You can pass only an address of any element of it, but all of it has no meaning.
So when someone calls a function like this:
myFunc(myArray);
myFunc(&myArray[0]);
No we can’t, you can return its address, and from its address you can do what
you need to(The same thing when you return a name of an array).
6
-What the reason of creating such arrays?
We need to put them such way and such order on the memory. So we can
easily access them later more faster.
7
Strings
-A string is a char array, it differs from a string array, this is, an array of chars
is a string differs from an array of strings.
BTW, in other languages e.g. C# or Java you can declare an array of chars as a
string like:
String myString={......};
8
Structures
-What is a Structure?
A data structure consists of other related Data Structures.
In other words, a Data type of what you want.
int carNumber;
int carHeight;
.
..
...
Now, when he wants to use this car, what will he do?
This way is not so good one, he uses every variable as capsulated one.
struct Car
{
int number;
int height;
.
..
...
};
9
Car myNewCar;
Note: You just declared it, it stores old values, so after declaration you have to
initialize it for some values.
Watch, we used the Car as a Data Type like int and float.
So now you can make your own structure (Data Type), as you wish.
-struct allowed us to collect some related Data Types to one Data Type.
Also no more separated variable so our code is more readable and organized.
myNewCar.number=7637;
myNewCar.height=130;
10
General Programming Concepts
The first one copies the values of the variables to the function’s parameters.
The second one copies the addresses of the variables to the function’s
parameters.
So if you changed the variable by the second method at the body of the
function, so you change the original value, but if you changed the value of a
variable by the first method at the function, you change the variable of the
function, not the original one, you can get this better, if you printed the
addresses and know which refers to what.
Compilation Phase:
Link Phase:
Link all .objs files together and check for link errors, if you have a link error this
means that some function or library that is needed cannot be found
#include<conio.h>
#include<stdio.h>
void Play();
int main()
{
11
Play();
return 0;
}
void play()
{
printf("I’m Playing!");
}
These type of errors the compiler can not get it. The Linker is the one who only
can find these ones.
Build Phase:
Generate .exe file by converting all objs files to Binary Values, to be ready for
execution.
Debugging allows you to watch the values of variables, the Call Stack, use
breakpoints, but building does not allow us to do that, it just compiles, links
and generates the .exe file.
Static linking is what you always do, when you call the function in a .c or .cpp
file, the Compiler copies its definition statically in the function call line.
Then the code goes normal after assigning values to the parameters.
Now imagine that we put the definition of these 3 function in a .dll file.
12
-for each call, the Compiler go to the .dll file and execute the function, at the
Running time, no need to put their definitions in our .c or .cpp so not in our .obj
file so not in our .exe file. You got it?
So a .exe contains high number of functions were linked Statically, has a high
size, but calling the functions will be faster, no need to go to a .dll file and
search for the function.
And another .exe contains high number of functions were linked Dynamically
has a lower size, but more slow than the first one.
-Hackers sometimes use the .dll files to hack, by changing the definition of a
function after knowing where is it in the .dll file.
Hey you!!
Did you ask him about the value of the engine’s temperature after reaching this
speed?
The answers of all these questions tell you the whole Performance of the car.
13
-Speed is only one factor of Performance.
-What is NULL?
NULL does not mean nothing, when we assign a variable to NULL, this variable
holds a special character (Zero or any other value), in other words, you can not
use a NULL variable directly, cause its value represents nothing.
For most C compilers you can change the null character as you wish, it is a
defined value.
Also any declaration of any variable does not assign it with 0, this variable
contains old values in the memory called garbage.
-Saying that this address holds nothing is false, how come! Binary System only
use zeros and ones, and our Computer is binary System, is not it?
14
General Knowledge (look around)
-Other Operating Systems like Linux and its versions the popular one is Ubuntu
for Windows users,
Ubuntu is a free Operating System, read more about it (i will special post later
enshallah).
15
Hope this tutorial was useful for you.
mailto:help.abdulrahman.yasser@googlemail.com
16