Twitter iPhone pliant OnePlus 11 PS5 Disney+ Orange Livebox Windows 11

Pointeur et tableau en C

5 réponses
Avatar
daniel
Bonjour,

J'ai ds probl=E8mes de pointeur et tableau
voici mon prog:

/********************************************************************/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <fcntl.h>


typedef char buf5_10[5][10];

buf5_10 ligne=3D{{"1111"},{"2222"},{"3333"},{"4444"},{"5555"}};
buf5_10 ligne_temp;
char bufline[100];


void formatageLine(char *line, buf5_10 *buf) {

printf("formatageligne %s %s %s %s %s\n",buf[0], buf[1], buf[2], buf[3], =

buf[4]);

}

/***********************************************
Fonction main
***********************************************/
int main(int argc, char **argv)
{

printf("ligne %s %s %s %s %s\n",ligne[0], ligne[1],ligne[2],=20
ligne[3],ligne[4]);
formatageLine(bufline,&ligne);


return 0;
}





Resultat:

ligne 1111 2222 3333 4444 5555
formatageligne 1111 P
=F0=FF=FFo=EC


Pourquoi n'ai je pas le meme resultat ?

Merci

Daniel

5 réponses

Avatar
Pierre Maurette
Bonjour,

J'ai ds problèmes de pointeur et tableau
voici mon prog:

/********************************************************************/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <fcntl.h>


typedef char buf5_10[5][10];

buf5_10 ligne={{"1111"},{"2222"},{"3333"},{"4444"},{"5555"}};
buf5_10 ligne_temp;
char bufline[100];


void formatageLine(char *line, buf5_10 *buf) {
void formatageLine(char *line, buf5_10 buf) {




printf("formatageligne %s %s %s %s %sn",buf[0], buf[1], buf[2], buf[3],
buf[4]);

}

/***********************************************
Fonction main
***********************************************/
int main(int argc, char **argv)
{

printf("ligne %s %s %s %s %sn",ligne[0], ligne[1],ligne[2],
ligne[3],ligne[4]);
formatageLine(bufline,&ligne);
formatageLine(bufline, ligne);



return 0;
}





Resultat:

ligne 1111 2222 3333 4444 5555
formatageligne 1111 P
ðÿÿoì


ligne 1111 2222 3333 4444 5555
formatageligne 1111 2222 3333 4444 5555



Pourquoi n'ai je pas le meme resultat ?
C passe les tableaux par pointeur "tout seul comme un grand".


--
Pierre

Avatar
Richard Delorme
Bonjour,

J'ai ds problèmes de pointeur et tableau
voici mon prog:

/********************************************************************/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <fcntl.h>


typedef char buf5_10[5][10];

buf5_10 ligne={{"1111"},{"2222"},{"3333"},{"4444"},{"5555"}};
buf5_10 ligne_temp;
char bufline[100];


void formatageLine(char *line, buf5_10 *buf) {


void formatageLine(char *line, buf5_10 buf) {

[...]
int main(int argc, char **argv)
{

printf("ligne %s %s %s %s %sn",ligne[0], ligne[1],ligne[2],
ligne[3],ligne[4]);
formatageLine(bufline,&ligne);


formatageLine(bufline, ligne);

[...]
Resultat:

ligne 1111 2222 3333 4444 5555
formatageligne 1111 P
ðÿÿoì



Pourquoi n'ai je pas le meme resultat ?


C'est un problème de type incompatible, dans main, ligne est de type
char [5][10], dans formatageLigne, le type de bug est char *[5][10]. La
première valeur d'un tableau ayant la même adresse que le tableau,
buf[0] (qui est de type char[5][10]) est convertit en char* et affiche
bien 1111, par contre buf[1] pointe vers le tableau char[5][10] suivant,
qui n'existe pas et affiche n'importe quoi (ou plante ou fait n'importe
quoi comme tout comportement indéfini qui se respecte).

Le typedef, en cachant le type (tableau [5][10]), induit sans doute
l'erreur ici.

--
Richard

Avatar
Emmanuel Delahaye
daniel wrote on 23/04/05 :

Ton code mise au point:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

typedef char buf5_10[5][10];

buf5_10 ligne {
{"1111"},
{"2222"},
{"3333"},
{"4444"},
{"5555"}
};
buf5_10 ligne_temp;
char bufline[100];


/* -ed-
* pourquoi une '*' a 'buf ? Il ne te plait pas ton type buf5_10 ?

void formatageLine (char *line, buf5_10 * buf)
*/
void formatageLine (char *line, buf5_10 buf)
{
printf ("formatage ligne %s %s %s %s %sn", buf[0], buf[1], buf[2],
buf[3], buf[4]);
}

int main_ (void)
{

printf ("ligne %s %s %s %s %sn", ligne[0], ligne[1], ligne[2],
ligne[3], ligne[4]);

/* -ed-
* evidemment, le '&' n'a plus lieu d'etre...

formatageLine (bufline, &ligne);
*/
formatageLine (bufline, ligne);

return 0;
}

quand aux globales (comme bufline), on évite...

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

"Clearly your code does not meet the original spec."
"You are sentenced to 30 lashes with a wet noodle."
-- Jerry Coffin in a.l.c.c++
Avatar
Emmanuel Delahaye
(supersedes )

daniel wrote on 23/04/05 :

Ton code mise au point:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

typedef char buf5_10[5][10];

buf5_10 ligne {
{"1111"},
{"2222"},
{"3333"},
{"4444"},
{"5555"}
};
buf5_10 ligne_temp;
char bufline[100];

/* -ed-
* pourquoi une '*' a 'buf ? Il ne te plait pas ton type buf5_10 ?

void formatageLine (char *line, buf5_10 * buf)
*/
void formatageLine (char *line, buf5_10 buf)
{
printf ("formatage ligne %s %s %s %s %sn", buf[0], buf[1], buf[2],
buf[3], buf[4]);
}

int main (void)
{

printf ("ligne %s %s %s %s %sn", ligne[0], ligne[1], ligne[2],
ligne[3], ligne[4]);

/* -ed-
* evidemment, le '&' n'a plus lieu d'etre...

formatageLine (bufline, &ligne);
*/
formatageLine (bufline, ligne);

return 0;
}

quand aux globales (comme bufline), on évite...

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

"C is a sharp tool"
Avatar
daniel
Merci beaucoup pour toutes les reponses.

Daniel


daniel wrote:
Bonjour,

J'ai ds problèmes de pointeur et tableau
voici mon prog:

/********************************************************************/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <fcntl.h>


typedef char buf5_10[5][10];

buf5_10 ligne={{"1111"},{"2222"},{"3333"},{"4444"},{"5555"}};
buf5_10 ligne_temp;
char bufline[100];


void formatageLine(char *line, buf5_10 *buf) {

printf("formatageligne %s %s %s %s %sn",buf[0], buf[1], buf[2], buf[3] ,
buf[4]);

}

/***********************************************
Fonction main
***********************************************/
int main(int argc, char **argv)
{

printf("ligne %s %s %s %s %sn",ligne[0], ligne[1],ligne[2],
ligne[3],ligne[4]);
formatageLine(bufline,&ligne);


return 0;
}





Resultat:

ligne 1111 2222 3333 4444 5555
formatageligne 1111 P
ðÿÿoì


Pourquoi n'ai je pas le meme resultat ?

Merci

Daniel