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 :

struct & fonctions


Sujet :

C

  1. #1
    Membre émérite Avatar de orfix
    Homme Profil pro
    Inscrit en
    Avril 2007
    Messages
    707
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Maroc

    Informations professionnelles :
    Secteur : High Tech - Produits et services télécom et Internet

    Informations forums :
    Inscription : Avril 2007
    Messages : 707
    Par défaut struct & fonctions
    SALUT,
    voila je débute avec les enregistrements (struct), j'ai pu comprendre le concept mais il y'a un point qui m'echape ,je vous donne un exemple:


    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
    typedef struct date{
    			   int jour,mois,annee;
    			   } date;
     
    typedef struct type_etudiant{
    			   unsigned int matricule;
    			   char nom[30];
    			   char adresse[50];
    			   int filiere;
    			   int type_bac;
    			   char tel[14];
    			   date d_entree;
    			   int anne_fil;
    			   int groupe_c;
    			   int groupe_tp;
    			   date d_naissance;
    			   } type_etudiant;
    ....
     
    main()
    {
      type_etudiant classe[20];
    ....
    }
    supposant que j'ai une fonction qui a pour seul paramètre le jour de d_naissance pour l'étudiant d'indice 5..(par exemple)!!
    elle effectuera des changement sur cette valeur donc le passage sera par adresse ,le problème est comment se fera l'appelle ainsi que la déclaration (prototype)..??

    si jamais vous avez des liens sur des cours (portant sur les enregistrement & fonctions) sa me sera aussi tres utile merci

  2. #2
    Membre éprouvé
    Profil pro
    Inscrit en
    Décembre 2004
    Messages
    1 299
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Décembre 2004
    Messages : 1 299
    Par défaut
    salut, tu pourrais faire qqch comme

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
     
    void ma_fun(type_etudiant * te)
    {
    te->d_naissance.jour=7;
    }

  3. #3
    Membre habitué
    Inscrit en
    Février 2007
    Messages
    10
    Détails du profil
    Informations forums :
    Inscription : Février 2007
    Messages : 10
    Par défaut reponse a la question
    salut,la question que vous avez pose est un peu pertinente pour un debutant
    ce qu'il faut comprendre dans les structures c'est quand il sagit d'une variable simple il faut utiliser l'operateur (.),l'operateur(->) pour les pointeurs.

  4. #4
    Rédacteur
    Avatar de Zavonen
    Profil pro
    Inscrit en
    Novembre 2006
    Messages
    1 772
    Détails du profil
    Informations personnelles :
    Âge : 77
    Localisation : France

    Informations forums :
    Inscription : Novembre 2006
    Messages : 1 772
    Par défaut
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    typedef struct date{
    			   int jour,mois,annee;
    			   } date;
    Pourquoi répéter deux fois le nom du type ?
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    typedef struct {
    			   int jour,mois,annee;
    			   } date;
    suffit .
    Ce qu'on trouve est plus important que ce qu'on cherche.
    Maths de base pour les nuls (et les autres...)

  5. #5
    Expert confirmé

    Profil pro
    Inscrit en
    Janvier 2007
    Messages
    10 610
    Détails du profil
    Informations personnelles :
    Âge : 67
    Localisation : France

    Informations forums :
    Inscription : Janvier 2007
    Messages : 10 610
    Billets dans le blog
    2
    Par défaut
    et le nom précédent l'accolade dans un typedef struct est le nom du tag du type, celui suivant l'accolade est celui du type.

    Donc ce sera

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    typedef struct Tag_date {
     
       int jour ;
       int mois ;
     
    } date ;
    La norme C99 (http://www.open-std.org/JTC1/SC22/WG...docs/n1124.pdf) le dit au paragraphe 6.7.2.3

    6.7.2.3 Tags
    Constraints
    1 A specific type shall have its content defined at most once.
    .....
    3 All declarations of structure, union, or enumerated types that have the same scope and use the same tag declare the same type. The type is incomplete 109) until the closing brace of the list defining the content, and complete thereafter.

    4 Two declarations of structure, union, or enumerated types which are in different scopes or use different tags declare distinct types. Each declaration of a structure, union, or enumerated type which does not include a tag declares a distinct type.

    5 A type specifier of the form
    struct-or-union identifieropt { struct-declaration-list }
    or
    enum identifier { enumerator-list }
    or
    enum identifier { enumerator-list , }
    declares a structure, union, or enumerated type. The list defines the structure content, union content, or enumeration content. If an identifier is provided, 110) the type specifier also declares the identifier to be the tag of that type.

    6 A declaration of the form
    struct-or-union identifier ;
    specifies a structure or union type and declares the identifier as a tag of that type. 111)

    7 If a type specifier of the form
    struct-or-union identifier
    occurs other than as part of one of the above forms, and no other declaration of the identifier as a tag is visible, then it declares an incomplete structure or union type, and declares the identifier as the tag of that type.111)

    8 If a type specifier of the form
    struct-or-union identifier
    or
    enum identifier
    occurs other than as part of one of the above forms, and a declaration of the identifier as a tag is visible, then it specifies the same type as that other declaration, and does not redeclare the tag.

    9 EXAMPLE 1 This mechanism allows declaration of a self-referential structure.
    struct tnode {
    int count;
    struct tnode *left, *right;
    };
    specifies a structure that contains an integer and two pointers to objects of the same type. Once this declaration has been given, the declaration
    struct tnode s, *sp;
    declares s to be an object of the given type and sp to be a pointer to an object of the given type. With these declarations, the expression sp->left refers to the left struct tnode pointer of the object to which sp points; the expression s.right->count designates the count member of the right struct
    tnode pointed to from s.

    10 The following alternative formulation uses the typedef mechanism:

    typedef struct tnode TNODE;
    struct tnode {
    int count;
    TNODE *left, *right;
    };
    TNODE s, *sp;
    Et les notes :

    109) An incomplete type may only by used when the size of an object of that type is not needed. It is not needed, for example, when a typedef name is declared to be a specifier for a structure or union, or
    when a pointer to or a function returning a structure or union is being declared. (See incomplete types in 6.2.5.) The specification has to be complete before such a function is called or defined.

    110) If there is no identifier, the type can, within the translation unit, only be referred to by the declaration
    of which it is a part. Of course, when the declaration is of a typedef name, subsequent declarations
    can make use of that typedef name to declare objects having the specified structure, union, or
    enumerated type.

    111) A similar construction with enum does not exist.

  6. #6
    Rédacteur
    Avatar de Zavonen
    Profil pro
    Inscrit en
    Novembre 2006
    Messages
    1 772
    Détails du profil
    Informations personnelles :
    Âge : 77
    Localisation : France

    Informations forums :
    Inscription : Novembre 2006
    Messages : 1 772
    Par défaut
    Merci à Souviron pour la mise au point sur Tag et Tag_type, mais en pratique, l'utilisation d'un tag se justifie lorsqu'on veut masquer des définitions de types (les définir au niveau du fichier d'implémentation).
    Ceci implique une instruction typedef séparée de la définition (au niveau du header donc).
    Dans la mesure où on combine le typedef avec la définition, il n'est plus possible de masquer.
    Je ne vois donc pas l'intérêt dans ce cas de définir un tag qu'il soit égal au nom de type ou différent.
    Ce qu'on trouve est plus important que ce qu'on cherche.
    Maths de base pour les nuls (et les autres...)

  7. #7
    Membre émérite Avatar de orfix
    Homme Profil pro
    Inscrit en
    Avril 2007
    Messages
    707
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Maroc

    Informations professionnelles :
    Secteur : High Tech - Produits et services télécom et Internet

    Informations forums :
    Inscription : Avril 2007
    Messages : 707
    Par défaut
    MERCI pour vos réponses a tous ....

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Réponses: 5
    Dernier message: 19/02/2014, 21h22
  2. Réponses: 5
    Dernier message: 21/09/2008, 09h45
  3. probleme fonction return struct
    Par ninours23 dans le forum Débuter
    Réponses: 4
    Dernier message: 18/02/2008, 10h44
  4. Réponses: 15
    Dernier message: 22/09/2007, 15h28
  5. Réponses: 4
    Dernier message: 11/09/2005, 01h21

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