Você está na página 1de 16

Session 1:

Prerequisite for this tutorial


-simple knowledge of C Programming Language.

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?

Simply they hold addresses of specific locations.


A pointer is a variable, holds an address of another variable.

-Why we need them?

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:

Assume having this code,

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

Address Memory Names of variables


es
1600 9 myVariable

Represents a 4 Byte Block 1604 B34 garbage

1608 3U392 garbage

-To declare a pointer in C or C++ language write:

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.

So we have to initialize it by:

myPointer=&myVariable;

Addresses Memory Names of variables

1600 9 myVariable

1604 B372 garbage

1608 1600 myPointer

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

Here you print the address stored on 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.

-more of one pointer can points to the same location.

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

watch the output and trace the code.

Also try this

printf(“%d”,*&*&*&*&*&*&myPointer);

What this output means?

We said that any pointer is a special variable that holds a value of an address.

What happens if we write this:

myPointer++;

( )
using the type of myPointer, the Compiler plus the sizeof myPointer to myPointer
value.

So myPointer now points to a different location.

5
Arrays

-When we write this line, What is the value of myArray?

int myArray[4];

This line does,

Allocating a pointer myArray points to the first element in the array.


And all these elements are of the same type which is int.

-How arrays are located in the memory?

As you know, element by element in order.


So if you have the first element’s address, and size of every element, you can
access all the elements by this function:

Element’s address = myArray + (Element’s index)×sizeof(int)

-Can we pass an array to a function?

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

this sends the address of the array to the function.

This is equivalent to:

myFunc(&myArray[0]);

-Can we return an array as one object?

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?

The answer is that we need a data structure like this typically.

We need to put them such way and such order on the memory. So we can
easily access them later more faster.

-How multidimensional arrays are allocated in the Memory?

Search for the answer.

7
Strings

In C a string is an array of characters terminated with a binary zero


character (written as '\0').

So a string is a special case of an array.

-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={......};

-Train yourself by treating with Strings, to search for a word in an array of


chars, or by inverting it to another string.

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.

Let’s talk more about that,


If someone while programming his game wants to create a car, what shall he
do?

The coming code:

int carNumber;
int carHeight;
.
..
...
Now, when he wants to use this car, what will he do?

Normally, assigning values to each variable he declared.

This way is not so good one, he uses every variable as capsulated one.

Better to make all these variable in one structure,

Write this code:

struct Car
{
int number;
int height;
.
..
...

};

Then declare new car:

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.

-to access the fields in Car


Write the coming:

myNewCar.number=7637;
myNewCar.height=130;

-Structures give programmers the idea to create classes later in Object


Oriented Programming, the raw idea.

10
General Programming Concepts

-Call by value and Reference, and the differences between them:

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.

What happens when i press build button?

Your C or C++ code enters these three phases:

Compilation Phase:

-check for Syntax and Semantic errors.


Needs a .c or .cpp file to generate an object file (.obj).

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

Try running this code:

#include<conio.h>
#include<stdio.h>
void Play();
int main()
{

11
Play();
return 0;
}
void play()
{
printf("I’m Playing!");
}

It has a link error, as the function’s name is not valid.

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.

What is the difference between Debug and Build?

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 and Dynamic Linking.

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.

Imagine that we have 3 functions, we call these 3 functions 9820 times.

How our binary code will be?

It will be so large and the .obj file also.

Now imagine that we put the definition of these 3 function in a .dll file.

.dll stands for Dynamic Link Library.

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?

When to choose what?

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.

-Differences between Performance and Speed.

This day you are going to buy a car 

The Seller: “it reaches speed 100 km per hour in 4 seconds”.

You: “Wow, this is great! I will buy!”.

Hey you!!

Did you ask him about the value of the engine’s temperature after reaching this
speed?

What about wheels temperature also?

How many litres of gas you will burn?

The answers of all these questions tell you the whole Performance of the car.

So there is a difference between Performance and Speed,

13
-Speed is only one factor of Performance.

So care about this point.

Will talk about both of them more later in session 2

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

-also about other IDEs like Netbeans and Eclipse.


they can compile other programming languages not only C, C++ or Java.

-Other compilers like GNU Compiler Collection(GCC).


a cross-platform Compiler(Platform stands for it compiles C code on any
machine regardless the Operating System running on it).

-Other Programming Languages like Python, Java and LISP.

15
Hope this tutorial was useful for you.

Hand it to your friends, no Copyrights!

Material by AbdulRahman Yasser


If you have any question or feedback, Contact me at:

mailto:help.abdulrahman.yasser@googlemail.com

Answers to asked questions will be added to this Tutorial, to simplify


it for new Readers.

Thanks for reading,

16

Você também pode gostar