• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

[C] Introducting Pointers

Mock

Mock the bear (MTB)
Joined
Jul 29, 2008
Messages
619
Reaction score
106
Location
Brazil
This tutorial is in C, some (most) say,

Code:
HURR DURR, IMMA C++ PROGAERMER!
INT MAIN(){
  PRINTF("HAI");
  RETUERN 0;
}
And also some say that are c++ programmers but dont know how to start using classes.
XP


Today i will (try) take a psuperficial introduction to POINTERS!
Some scary out with this :p

The reason its because pointers are not always trivial, you have to think a lil more so the most of begginers "programmers" stuck on pointers and then just skip it.


To start, what is a pointer?
Pointer is something to point something
you-dont-say.gif


Lets take an example.

When you do this:
int var = 7;
You atributed some region of memory to store an int type of number as 7.
So then, this var is stored in some memory that are registred as an memory position.
Pointers point to memory positions.

If you do this:
printf("%d",&var);
You will see an number. This number is the memory position of the "var".

Now introducin' POINTERZ.

int *point;

This is how you declare a pointer.
But this pointer by himself is pointing to something that we call "memory garbage", you need to ser what is to that point, so the do it:
point = &var;
You atributted to pointer point to memory aderess of var.
If you use point = var; you will get an error. Take in mind:
int *var; and int var; DONT ARE THE SAME! The first its a POINTER (memory position)to int, the another IS THE INT
You can convert an memory position to int but is useless.

So then you can acess the content in memory position by the pointer using:
printf("%d",*point);
That going to print 7


You might be asking WHY and WHERE i am going to use this?
Relax, the things is going to ble clear (or not)

When you create an array like:

int ids[10];
you created an pointer.
I will show.
Run this:
Code:
#include <stdio.h>
#include <stdlib.h>
int main(){
int ids[] = {10,5,8,2,4,0,1,3,6,7};

printf("%d\n",*(ids));
printf("%d\n",*(ids+1));
printf("%d\n",*(ids+5));
printf("%d\n",*(ids+9));
return 0;
}

Cool heh? :3

When you create an array you say to you PC reserv an amount of memory. In our case, we start with ids var, lets assume the ids are on memory position 1000, int vars use 4 memory position, so then your pc will allocate 10 positions of memory to the array starting form the 1000. as int use 4 memory position, the first is 1000, thesecond 1004, 1008 and go on.
When you use ids+N, every n is going to jump 4 mem positions because int uses 4 mem position. In char it uses only 1, float 8. long int 8, long float 16...
You dont need to know this, you need to know THE VAR "ids" is a pointer!

So you can do this:

Code:
#include <stdio.h>
#include <stdlib.h>
int sum(int *arr,int size){
    int i= 0;
    int total=0;
    for (i=0;i<size;i++)
    {
        total = total+arr[i]; //or *(arr+i)
        arr[i] = arr[i]*10;
    }
    return total;
}
int main(){
int ids[] = {10,5,8,2,4,0,1,3,6,7};

  printf("%d\n",sum(ids,10));
  printf("%d\n",ids[0]);
return 0;

Explanation:
You create the array, and send it to a function and the size of this array.
As the array is a pointer i put (int *arr) and the size. i ran a for adding all the content in the array and mul all the terms of array to 10.
So the i print the content. and in the next line i print the first value. But as you see, i put the first value as 10 and it shows 100.

:)

Its because i sent the pointer to all array and changed it :p
This is a way to "return" more than one values in c.

If you need a matrix, you just use this:
int **matrix;
This is a pointer that point to int pointers!
13867d1345746301-mafia-campus-town-showdown-signup-inception_meme__1_.png



I said about int pointers, but it works the same for all type of vars, to strucs form classes :p
(Also when you open a fise using an fopen its an pointet that point to content in file).

Its dumb you got an value content 1gb of information and just send that to another position of memory every time you use it in a function. Just USE a il pointer (POINTERS ALWAYS USE 4 MEM POSITIONS!!!)


Pointers is LONG topic in c programming, i am just introducing it.

Curiosity: EVERYTHING, every var is an number :p
Even strings, classes, arrays, ALL is an number ^^
 

Attachments

Last edited:
Back
Top