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

Bibliothèque standard C Discussion :

Strcpy petite question


Sujet :

Bibliothèque standard C

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre éclairé
    Profil pro
    Inscrit en
    Septembre 2007
    Messages
    371
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Septembre 2007
    Messages : 371
    Par défaut Strcpy petite question
    Voici mon code qui me dit erreur de correspondance de type ...
    si le compil' le dit cest que ca doit etre vrai , mais la je n'arrive vraiment pas a resoudre le probleme

    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
    //---------------------------------------------------------------------------
    #pragma hdrstop
    #pragma argsused
    #include <stdio.h>
    #include <string.h>
    #include <conio.h>
    //-------------------------------------------------------------------------
    void saisir_chain(char *temp){
            printf (" \n temp : ");
            scanf("%s",temp);
            printf("\n %s",temp);
    }
    void tab_chain(char *tab1,char temp[]){
            int i=0;
            strcpy(tab1[i],temp);
            printf( "\n %s ",tab1[i]);
    }
    int main()
    {
     
            char tab1[3][20];
            char temp[20];
            saisir_chain(temp);
            tab_chain(tab1,temp);
            return 0;
    }
    //---------------------------------------------------------------------------

  2. #2
    Membre très actif Avatar de Goundy
    Profil pro
    Étudiant
    Inscrit en
    Avril 2005
    Messages
    605
    Détails du profil
    Informations personnelles :
    Âge : 39
    Localisation : Belgique

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Avril 2005
    Messages : 605
    Par défaut
    strcpy(tab, temp) plutôt
    Compil your life guy!
    The Aures Project

  3. #3
    Membre éclairé
    Profil pro
    Inscrit en
    Septembre 2007
    Messages
    371
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Septembre 2007
    Messages : 371
    Par défaut
    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
    //---------------------------------------------------------------------------
    #pragma hdrstop
    #pragma argsused
    #include <stdio.h>
    #include <string.h>
    #include <conio.h>
    //-------------------------------------------------------------------------
    void saisir_chain(char *temp){
            printf (" \n temp : ");
            scanf("%s",temp);
            printf("\n %s",temp);
    }
    void tab_chain(char *tab1,char temp[]){
     
            strcpy(tab1,temp);
            printf( "\n %s ",tab1);
    }
    int main()
    {
     
            char tab1[3][20];
            char temp[20];
            saisir_chain(temp);
            tab_chain(tab1,temp);
            return 0;
    }
    //---------------------------------------------------------------------------
    OK jai modifié , cependant toujours conversion de pointeurs suspecte ...

  4. #4
    Membre confirmé Avatar de nicodn02
    Profil pro
    Consultant .NET
    Inscrit en
    Mars 2007
    Messages
    263
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations professionnelles :
    Activité : Consultant .NET

    Informations forums :
    Inscription : Mars 2007
    Messages : 263
    Par défaut
    Je pense que c'est le fait que tu es un tableau a 2 dimensions!!

  5. #5
    Membre éclairé
    Profil pro
    Inscrit en
    Septembre 2007
    Messages
    371
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Septembre 2007
    Messages : 371
    Par défaut
    Bah pour stocker des chaines de caracteres dans un tableau il faut 2 dimensions

  6. #6
    Membre confirmé Avatar de nicodn02
    Profil pro
    Consultant .NET
    Inscrit en
    Mars 2007
    Messages
    263
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations professionnelles :
    Activité : Consultant .NET

    Informations forums :
    Inscription : Mars 2007
    Messages : 263
    Par défaut
    non, un tableau a une dimension suffit!
    Le probleme qui pourrait se poser est si la personne entre une chaine plus grande que 28 ( car chaine est un tableau de taille 30 cad, de 0 a 29. Or une chaine se termine par un '\0' donc 28).

  7. #7
    Membre éclairé
    Profil pro
    Inscrit en
    Septembre 2007
    Messages
    371
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Septembre 2007
    Messages : 371
    Par défaut
    j'ai reduit le probleme ...
    mais a la base cest une saisie de 10 chaines dans un tableau ... d'ou les 2 dimensions.

    la saisie se fait avec une boucle pour c'est pour sa que dans le 1er code jai mis :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    int i = 0 ;
    strcpy ( tab1 [i] , temp ) ;
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    for (i=0;i<10;i++) {
          int i =0 ;
          printf("\n entrer un mot ... ");
          scanf("%s",temp) ;
          strcpy(tab1[i],temp ) ;
     
    }
    voila or ca me bloque au strcpy ...

  8. #8
    Membre éclairé
    Profil pro
    Inscrit en
    Septembre 2007
    Messages
    371
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Septembre 2007
    Messages : 371
    Par défaut
    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
    //---------------------------------------------------------------------------
    #pragma hdrstop
    #pragma argsused
    #include <stdio.h>
    #include <string.h>
    #include <conio.h>
    //---------------------------------------------------------------------------
     
    void saisir_tab(int *tab) {
    int i;
          for (i=0;i<3;i++) {
                  printf("entier : ");
                  scanf("%d",&tab[i]);
          }
            printf ("\n %d ",tab[0]);
            printf ("\n %d ",tab[1]);
            printf ("\n %d ",tab[2]);
            getch();
            clrscr();
     
    }
    void affichage_tab(int tab[]) {
            int i;
            for (i=0;i<3;i++) {
                    printf(" %d ",tab[i]);
            }
            getch();
    }
    void saisir_chain(char *temp,char *tab1){
            int i;
            for (i=0;i<3;i++) {
                  printf (" \n temp : ");
                  scanf("%s",temp);
                  strcpy(tab1[i],temp);
                  printf( "\ntab1 : %s ",tab1);
                  printf("\ntemp : %s",temp);
            }
    }
    void aff_chain(char temp[]) {
            printf("\n %s ",temp);
    }
     
    void aff_chain2(char tab1[]) {
            int i;
            for (i=0;i<3;i++) {
                    printf("\n %s ",tab1[i]);
            }
    }
    int main()
    {       /*
            int tab[3];
     
     
            saisir_tab(tab);
            affichage_tab(tab);
            getch();   */
            char tab1[3][20];
            char temp[20];
            saisir_chain(temp,tab1);
            aff_chain(temp);
            aff_chain2(tab1);
            getch();
     
     
     
     
            return 0;
    }
    //---------------------------------------------------------------------------

  9. #9
    Expert confirmé
    Avatar de Thierry Chappuis
    Homme Profil pro
    Enseignant Chercheur
    Inscrit en
    Mai 2005
    Messages
    3 499
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 48
    Localisation : Suisse

    Informations professionnelles :
    Activité : Enseignant Chercheur
    Secteur : Industrie Pharmaceutique

    Informations forums :
    Inscription : Mai 2005
    Messages : 3 499
    Par défaut
    Citation Envoyé par Pugebad Voir le message
    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
    //---------------------------------------------------------------------------
    #pragma hdrstop
    #pragma argsused
    #include <stdio.h>
    #include <string.h>
    #include <conio.h>
    //---------------------------------------------------------------------------
     
    void saisir_tab(int *tab) {
    int i;
          for (i=0;i<3;i++) {
                  printf("entier : ");
                  scanf("%d",&tab[i]);
          }
            printf ("\n %d ",tab[0]);
            printf ("\n %d ",tab[1]);
            printf ("\n %d ",tab[2]);
            getch();
            clrscr();
     
    }
    void affichage_tab(int tab[]) {
            int i;
            for (i=0;i<3;i++) {
                    printf(" %d ",tab[i]);
            }
            getch();
    }
    void saisir_chain(char *temp,char *tab1){
            int i;
            for (i=0;i<3;i++) {
                  printf (" \n temp : ");
                  scanf("%s",temp);
                  strcpy(tab1[i],temp);
                  printf( "\ntab1 : %s ",tab1);
                  printf("\ntemp : %s",temp);
            }
    }
    void aff_chain(char temp[]) {
            printf("\n %s ",temp);
    }
     
    void aff_chain2(char tab1[]) {
            int i;
            for (i=0;i<3;i++) {
                    printf("\n %s ",tab1[i]);
            }
    }
    int main()
    {       /*
            int tab[3];
     
     
            saisir_tab(tab);
            affichage_tab(tab);
            getch();   */
            char tab1[3][20];
            char temp[20];
            saisir_chain(temp,tab1);
            aff_chain(temp);
            aff_chain2(tab1);
            getch();
     
     
     
     
            return 0;
    }
    //---------------------------------------------------------------------------
    Mon compilo me donne les erreurs suivantes:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    Compiling: main.c
    /home/thierry/devel/sdz/main.c: In function «saisir_chain":
    /home/thierry/devel/sdz/main.c:41: erreur: passing argument 1 of «strcpy" makes pointer from integer without a cast
    /home/thierry/devel/sdz/main.c: In function «aff_chain2":
    /home/thierry/devel/sdz/main.c:53: attention : format «%s" expects type «char *", but argument 2 has type «int"
    /home/thierry/devel/sdz/main.c: Hors de toute fonction :
    /home/thierry/devel/sdz/main.c:57: attention : function declaration isn"t a prototype
    /home/thierry/devel/sdz/main.c: In function «main":
    /home/thierry/devel/sdz/main.c:66: erreur: passing argument 2 of «saisir_chain" from incompatible pointer type
    /home/thierry/devel/sdz/main.c:68: erreur: passing argument 1 of «aff_chain2" from incompatible pointer type
    Process terminated with status 1 (0 minutes, 2 seconds)
    5 errors, 0 warnings
    Voici comment je réécrirais ton code:

    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
    76
    77
    78
    79
    80
    81
    #include <stdio.h>
    #include <string.h>
     
    #define CHAINES_TAILLE_MAX 20
    #define CHAINES_NB 3
     
    static void saisir_ligne(char *tampon, size_t taille, FILE *fp);
     
    void
    saisir_chaines(char tab[][CHAINES_TAILLE_MAX], size_t n_lignes)
    {
        size_t i;
     
        for (i = 0; i < n_lignes ;i++)
        {
            printf("Entrez la %d%s chaine: ", i+1, (i > 0) ? "ème" : "ère");
            fflush(stdout);
            saisir_ligne(tab[i], CHAINES_TAILLE_MAX, stdin);
        }
    }
     
    void
    afficher_chaines(char tab[][CHAINES_TAILLE_MAX], size_t n_lignes)
    {
        size_t i;
     
        for (i = 0; i < n_lignes; i++)
        {
            puts(tab[i]);
        }
    }
     
    int main(void)
    {
        char tableau[CHAINES_NB][CHAINES_TAILLE_MAX] = {{""}};
     
        saisir_chaines(tableau, CHAINES_NB);
        afficher_chaines(tableau, CHAINES_NB);
     
        return 0;
    }
     
    /* -tc- fonctions destinees a la saisie de ligne */
     
    /* -tc- Elimine de caractere de fin de ligne saisi par fgets() et nettoie le
       tampon du flux entrant si necessaire */
    static void
    fclean(char *tampon, FILE *fp)
    {
        if (tampon != NULL && fp != NULL)
        {
            char *pc = strchr(tampon, '\n');
     
            if (pc != NULL)
            {
                *pc = 0;
            }
            else
            {
                int c;
     
                while ((c = fgetc(fp)) != '\n' && c != EOF)
                {
                }
            }
        }
    }
     
    /* -tc- Permet la saisie securisee d'une ligne et vide de tampon du flux
       d'entrant si necessaire */
    static void
    saisir_ligne(char *tampon, size_t taille, FILE *fp)
    {
        if (tampon != NULL && taille > 1 && fp != NULL)
        {
            if (fgets(tampon, taille, fp) != NULL)
            {
                fclean(tampon, fp);
            }
        }
    }
    Thierry
    "The most important thing in the kitchen is the waste paper basket and it needs to be centrally located.", Donald Knuth
    "If the only tool you have is a hammer, every problem looks like a nail.", probably Abraham Maslow

    FAQ-Python FAQ-C FAQ-C++

    +

  10. #10
    Membre chevronné Avatar de corentin59
    Profil pro
    Inscrit en
    Octobre 2006
    Messages
    462
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2006
    Messages : 462
    Par défaut
    Ou alors, Thierry a été plus rapide que moi...

    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
    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
     
    #define NBCHAR 20
    #define NBMOT 3
     
    void saisir_chain(char *temp) {
        printf ("temp : ");
        fgets(temp,NBCHAR,stdin);
        if ( temp[strlen(temp)-1]=='\n' ) temp[strlen(temp)-1] = '\0';
        printf("chaine entree : %s\n",temp);
    }
     
    void tab_chain(char *tab,char *temp) {
        strcpy(tab,temp);
        printf("chaine copiee : %s\n",tab);
    }
     
    int main(void) {
        char tab[NBMOT][NBCHAR];
        char temp[NBCHAR];
        int i;
     
        for (i=0;i<NBMOT;i++) {
            printf("chaine n %d/%d\n",i+1,NBMOT);
            saisir_chain(temp);
            tab_chain(tab[i],temp);
        }
     
        return 0;
    }

Discussions similaires

  1. [Visuel XP] Petite question sur le theme XP...
    Par ZoumZoumMan dans le forum C++Builder
    Réponses: 12
    Dernier message: 20/01/2005, 14h41
  2. [CR8.5] petite question ..
    Par mcrocher dans le forum SAP Crystal Reports
    Réponses: 1
    Dernier message: 13/09/2004, 15h04
  3. Une petite question
    Par Etienne1 dans le forum MS SQL Server
    Réponses: 3
    Dernier message: 10/08/2004, 16h19
  4. [FOREIGN KEY] petite question bete ...
    Par dzincou dans le forum PostgreSQL
    Réponses: 5
    Dernier message: 13/01/2004, 16h35
  5. Petite question sur les performances de Postgres ...
    Par cb44 dans le forum PostgreSQL
    Réponses: 5
    Dernier message: 13/01/2004, 13h49

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