IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

C Discussion :

Fonction qui rend un pointeur


Sujet :

C

  1. #1
    WaM
    WaM est déconnecté
    Membre habitué
    Profil pro
    Inscrit en
    Janvier 2004
    Messages
    10
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Janvier 2004
    Messages : 10
    Par défaut Fonction qui rend un pointeur
    Bonjour
    Voila j'ai écrit une fonction qui ne prend rien en argument et qui doit rendre un pointeur vers une structure objet3d, que j'ai écrite.
    Voici mon code :

    voici la définition du type objet3d
    typedef struct {
    int x;
    int y;
    int bool_visible;
    } point2d ;

    typedef struct {
    float x;
    float y;
    float z;
    } point3d;

    typedef struct {
    unsigned short int from;
    unsigned short int to;
    } arete;

    typedef struct {
    unsigned short nb_dots;
    unsigned short int dots[MAX_DOTS];
    int R,G,B;
    } face;

    typedef struct {
    unsigned int num_sommets;
    point3d sommets[MAX_SOMMETS];
    point3d places[MAX_SOMMETS];
    point2d projetes[MAX_SOMMETS];

    unsigned int num_aretes;
    arete aretes[MAX_ARETES];

    unsigned int num_faces;
    face faces[MAX_FACES];

    float angleX;
    float angleY;
    float angleZ;

    float TransMatrix[3][1];

    } objet3d;
    dans mon main.c, dans la fonction main
    objet3d *test = NULL;
    test = make_cube(20.,20.,20.,20.,RGBToInt(0,200,0),20.,20.,20.);
    dans mon objects.h
    objet3d * make_cube(float size, float x, float y, float z, int color, float rotX, float rotY, float rotZ);
    et dans mon objects.c
    objet3d * make_cube(float size, float x, float y, float z, int color, float rotX, float rotY, float rotZ)
    {
    objet3d* obj = NULL;
    obj = (objet3d *) malloc(sizeof(objet3d));


    obj->angleX = rotX;
    obj->angleY = rotY;
    obj->angleZ = rotZ;

    obj->TransMatrix[0][0] = x;
    obj->TransMatrix[1][0] = y;
    obj->TransMatrix[2][0] = z;

    obj->num_aretes = 12;
    obj->num_faces = 6;
    obj->num_sommets = 8;

    obj->sommets[] = { {-size,-size,-size}, {size,-size,-size}, {-size,size,-size}, {size,size,-size},
    {-size,-size,size}, {size,-size,size}, {-size,size,size}, {size,size,size} };

    return obj;
    }

    A la compilation 'Compile', ca passe bien, par contre au 'Build', VC++ me crache 17 erreur et 1 warning :
    --------------------Configuration: cube - Win32 Debug--------------------
    Compiling...
    objects.c
    G:\wam\C\tinyPTC\3d\cube\objects.c(9) : error C2143: syntax error : missing '{' before '*'
    G:\wam\C\tinyPTC\3d\cube\objects.c(11) : error C2065: 'objet3d' : undeclared identifier
    G:\wam\C\tinyPTC\3d\cube\objects.c(11) : error C2065: 'obj' : undeclared identifier
    G:\wam\C\tinyPTC\3d\cube\objects.c(11) : error C2065: 'NULL' : undeclared identifier
    G:\wam\C\tinyPTC\3d\cube\objects.c(11) : error C2106: '=' : left operand must be l-value
    G:\wam\C\tinyPTC\3d\cube\objects.c(12) : error C2059: syntax error : ')'
    G:\wam\C\tinyPTC\3d\cube\objects.c(15) : error C2223: left of '->angleX' must point to struct/union
    G:\wam\C\tinyPTC\3d\cube\objects.c(16) : error C2223: left of '->angleY' must point to struct/union
    G:\wam\C\tinyPTC\3d\cube\objects.c(17) : error C2223: left of '->angleZ' must point to struct/union
    G:\wam\C\tinyPTC\3d\cube\objects.c(19) : error C2223: left of '->TransMatrix' must point to struct/union
    G:\wam\C\tinyPTC\3d\cube\objects.c(20) : error C2223: left of '->TransMatrix' must point to struct/union
    G:\wam\C\tinyPTC\3d\cube\objects.c(21) : error C2223: left of '->TransMatrix' must point to struct/union
    G:\wam\C\tinyPTC\3d\cube\objects.c(23) : error C2223: left of '->num_aretes' must point to struct/union
    G:\wam\C\tinyPTC\3d\cube\objects.c(24) : error C2223: left of '->num_faces' must point to struct/union
    G:\wam\C\tinyPTC\3d\cube\objects.c(25) : error C2223: left of '->num_sommets' must point to struct/union
    G:\wam\C\tinyPTC\3d\cube\objects.c(27) : error C2223: left of '->sommets' must point to struct/union
    G:\wam\C\tinyPTC\3d\cube\objects.c(27) : error C2059: syntax error : ']'
    G:\wam\C\tinyPTC\3d\cube\objects.c(30) : warning C4047: 'return' : 'int *' differs in levels of indirection from 'int '
    Error executing cl.exe.

    cube.exe - 17 error(s), 1 warning(s)

    Auriez-vous une idée pour corriger ce pb ? (la ligne 9 de objects.c est celle qui contient le typage de make_cube)

    merci

  2. #2
    Membre Expert
    Avatar de Pragmateek
    Homme Profil pro
    Formateur expert .Net/C#
    Inscrit en
    Mars 2006
    Messages
    2 635
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 38
    Localisation : France, Val de Marne (Île de France)

    Informations professionnelles :
    Activité : Formateur expert .Net/C#
    Secteur : Conseil

    Informations forums :
    Inscription : Mars 2006
    Messages : 2 635
    Par défaut
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    G:\wam\C\tinyPTC\3d\cube\objects.c(11) : error C2065: 'objet3d' : undeclared identifier
    G:\wam\C\tinyPTC\3d\cube\objects.c(11) : error C2065: 'obj' : undeclared identifier
    Il faut inclure le header déclarant ces objets.
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
     G:\wam\C\tinyPTC\3d\cube\objects.c(11) : error C2065: 'NULL' : undeclared identifier
    NULL est une macro peut être non définie pour ce compilateur.
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    obj->sommets[] = { {-size,-size,-size}, {size,-size,-size}, {-size,size,-size}, {size,size,-size},
    Pas la peine de signaler que c'est un tableau, devrait suffire.

  3. #3
    WaM
    WaM est déconnecté
    Membre habitué
    Profil pro
    Inscrit en
    Janvier 2004
    Messages
    10
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Janvier 2004
    Messages : 10
    Par défaut
    Citation Envoyé par seriousme
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    G:\wam\C\tinyPTC\3d\cube\objects.c(11) : error C2065: 'objet3d' : undeclared identifier
    G:\wam\C\tinyPTC\3d\cube\objects.c(11) : error C2065: 'obj' : undeclared identifier
    Il faut inclure le header déclarant ces objets.
    C'est deja fai ten fait...c'est déclaré dans le main.c, avant le #include "objects.h"...

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
     G:\wam\C\tinyPTC\3d\cube\objects.c(11) : error C2065: 'NULL' : undeclared identifier
    NULL est une macro peut être non définie pour ce compilateur.
    Si si, NULL est bien défini, je l'utilise autre part dans le prog. Je pense que cette erreur découle de celle avant, ca a du 'casser' la syntaxe.

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    obj->sommets[] = { {-size,-size,-size}, {size,-size,-size}, {-size,size,-size}, {size,size,-size},
    Pas la peine de signaler que c'est un tableau, devrait suffire.
    En effet, ca suffit, mais il me reste 12 erreurs...

    Merci deja

  4. #4
    Membre Expert
    Avatar de Pragmateek
    Homme Profil pro
    Formateur expert .Net/C#
    Inscrit en
    Mars 2006
    Messages
    2 635
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 38
    Localisation : France, Val de Marne (Île de France)

    Informations professionnelles :
    Activité : Formateur expert .Net/C#
    Secteur : Conseil

    Informations forums :
    Inscription : Mars 2006
    Messages : 2 635
    Par défaut
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
     C'est deja fai ten fait...c'est déclaré dans le main.c, avant le #include "objects.h"
    Il faut le remettre dans chaque fichier source car chaque fichier source est compilé séparément puis les fichiers objets générés sont linkés.

  5. #5
    Expert éminent
    Avatar de Emmanuel Delahaye
    Profil pro
    Retraité
    Inscrit en
    Décembre 2003
    Messages
    14 512
    Détails du profil
    Informations personnelles :
    Âge : 68
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Retraité

    Informations forums :
    Inscription : Décembre 2003
    Messages : 14 512
    Par défaut
    Citation Envoyé par WaM
    Bonjour
    Voila j'ai écrit une fonction qui ne prend rien en argument et qui doit rendre un pointeur vers une structure objet3d, que j'ai écrite.
    Voici mon code :
    Ton code est incomplet. On a du mal à voir quelle est l'organisation réelle. C'est pas très difficile de poster le code exact...

    Ceci fonctione. La mémoire n'est pas libérée...
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
     
    /* objets.h */
     
    enum
    {
       /* valeurs arbitraires... */
       MAX_DOTS = 10,
       MAX_SOMMETS = 8,
       MAX_ARETES = 8,
       MAX_FACES = 6,
     
       objet_dummy
    };
     
    typedef struct
    {
       int x;
       int y;
       int bool_visible;
    }
    point2d ;
     
    typedef struct
    {
       float x;
       float y;
       float z;
    }
    point3d;
     
    typedef struct
    {
       unsigned short int from;
       unsigned short int to;
    }
    arete;
     
    typedef struct
    {
       unsigned short nb_dots;
       unsigned short int dots[MAX_DOTS];
       int R, G, B;
    }
    face;
     
    typedef struct
    {
       unsigned int num_sommets;
       point3d sommets[MAX_SOMMETS];
       point3d places[MAX_SOMMETS];
       point2d projetes[MAX_SOMMETS];
     
       unsigned int num_aretes;
       arete aretes[MAX_ARETES];
     
       unsigned int num_faces;
       face faces[MAX_FACES];
     
       float angleX;
       float angleY;
       float angleZ;
     
       float TransMatrix[3][1];
     
    }
    objet3d;
     
    objet3d * make_cube(float size
                        , float x
                        , float y
                        , float z
                        , int color
                        , float rotX
                        , float rotY
                        , float rotZ);
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
     
    #include "objets.h"
    #include <stdlib.h>
     
    objet3d * make_cube(float size
                        , float x
                        , float y
                        , float z
                        , int color
                        , float rotX
                        , float rotY
                        , float rotZ)
    {
       objet3d* obj = malloc(sizeof * obj);
       if (obj != NULL)
       {
          const point3d sommets[MAX_SOMMETS] =
             {
                {
                   -size, -size, -size
                },
                {
                   size, -size, -size
                },
                {
                   -size, size, -size
                },
                {
                   size, size, -size
                },
                {
                   -size, -size, size
                },
                {
                   size, -size, size
                },
                {
                   -size, size, size
                },
                {
                   size, size, size
                }
             };
     
          size_t i;
          for (i = 0; i < sizeof obj->sommets / sizeof *obj->sommets; i++)
          {
             obj->sommets[i] = sommets[i];
          }
       }
     
       obj->angleX = rotX;
       obj->angleY = rotY;
       obj->angleZ = rotZ;
     
       obj->TransMatrix[0][0] = x;
       obj->TransMatrix[1][0] = y;
       obj->TransMatrix[2][0] = z;
     
       obj->num_aretes = 12;
       obj->num_faces = 6;
       obj->num_sommets = 8;
     
       return obj;
    }
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
     
    #include <stdio.h>
    #include <stdlib.h>
    #include "objets.h"
     
    /* codage arbitraire... */
    static unsigned long RGBToInt (unsigned r, unsigned g, unsigned b)
    {
       unsigned long ul = 0;
     
       ul |= (r & 0xFF) << (8 * 2);
       ul |= (g & 0xFF) << (8 * 1);
       ul |= (b & 0xFF) << (8 * 0);
    }
     
    int main ()
    {
       objet3d *test = NULL;
       test = make_cube(20., 20., 20., 20., RGBToInt(0, 200, 0), 20., 20., 20.);
       return 0;
    }

  6. #6
    Expert confirmé
    Avatar de Skyounet
    Homme Profil pro
    Software Engineer
    Inscrit en
    Mars 2005
    Messages
    6 380
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 38
    Localisation : Etats-Unis

    Informations professionnelles :
    Activité : Software Engineer
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Mars 2005
    Messages : 6 380
    Par défaut
    Citation Envoyé par WaM
    G:\wam\C\tinyPTC\3d\cube\objects.c(11) : error C2065: 'NULL' : undeclared identifier
    NULL est défini dans stdio.h n'aurais-tu pas oublier d'inclure ce fichier?

  7. #7
    Expert éminent
    Avatar de Emmanuel Delahaye
    Profil pro
    Retraité
    Inscrit en
    Décembre 2003
    Messages
    14 512
    Détails du profil
    Informations personnelles :
    Âge : 68
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Retraité

    Informations forums :
    Inscription : Décembre 2003
    Messages : 14 512
    Par défaut
    Citation Envoyé par Skyrunner
    NULL est défini dans stdio.h n'aurais-tu pas oublier d'inclure ce fichier?
    Il a surtout oublié <stdlib.h> pour malloc()...

Discussions similaires

  1. DLL:exporter une fonction qui retourne un pointeur
    Par ephemeride dans le forum C++
    Réponses: 2
    Dernier message: 29/09/2006, 11h42
  2. Fonction qui renvoie un pointeur de fonction
    Par RaphAstronome dans le forum C++
    Réponses: 11
    Dernier message: 20/08/2006, 14h06
  3. Fonction qui rend nul le bouton d'alimentation ?
    Par rpoulin dans le forum Langage
    Réponses: 7
    Dernier message: 09/10/2005, 05h03
  4. Réponses: 17
    Dernier message: 24/03/2005, 12h24
  5. fonction qui retourne un pointeur
    Par sorari dans le forum C++
    Réponses: 6
    Dernier message: 16/03/2005, 21h23

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo