Exemple d’utilisation des pointeurs comme des tableaux

Author:


Download

#include 

using namespace std;

void modifier(int* const tab, const int NOMBRE_ELEMENTS);
void afficher(const int* const tab, const int NOMBRE_ELEMENTS);

int main()
{
    int chiffreAffaire[3] = {1000, 10000, 100000};

    cout << *chiffreAffaire << endl; //Pointeur sur le prémier élément du tableau(500)
    cout << *(chiffreAffaire + 1) << endl;
    cout << *(chiffreAffaire + 2) << endl;

    modifier(chiffreAffaire, 3);//Afficher le contenu du tableau

    afficher(chiffreAffaire, 3);//Afficher le contenu du tableau

    return 0;
}

void modifier(int* const tab, const int NOMBRE_ELEMENTS)
{
	/*
	 ici la variable tab est considérée comme un tableau
	 grâce au pointeur
	 */
    for (int i = 0; i < NOMBRE_ELEMENTS; ++i)
        tab[i] *= 10; //Multiplier chaque élément du tableau par 10
}

void afficher(const int* const tab, const int NOMBRE_ELEMENTS)
{
		/*
	 ici ausi la variable tab est considérée comme un tableau
	 grâce au pointeur
	 */
    for (int i = 0; i < NOMBRE_ELEMENTS; ++i)
        cout << tab[i] << endl;
}

Leave a Reply

Your email address will not be published. Required fields are marked *