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 :

Problème pointeur tableau


Sujet :

C

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre actif
    Homme Profil pro
    Inscrit en
    Mars 2013
    Messages
    36
    Détails du profil
    Informations personnelles :
    Sexe : Homme

    Informations forums :
    Inscription : Mars 2013
    Messages : 36
    Par défaut Problème pointeur tableau
    Bonsoir
    je suis débutant en C et j'ai un problème avec un programme
    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
     
    int first(int arr[], int low, int high)
    {
      if(high >= low)
      {
     
        int mid = low + (high - low)/2;
     
     
        if ( ( mid == 0 || arr[mid-1] == 0) && arr[mid] == 1)
          return mid;
     
        // if the element is 0, recur for right side
        else if (arr[mid] == 0)
        return first(arr, (mid + 1), high);
     
        else // If element is not first 1, recur for left side
          return first(arr, low, (mid -1));
      }
      return -1;
    }
    int main()
    { int R=3,C=3;
        int mat[R][C] = { {0, 0, 0, 1},
            {0, 1, 1, 1},
            {1, 1, 1, 1},
            {0, 0, 0, 0}
        };
        int i,k=0,index;
        int tab[5];
        for (i = 0; i < R; i++)
        {
         index = first (mat[i],0, C-1);
         tab[k]=index;
           k++;
          //printf("Index of row is %d \n", index);
     
        }
    quand je compile :
    attention : passing argument 1 of 'first' from incompatible pointer type [enabled by default]|
    note: expected 'int *' but argument is of type 'float *'|
    attention : variable 'tab' set but not used [-Wunused-but-set-variable]|
    ||=== Build finished: 2 errors, 0 warnings (0 minutes, 0 seconds) ===|

  2. #2
    Membre Expert
    Avatar de imperio
    Homme Profil pro
    Étudiant
    Inscrit en
    Mai 2010
    Messages
    869
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Ain (Rhône Alpes)

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

    Informations forums :
    Inscription : Mai 2010
    Messages : 869
    Par défaut
    Tu déclares un tableau de 4x4 en lui donnant une taille de 3x3. Pour le reste ça compile nickel sur mon pc.

  3. #3
    Membre émérite
    Avatar de bpy1401
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Mars 2003
    Messages
    511
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 64
    Localisation : France, Eure (Haute Normandie)

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : Industrie

    Informations forums :
    Inscription : Mars 2003
    Messages : 511
    Par défaut
    Bonjour,

    Tu passe en argument un tableau à deux dimensions, mais ta fonction s'attend à un tableau à une dimension.
    Page sur Developpez : http://pbriand.developpez.com

  4. #4
    Membre Expert
    Avatar de imperio
    Homme Profil pro
    Étudiant
    Inscrit en
    Mai 2010
    Messages
    869
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Ain (Rhône Alpes)

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

    Informations forums :
    Inscription : Mai 2010
    Messages : 869
    Par défaut
    @bpy1401: j'ai beau chercher, je vois pas du tout où il le fait...

    @benois123: la ligne 17 est inutile au passage, le else ne sert à rien, tu peux mettre directement ton return.

  5. #5
    Modérateur

    Avatar de Bktero
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Juin 2009
    Messages
    4 493
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 37
    Localisation : France, Loire Atlantique (Pays de la Loire)

    Informations professionnelles :
    Activité : Ingénieur développement logiciels

    Informations forums :
    Inscription : Juin 2009
    Messages : 4 493
    Billets dans le blog
    1
    Par défaut
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    int R=3,C=3;
     
        int mat[R][C] = { {0, 0, 0, 1},
            {0, 1, 1, 1},
            {1, 1, 1, 1},
            {0, 0, 0, 0}
    Ca, ça fait 4*4 et non 3*3 !

    Je n'ai d'ailleurs pas d'erreur avec des flottants, mais comme imperio, des tonnes d'erreurs et de warning à cause de cet extrait de code.

    EDIT : il semblerait que gcc n'accepte pas cette écriture, même en donnant la valeur 4 à R et C. En effet, il semble considérer que cette taille variable pourrait contredire la taille de l'initialisation statique et il le fait savoir :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    int main(void)
    { 
     
        int R=4, C=4;
        int mat[R][C] = 
        {   {0, 0, 0, 1},
            {0, 1, 1, 1},
            {1, 1, 1, 1},
            {0, 0, 0, 0}
        };
     
      return 0;
    }
    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
    Compilation finished with errors:
    source.c: In function 'main':
    source.c:6:5: error: variable-sized object may not be initialized
    source.c:7:5: warning: excess elements in array initializer [enabled by default]
    source.c:7:5: warning: (near initialization for 'mat[0]') [enabled by default]
    source.c:7:5: warning: excess elements in array initializer [enabled by default]
    source.c:7:5: warning: (near initialization for 'mat[0]') [enabled by default]
    source.c:7:5: warning: excess elements in array initializer [enabled by default]
    source.c:7:5: warning: (near initialization for 'mat[0]') [enabled by default]
    source.c:7:5: warning: excess elements in array initializer [enabled by default]
    source.c:7:5: warning: (near initialization for 'mat[0]') [enabled by default]
    source.c:7:5: warning: excess elements in array initializer [enabled by default]
    source.c:7:5: warning: (near initialization for 'mat') [enabled by default]
    source.c:8:9: warning: excess elements in array initializer [enabled by default]
    source.c:8:9: warning: (near initialization for 'mat[0]') [enabled by default]
    source.c:8:9: warning: excess elements in array initializer [enabled by default]
    source.c:8:9: warning: (near initialization for 'mat[0]') [enabled by default]
    source.c:8:9: warning: excess elements in array initializer [enabled by default]
    source.c:8:9: warning: (near initialization for 'mat[0]') [enabled by default]
    source.c:8:9: warning: excess elements in array initializer [enabled by default]
    source.c:8:9: warning: (near initialization for 'mat[0]') [enabled by default]
    source.c:8:9: warning: excess elements in array initializer [enabled by default]
    source.c:8:9: warning: (near initialization for 'mat') [enabled by default]
    source.c:9:9: warning: excess elements in array initializer [enabled by default]
    source.c:9:9: warning: (near initialization for 'mat[0]') [enabled by default]
    source.c:9:9: warning: excess elements in array initializer [enabled by default]
    source.c:9:9: warning: (near initialization for 'mat[0]') [enabled by default]
    source.c:9:9: warning: excess elements in array initializer [enabled by default]
    source.c:9:9: warning: (near initialization for 'mat[0]') [enabled by default]
    source.c:9:9: warning: excess elements in array initializer [enabled by default]
    source.c:9:9: warning: (near initialization for 'mat[0]') [enabled by default]
    source.c:9:9: warning: excess elements in array initializer [enabled by default]
    source.c:9:9: warning: (near initialization for 'mat') [enabled by default]
    source.c:10:9: warning: excess elements in array initializer [enabled by default]
    source.c:10:9: warning: (near initialization for 'mat[0]') [enabled by default]
    source.c:10:9: warning: excess elements in array initializer [enabled by default]
    source.c:10:9: warning: (near initialization for 'mat[0]') [enabled by default]
    source.c:10:9: warning: excess elements in array initializer [enabled by default]
    source.c:10:9: warning: (near initialization for 'mat[0]') [enabled by default]
    source.c:10:9: warning: excess elements in array initializer [enabled by default]
    source.c:10:9: warning: (near initialization for 'mat[0]') [enabled by default]
    source.c:10:9: warning: excess elements in array initializer [enabled by default]
    source.c:10:9: warning: (near initialization for 'mat') [enabled by default]
    source.c:6:9: warning: unused variable 'mat' [-Wunused-variable]
    Alors que :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    int main(void)
    { 
     
        int R=4, C=4;
        int mat[4][4] = 
        {   {0, 0, 0, 1},
            {0, 1, 1, 1},
            {1, 1, 1, 1},
            {0, 0, 0, 0}
        };
     
      return 0;
    }
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    Compilation finished with warnings:
    source.c: In function 'main':
    source.c:6:9: warning: unused variable 'mat' [-Wunused-variable]
    source.c:5:14: warning: unused variable 'C' [-Wunused-variable]
    source.c:5:9: warning: unused variable 'R' [-Wunused-variable]
    Avec gcc et les options -std=c99 -Wall -Wextra.

    @benois123: la ligne 17 est inutile au passage, le else ne sert à rien, tu peux mettre directement ton return.
    Il ne mange pas de pain et aide à la lisibilité du code, bien qu'effectivement inutile

  6. #6
    Membre Expert
    Avatar de imperio
    Homme Profil pro
    Étudiant
    Inscrit en
    Mai 2010
    Messages
    869
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Ain (Rhône Alpes)

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

    Informations forums :
    Inscription : Mai 2010
    Messages : 869
    Par défaut
    Il ne mange pas de pain et aide à la lisibilité du code, bien qu'effectivement inutile
    Je pense que ça dépend de la personne. Pour moi c'est moins agréable, pour d'autre c'est plus clair. Les deux cas sont corrects.

Discussions similaires

  1. Problème pointeur dans un tableau à deux dimensions.
    Par Hunken dans le forum Débuter
    Réponses: 2
    Dernier message: 12/03/2015, 11h32
  2. Problème déclaration tableau de pointeur.
    Par BlackStitch dans le forum Débuter
    Réponses: 2
    Dernier message: 16/12/2014, 01h35
  3. [débutant] problème création tableau (pointeurs and Cie)
    Par olivier1209 dans le forum Débuter
    Réponses: 13
    Dernier message: 08/03/2009, 15h54
  4. Problème de tableau de pointeurs
    Par befast dans le forum C++
    Réponses: 19
    Dernier message: 05/10/2006, 15h22
  5. [MFC] Problème pointeur sur une classe
    Par mick74 dans le forum MFC
    Réponses: 7
    Dernier message: 14/04/2004, 14h17

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