Probleme de pointeur en c
Résolu
xave4552
Messages postés
53
Date d'inscription
Statut
Membre
Dernière intervention
-
xave4552 Messages postés 53 Date d'inscription Statut Membre Dernière intervention -
xave4552 Messages postés 53 Date d'inscription Statut Membre Dernière intervention -
Bonjour,
Voilat je commence a utiliser les pointeurs et j'ai un petit problème.
En effet je veut inverser l'ordre des valeurs d'un tableau.
Voilat le résultat du code:
Première colone tableau courant deuxieme colonne tableau précedent.
t[0]= -33686019 t2[0]= 0
t[1]= 9 t2[1]= 1
t[2]= 8 t2[2]= 2
t[3]= 8 t2[3]= 4
t[4]= 7 t2[4]= 4
t[5]= 4 t2[5]= 4
t[6]= 4 t2[6]= 7
t[7]= 4 t2[7]= 8
t[8]= 2 t2[8]= 8
t[9]= 1 t2[9]= 9
Je pense que cela vien de l'utilisation des pointeurs ?
Voici la fonction:
void inversion(int *t, int taille){
int i, buffer;
for (i = 0 ; i < taille /2 ; i ++){
buffer = *(t+i);
*(t+i) = *(t+(taille-i));
*(t+(taille-i)) = buffer;
};
};
Si quelqu'un aurait une solution ce serait sympa. Merci.
Voilat je commence a utiliser les pointeurs et j'ai un petit problème.
En effet je veut inverser l'ordre des valeurs d'un tableau.
Voilat le résultat du code:
Première colone tableau courant deuxieme colonne tableau précedent.
t[0]= -33686019 t2[0]= 0
t[1]= 9 t2[1]= 1
t[2]= 8 t2[2]= 2
t[3]= 8 t2[3]= 4
t[4]= 7 t2[4]= 4
t[5]= 4 t2[5]= 4
t[6]= 4 t2[6]= 7
t[7]= 4 t2[7]= 8
t[8]= 2 t2[8]= 8
t[9]= 1 t2[9]= 9
Je pense que cela vien de l'utilisation des pointeurs ?
Voici la fonction:
void inversion(int *t, int taille){
int i, buffer;
for (i = 0 ; i < taille /2 ; i ++){
buffer = *(t+i);
*(t+i) = *(t+(taille-i));
*(t+(taille-i)) = buffer;
};
};
Si quelqu'un aurait une solution ce serait sympa. Merci.
A voir également:
- Probleme de pointeur en c
- Pointeur souris - Guide
- Curseur Windows 10 : comment bien voir la souris à l'écran - Guide
- Le pointeur de mon pc portable ne marche plus - Guide
- Pointeur laser publicitaire ✓ - Forum Loisirs / Divertissements
- Pointeur souris avec rond de chargement qui clignote sans arrêts ✓ - Forum Windows
3 réponses
Je te donne un exemple :
Et la trace :
./xave
0 1 2 3 4 5 6 7 8 9
9 8 7 6 5 4 3 2 1 0
Cdlt
#include <stdio.h> static void affichage(const int* const, const size_t); static void inverse(int*, const size_t); static void affichage(const int *const tab, const size_t sz) { size_t i=0; while(i<sz) printf("%d ",tab[i++]); putchar('\n'); } static void inverse(int *const tab, const size_t sz) { size_t i; for(i=0;i<sz/2;i++) { int buffer=tab[i]; tab[i]=tab[sz-i-1]; tab[sz-i-1]=buffer; } } int main(void) { int tab[]={0,1,2,3,4,5,6,7,8,9}; affichage(tab,sizeof tab/sizeof *tab); inverse(tab,sizeof tab/sizeof *tab); affichage(tab,sizeof tab/sizeof *tab); return 0; }
Et la trace :
./xave
0 1 2 3 4 5 6 7 8 9
9 8 7 6 5 4 3 2 1 0
Cdlt
Bonjour,
Tu peux utiliser l'opérateur XOR (ou exclusif) pour échanger deux variables sans l'aide d'une troisième.
int x = 42;
int y = -21;
x = x^y;
y = x^y;
x = x^y;
/*
** Ici x = -21 et y = 42
*/
Attention cela fonctionne que avec des types scalaires (int, long, char, ...) mais pas de structure par exemple.
Tu peux utiliser l'opérateur XOR (ou exclusif) pour échanger deux variables sans l'aide d'une troisième.
int x = 42;
int y = -21;
x = x^y;
y = x^y;
x = x^y;
/*
** Ici x = -21 et y = 42
*/
Attention cela fonctionne que avec des types scalaires (int, long, char, ...) mais pas de structure par exemple.