Partage
  • Partager sur Facebook
  • Partager sur Twitter

problème fichier .h et .c pour structures

    19 avril 2019 à 17:02:40

    Bonjour,

    Dans le cadre d'un projet en C utilisant allegro je souhaite séparer mon code en plusieurs fichiers. J'ai donc créé les fichier .c: main, sous-programmes et structure et .h: sous-programmes et structure. Mais je ne savais pas quoi mettre dans le fichier structure.h, j'y ai donc mis mes prototypes de structures: (exemple: typedef struct acteur). Cela ne marche pas quand je compile, les structures ne sont pas connues. J'ai donc enlevé les fichiers structure .h et .c, pour placer mes différentes structures au début du main.c et du fichier sous-programme.c, en pensant que une fois intégré dans les fichiers les structures seraient connues. Mais quand je compile cela ne marche toujours pas. J'aimerais donc savoir comment séparer mes structures dans un fichier à part s'il vous plaît.

    Merci d'avance pour vos réponses.

    • Partager sur Facebook
    • Partager sur Twitter
      19 avril 2019 à 18:12:50

      Comme un exemple vaut parfois le meilleur des discours : 

      "MAIN.C"

      #include "ptr.h"
      #include "log.h"
      
      int main()
      {
          f_log = log_create_null();
      
          int **p = NULL;
          size_t x = 0;
          size_t *y = NULL;
          p = i_2D_allocate( ++x, &y );
          i_2D_row_add_ ( &p, x-1, 10, y );
          i_2D_line_add_( &p, &x, x+1, &y );
          i_2D_row_add_ ( &p, x-1, 5, y );
          i_2D_line_add_( &p, &x, x+1, &y );
          i_2D_display( p, x, y );
          i_2D_deallocate( p, x );
          free(y);
      
          log_display( f_log );
          log_destroy( f_log );
      
          return 0;
      }
      

      "PTR.C"

      #include "ptr.h"
      
      void g_deallocate( void *p )
      {
          if ( p ) { free( p ); }
      }
      
      int *i_allocate( size_t p_sz )
      {
          int *p = NULL;
          if ( ( p = malloc( p_sz*sizeof(*p) ) ) ) //T0
          {
              for ( size_t i=0 ; i<p_sz ; i++ )
                  p[i] = 0;
          }
          else log_add("ptr.c", "i_allocate","T0 :: p = NULL");
          return p;
      }
      
      void i_display( int *p, size_t p_sz )
      {
          if (p && p_sz > 0)
          {
              int digitInt = log10(pow(2,8*sizeof(*p    )))+1;
              int digitSiz = log10(pow(2,8*sizeof(size_t)))+1;
              printf("\nSize of p : %zu => i \356 [0:%zu]", p_sz, p_sz-1);
              for ( size_t i=0 ; i<p_sz ; i++ )
                  printf("\np[%0*zu] = %*d", digitSiz, i, digitInt, p[i]);
          }
      }
      
      int i_random( int bound, int sign )
      {
          srand(time(NULL));
          int q = sign;
      
          if ( sign == 2 )
              q = rand()%2;
      
          int r = pow ( -1., q%2 );
      
          return r*rand()%bound;
      }
      
      int *i_allocate_random( size_t p_sz, int bound, int sign )
      {
          int *p = NULL;
          if ( ( p = i_allocate( p_sz ) ) ) //T0
          {
              srand(time(NULL));
              int r, q = sign;
              for ( size_t i=0; i<p_sz ; i++ )
              {
                  if ( sign == 2 )
                      q = rand()%2;
      
                  r = pow ( -1., q%2 );
                  p[i] = r*rand()%bound;
              }
                      }
          else log_add("ptr.c", "i_allocate_random","T0 :: p = NULL");
          return p;
      }
      
      bool i_realloc( int **p, size_t *p_sz, size_t new_sz )
      {
          bool b = false;
          if ( *p && new_sz > 0 ) //T0
          {
              int *p_realloc = NULL;
              if ( ( p_realloc = realloc( (*p), new_sz*sizeof(*p_realloc) ) ) ) //T1
              {
                  (*p) = p_realloc;
                  for ( size_t i=*p_sz ; i<new_sz ; i++ )
                      (*p)[i] = 0;
      
                  *p_sz = new_sz;
                  b = true;
              }
              else log_add("ptr.c", "i_realloc", "T1 :: p_realloc = NULL");
          }
          else
          {
              if ( !p )
                  log_add("ptr.c", "i_realloc", "T0 :: p = NULL");
      
              if ( new_sz == 0 )
                  log_add("ptr.c", "i_realloc", "T0 :: new_sz <= 0");
          }
          return b;
      }
      
      bool i_insert( int **p, size_t *p_sz, size_t offset, int v )
      {
          bool b = false;
          if ( *p && 0 <= offset && offset < *p_sz+1 ) //T1
          {
              if ( i_realloc( p, p_sz, *p_sz+1 ) == true ) //T2
              {
                   for ( size_t i=(*p_sz)-1 ; i>offset ; i-- )
                      (*p)[i] = (*p)[i-1];
      
                  (*p)[offset] = v;
                  b = true;
              }
              else log_add("ptr.c", "i_insert", "T1 :: i_realloc returned false");
          }
          else
          {
              if ( !(*p) )
                  log_add("ptr.c", "i_insert", "T0 :: (*p) = NULL");
      
              if ( !(0 <= offset && offset < *p_sz+1) )
                  log_add("ptr.c", "i_insert", "T0 :: offset not in [0;*pz+1[");
          }
          return b;
      }
      
      int *i_extract( int *p, size_t p_sz, size_t i0, size_t i1, size_t *q_sz )
      {
          int *q = NULL;
          if ( p && p_sz >= i1-i0 ) //T0
          {
              *q_sz = i1-i0+1;
              if ( ( q = i_allocate(*q_sz) ) ) //T1
                  i_replace_partial( q, p, i0, i1+1, i0, 0, -1, 1 );
      
              else log_add("ptr.c", "i_insert", "T1 :: q is NULL");
          }
          else
          {
              if ( !p )
                  log_add("ptr.c", "i_insert", "T0 :: p is NULL");
      
              if ( !( p_sz >= i1-i0) )
                  log_add("ptr.c", "i_insert", "T0 :: pz < i1-i0 ");
          }
          return q;
      }
      
      bool i_split( int *p, size_t *p_sz, int **p1, size_t *p1_sz, int **p2, size_t *p2_sz, size_t offset )
      {
          bool b = false;
          if ( p && 0 <= offset && offset < *p_sz ) //T0
          {
              if ( ((*p1) = i_extract( p, *p_sz, 0, offset, p1_sz )) && ((*p2) = i_extract( p, *p_sz, offset+1, *p_sz-1, p2_sz )) )
                  b = true;
      
              else
              {
                  if ( !(*p1) )
                      log_add("ptr.c", "i_split", "T1 :: *p1 = NULL");
      
                  if ( !(*p2) )
                      log_add("ptr.c", "i_split", "T1 :: *p2 = NULL");
              }
          }
          else
          {
              if ( !(*p) )
                  log_add("ptr.c", "i_split", "T0 :: p = NULL");
      
              if ( !(0 <= offset && offset < *p_sz+1) )
                  log_add("ptr.c", "i_split", "T0 :: offset not in [0;*pz+1[");
          }
          return b;
      }
      
      int *i_merge( int *p0, size_t p0_sz, int *p1, size_t p1_sz)
      {
          int *p_out = NULL;
          if ( ( p_out = i_allocate( p0_sz + p1_sz )) ) //T0
          {
              if ( i_replace_partial(p_out, p0, 0, p0_sz, 0, 0, 1, 1) == false ) //T1
                  log_add("ptr.c", "i_merge", "T1 :: i_replace_partial returned NULL");
      
              if ( i_replace_partial(p_out, p1, p0_sz+1, p0_sz+p1_sz, 0, p0_sz, 1, -1) == false ) //T2
                  log_add("ptr.c", "i_merge", "T1 :: i_replace_partial returned NULL");
          }
          else log_add("ptr.c", "i_merge", "T0 :: p_out = NULL");
          return p_out;
      }
      
      int *i_copy( int *p, size_t p_sz )
      {
          int *q = NULL;
          if ( p && p_sz > 0 ) //T0
          {
              if ( ( q = i_allocate(p_sz) ) ) //T1
              {
                  if ( i_replace_partial( q, p, 0, p_sz, 0, 0, 1, 1) == false ) //T2
                      log_add("ptr.c", "i_copy", "T2 :: i_replace_partial returned NULL");
              }
              else log_add("ptr.c", "i_copy", "T1 :: q = NULL");
          }
          else
          {
              if ( !p )
                  log_add("ptr.c", "i_copy", "T0 :: p = NULL");
      
              if ( p_sz <= 0 )
                  log_add("ptr.c", "i_copy", "T0 :: p_sz <= 0");
          }
          return q;
      }
      
      bool i_remove( int **p, size_t *p_sz, size_t offset )
      {
          bool b = false;
          if ( *p && 0 <= offset && offset < *p_sz ) //T0
          {
              size_t q_sz = *p_sz; size_t index = 0;
              int *q = NULL;
              if ( ( q = i_copy( (*p), *p_sz ) ) ) //T1
              {
                  if ( i_realloc( p, p_sz, *p_sz-1 ) == true ) //T2
                  {
                      for ( size_t i=0 ; i<q_sz ; i++ )
                      {
                          if ( i != offset )
                              (*p)[index] = q[i], index++;
                      }
                      g_deallocate( q );
                      b = true;
                  }
                  else log_add("ptr.c", "i_remove", "T2 :: i_realloc returned false");
              }
              else log_add("ptr.c", "i_remove", "T1 :: q = NULL");
          }
          else
          {
              if ( !(*p) )
                  log_add("ptr.c", "i_remove", "T0 :: p = NULL");
      
              if ( !(0 <= offset && offset < *p_sz+1) )
                  log_add("ptr.c", "i_remove", "T0 :: offset not in [0;*pz+1[");
          }
          return b;
      }
      
      bool i_replace( int *p0, size_t p0_sz, int *p1, size_t p1_sz )
      {
          bool b = false;
          if ( p0 && p1 && p0_sz >= p1_sz ) //T0
          {
              if ( i_replace_partial( p0, p1, 0, p1_sz, 0, 0, 1, 1) == false ) //T1
                  log_add("ptr.c", "i_replace", "T1 :: i_replace returned NULL");
      
              else b = true;
          }
          else
          {
              if ( !p0 )
                  log_add("ptr.c", "i_replace", "T0 :: p0 = NULL");
      
              if ( !p1 )
                  log_add("ptr.c", "i_replace", "T0 :: p1 = NULL");
      
              if ( p0_sz < p1_sz )
                  log_add("ptr.c", "i_replace", "T0 :: p0_sz < p1_sz");
          }
          return b;
      }
      
      
      bool i_init( int *p, size_t p_sz, int v )
      {
          bool b = false;
          if ( *p && p_sz > 0 ) //T1
          {
              size_t i = 0;
              for ( i=0 ; i<p_sz ; i++ )
                  p[i] = v;
      
              b = true;
          }
          else
          {
              if ( !p )
                  log_add("ptr.c", "i_init", "T0 :: p = NULL");
      
              if ( p_sz <= 0 )
                  log_add("ptr.c", "i_init", "T0 :: p_sz <= 0");
          }
          return b;
      }
      
      bool i_replace_partial( int *p0 , int *p1, size_t i_for_start, size_t i_for_end, size_t i_p0, size_t i_p1, short p0_sign, short p1_sign )
      {
          bool b = false;
          if ( p0 && p1 ) //T0
          {
              for ( size_t i=i_for_start ; i<i_for_end ; i++ )
                  p0[i+p0_sign*i_p0] = p1[i+p1_sign*i_p1];
      
              b = true;
          }
          else
          {
              if ( !p0 )
                  log_add("ptr.c", "i_replace_partial", "T0 :: p0 = NULL");
      
              if ( !p1 )
                  log_add("ptr.c", "i_replace_partial", "T0 :: p1 = NULL");
          }
          return b;
      }
      
      int *i_rshift( int *p, size_t p_sz, size_t shift )
      {
          int *q = NULL;
          if ( p && p_sz>0 ) //T0
          {
              shift %= p_sz;
              if ( shift > 0 )
              {
                  int *r = NULL;
                  if ( ( r = i_allocate(p_sz + shift) ) ) //T1
                  {
                      if ( i_replace_partial( r, p, 0, p_sz, shift, 0, 1, 1) == true ) //T2
                      {
                          if ( ( q = i_allocate( p_sz ) ) ) //T3
                          {
                              if ( i_replace_partial( q, r, p_sz, p_sz+shift, p_sz, 0, -1, 1) == true ) //T4
                              {
                                  if ( i_replace_partial( q, r, shift, p_sz, 0, 0, 1, 1) == false ) //T5
                                      log_add("ptr.c", "i_rshift", "T5 :: i_replace_partial returned false");
                              }
                              else log_add("ptr.c", "i_rshift", "T4 :: i_replace_partial returned false");
                          }
                          else log_add("ptr.c", "i_rshift", "T3 :: q = NULL");
                          g_deallocate( r );
                      }
                      else log_add("ptr.c", "i_rshift", "T2 :: i_replace_partial returned false");
                  }
                  else log_add("ptr.c", "i_rshift", "T1 :: r = NULL");
              }
              else
              {
                  if ( !( q = i_copy( p, p_sz ) ) ) //T6
                      log_add("ptr.c", "i_rshift", "T6 :: q = NULL");
              }
          }
          else
          {
              if( !p )
                  log_add("ptr.c", "i_rshift", "T0 :: p0 = NULL");
      
              if( p_sz <= 0 )
                  log_add("ptr.c", "i_rshift", "T0 :: p_sz <= 0");
          }
          return q;
      }
      
      int *i_lshift( int *p, size_t p_sz, size_t shift )
      {
          int *q = NULL;
          if ( p && p_sz>0 ) //T0
          {
              shift %= p_sz;
              if ( !( q = i_rshift(p, p_sz, p_sz-shift) ) ) //T1
                  log_add("ptr.c", "i_lshift", "T1 :: q = NULL");
          }
          else
          {
              if ( !p )
                  log_add("ptr.c", "i_lshift", "T0 :: p = NULL");
      
              if ( p_sz == 0 )
                  log_add("ptr.c", "i_lshift", "T0 :: p_sz = 0");
          }
          return q;
      }
      
      bool i_rshift_self( int *p, size_t p_sz, size_t shift )
      {
          bool b = false;
          int *q = NULL;
          if ( ( q = i_rshift( p, p_sz, shift ) ) ) //T0
          {
              if ( i_replace_partial( p, q, 0, p_sz, 0, 0, 1, 1 ) == true ) //T1
                  b = true;
      
              else log_add("ptr.c", "i_rshift_self", "T0 :: p = NULL");
              g_deallocate(q);
          }
          else log_add("ptr.c", "i_rshift_self", "T0 :: q = NULL");
          return b;
      }
      
      bool i_lshift_self( int *p, size_t p_sz, size_t shift )
      {
          bool b = false;
          int *q = NULL;
          if ( ( q = i_lshift( p, p_sz, shift ) ) ) //T0
          {
              if ( i_replace_partial( p, q, 0, p_sz, 0, 0, 1, 1 ) == true ) //T1
                  b = true;
      
              else log_add("ptr.c", "i_lshift_self", "T1 :: p = NULL");
              g_deallocate( q );
          }
          else log_add("ptr.c", "i_lshift_self", "T0 :: q = NULL");
          return b;
      }
      
      int *i_sort( int *p, size_t p_sz, unsigned short order )
      {
          int *p_out = NULL;
          if ( p && p_sz > 0 ) //T0
          {
              if( ( p_out = i_copy(p, p_sz) ) ) //T1
              {
                  if ( order%2 == 0 )
                  {
                      for ( size_t i=0 ; i < p_sz ; i++ )
                      {
                          for ( size_t j=0 ; j < p_sz ; j++ )
                          {
                              if ( p_out[j] > p_out[i] )
                              {
                                  int x = p_out[j];
                                  p_out[j] = p_out[i];
                                  p_out[i] = x;
                              }
                          }
                      }
                  }
                  else
                  {
                      for ( size_t i=0 ; i < p_sz ; i++ )
                      {
                          for ( size_t j=0 ; j < p_sz ; j++ )
                          {
                              if ( p_out[j] < p_out[i] )
                              {
                                  int x = p_out[j];
                                  p_out[j] = p_out[i];
                                  p_out[i] = x;
                              }
                          }
                      }
                  }
              }
              else log_add("ptr.c", "i_sort", "T1 :: p_out = NULL");
          }
          else
          {
              if ( !p )
                  log_add("ptr.c", "i_sort", "T0 :: p = NULL");
      
              if ( p_sz == 0 )
                  log_add("ptr.c", "i_sort", "T0 :: p_sz = 0");
          }
          return p_out;
      }
      
      bool i_sort_self( int *p, size_t p_sz, unsigned short order )
      {
          bool b = false;
          if ( p && p_sz > 0 ) //T0
          {
              if ( order%2 == 0 )
              {
                  for ( size_t i=0 ; i < p_sz ; i++ )
                  {
                      for ( size_t j=0 ; j < p_sz ; j++ )
                      {
                          if ( p[j] > p[i] )
                          {
                              int x = p[j];
                              p[j] = p[i];
                              p[i] = x;
                          }
                      }
                  }
                  b = true;
              }
              else
              {
                  for ( size_t i=0 ; i < p_sz ; i++ )
                  {
                      for ( size_t j=0 ; j < p_sz ; j++ )
                      {
                          if ( p[j] < p[i] )
                          {
                              int x = p[j];
                              p[j] = p[i];
                              p[i] = x;
                          }
                      }
                  }
                  b = true;
              }
          }
          else
          {
              if ( !p )
                  log_add("ptr.c", "i_sort_self", "T0 :: p = NULL");
      
              if ( p_sz == 0 )
                  log_add("ptr.c", "i_sort_self", "T0 :: p_sz = 0");
          }
          return b;
      }
      
      bool sz_search_limit( size_t *p, size_t p_sz, size_t *v, unsigned short limit )
      {
          bool b = false;
          if ( p && p_sz > 0 ) //T0
          {
              if ( limit == 0 ) //T1
              {
                  *v = SIZE_MAX;
                  for ( size_t i = 0 ; i< p_sz ; i++ )
                      if ( p[i] <= *v )
                          *v = p[i];
      
                  b=true;
              }
              else if ( limit == 1 )
              {
                  *v = 0;
                  for ( size_t i = 0 ; i<p_sz ; i++ )
                      if ( p[i] >= *v )
                          *v = p[i];
      
                  b=true;
              }
              else log_add("ptr.c","sz_search_limit","T1 :: limit != [0;1]");
          }
          else
          {
              if ( !p )
                  log_add("ptr.c", "sz_search_limit", "T0 :: p = NULL");
      
              if ( p_sz == 0 )
                  log_add("ptr.c", "sz_search_limit", "T0 :: p_sz = 0");
          }
          return b;
      }
      
      bool i_search_limit( int *p, size_t p_sz, int *v, unsigned short limit )
      {
          bool b = false;
          if ( p && p_sz > 0 ) //T0
          {
              if ( limit == 0 ) //T1
              {
                  *v = INT_MAX;
                  for ( size_t i = 0 ; i< p_sz ; i++ )
                      if ( p[i] <= *v )
                          *v = p[i];
      
                  b=true;
              }
              else if ( limit == 1 )
              {
                  *v = INT_MIN;
                  for ( size_t i = 0 ; i<p_sz ; i++ )
                      if ( p[i] >= *v )
                          *v = p[i];
      
                  b=true;
              }
              else log_add("ptr.c","i_search_limit","T1 :: limit != [0;1]");
          }
          else
          {
              if ( !p )
                  log_add("ptr.c", "i_search_limit", "T0 :: p = NULL");
      
              if ( p_sz == 0 )
                  log_add("ptr.c", "i_search_limit", "T0 :: p_sz = 0");
          }
          return b;
      }
      
      double i_average( int *p, size_t p_sz )
      {
          double avg = 0.;
          if ( p && p_sz > 0 ) //T0
          {
              double sum = 0.;
              for ( size_t i = 0 ; i < p_sz ; i++ )
                  sum += (double)p[i];
      
              avg = 1.*sum/p_sz;
          }
          else
          {
              if ( !p )
                  log_add("ptr.c","i_average","T0 :: p is NULL");
      
              else if ( p_sz == 0 )
                  log_add("ptr.c","i_average","T0 :: p_sz = 0");
      
              else log_add("ptr.c","i_average","T0 :: unknown error");
          }
          return avg;
      }
      
      double i_variance( int *p, size_t p_sz )
      {
          double variance = 0.;
          if ( p && p_sz >0 ) //T0
          {
              double avg = i_average( p, p_sz );
              double sum = 0.;
              for ( size_t i = 0 ; i < p_sz ; i++ )
                  sum += (double)((p[i]-avg)*(p[i]-avg));
      
              variance = 1.*sum/(p_sz-1);
          }
          else
          {
              if ( !p )
                  log_add("ptr.c","i_variance","T0 :: p is NULL");
      
              else if ( p_sz == 0 )
                  log_add("ptr.c","i_variance","T0 :: p_sz = 0");
      
              else log_add("ptr.c","i_variance","T0 :: unknown error");
          }
          return variance;
      }
      
      double i_std_deviation( int* p, size_t p_sz )
      {
          double std_deviation = 0.;
          if ( p && p_sz > 0 ) //T0
              std_deviation = sqrt( i_variance( p, p_sz ) );
      
          else
          {
              if ( !p )
                  log_add("ptr.c","i_std_deviation","T0 :: p is NULL");
      
              else if ( p_sz == 0 )
                  log_add("ptr.c","i_std_deviation","T0 :: p_sz = 0");
      
              else log_add("ptr.c","i_std_deviation","T0 :: unknown error");
          }
          return 1.*std_deviation;
      }
      
      size_t *i_search( int *p, size_t p_sz, size_t *q_sz, int v )
      {
          size_t *q = NULL;
          if ( p && p_sz > 0 ) //T0
          {
              *q_sz = 0;
              for ( size_t i=0 ; i<p_sz ; i++ )
              {
                  if ( p[i] == v )
                  {
                      size_t *q_realloc = NULL;
                      if ( ( q_realloc = realloc( q, ++(*q_sz)*sizeof( *q ) ) ) ) //T1
                      {
                          q = q_realloc;
                          q[*q_sz-1] = i;
                      }
                      else
                      {
                          log_add("ptr.c","i_search","T1 :: realloc returned NULL");
                          g_deallocate(q);
                      }
                  }
              }
          }
          else
          {
              if ( !p )
                  log_add("ptr.c","i_search","T0 :: p is NULL");
      
              else if ( p_sz == 0 )
                  log_add("ptr.c","i_search","T0 :: p_sz = 0");
      
              else log_add("ptr.c","i_search","T0 :: unknown error");
          }
          return q;
      }
      
      void i_1D_display( i_1D_t *st )
      {
          if (st)
          {
              i_display(st->p, st->p_sz);
          }
      }
      
      void i_1D_destroy( i_1D_t *st )
      {
          if ( st )
          {
              if ( st->p ) { g_deallocate( st->p ), st->p = NULL; }
              free( st );
          }
      }
      
      i_1D_t *i_1D_create_null( void )
      {
          i_1D_t *st = NULL;
          if ( ( st = malloc( sizeof( *st ) ) ) )
          {
              st->p = NULL;
              st->p_sz = 0;
              st->d_sz = 0;
          }
          return st;
      }
      
      i_1D_t *i_1D_create( size_t x )
      {
          i_1D_t *st = NULL;
          if ( ( st = malloc( sizeof( *st ) ) ) )
          {
              if ( ( st->p = i_allocate( x ) ) )
              {
                  st->p_sz = x;
                  st->d_sz = 0;
              }
          }
          return st;
      }
      
      i_1D_t *i_1D_create_random( size_t x, int bound, int sign )
      {
          i_1D_t *st = NULL;
          if ( ( st = malloc( sizeof( *st ) ) ) )
          {
              if ( ( st->p = i_allocate_random( x, bound, sign ) ) )
              {
                  st->p_sz = x;
                  st->d_sz = 0;
              }
          }
          return st;
      }
      
      bool i_1D_realloc( i_1D_t **st, size_t new_sz )
      {
          return i_realloc( &(*st)->p, &(*st)->p_sz, new_sz );
      }
      
      bool i_1D_insert( i_1D_t **st, size_t offset, int v )
      {
          return i_insert( &(*st)->p, &(*st)->p_sz, offset, v );
      }
      
      bool i_1D_replace_partial ( i_1D_t *st0, i_1D_t *st1, size_t i_for_start, size_t i_for_end, size_t i_p0, size_t i_p1, short p0_sign, short p1_sign )
      {
          return i_replace_partial( st0->p, st1->p, i_for_start, i_for_end, i_p0, i_p1, p0_sign, p1_sign );
      }
      
      i_1D_t *i_1D_extract( i_1D_t *st, size_t i0, size_t i1 )
      {
          i_1D_t *st_out = NULL;
          if ( ( st_out = i_1D_create_null() ) )
          {
              size_t sz = 0;
              if ( ( st_out->p = i_extract( st->p, st->p_sz, i0, i1, &sz ) ) )
              {
                  st_out->p_sz = sz;
                  st_out->d_sz = sz;
              }
          }
          return st_out;
      }
      
      bool i_1D_split( i_1D_t *st0, i_1D_t **st1, i_1D_t **st2, size_t offset )
      {
          bool b = false;
          if ( ( (*st1) = i_1D_create_null() ) && ( (*st2) = i_1D_create_null() ) )
              b = i_split( st0->p, &st0->p_sz, &(*st1)->p, &(*st1)->p_sz, &(*st2)->p, &(*st2)->p_sz, offset );
      
          return b;
      }
      
      i_1D_t *i_1D_merge( i_1D_t *st0, i_1D_t *st1 )
      {
          i_1D_t *st_out = NULL;
          if ( st0 && st1 )
          {
              if ( ( st_out = i_1D_create_null() ) )
              {
                  if ( ( st_out->p = i_merge( st0->p, st0->p_sz, st1->p, st1->p_sz ) ) )
                  {
                      st_out->p_sz = st0->p_sz + st1->p_sz ;
                      st_out->d_sz = st_out->p_sz;
                  }
                  else i_1D_destroy( st_out );
              }
          }
          return st_out;
      }
      
      i_1D_t *i_1D_copy( i_1D_t *st )
      {
          i_1D_t *st_out = NULL;
          if ( ( st_out = i_1D_create(st->p_sz) ) )
              i_1D_replace_partial( st_out, st, 0, st->p_sz, 0, 0, 1, 1);
      
          return st_out;
      }
      
      bool i_1D_remove( i_1D_t **st, size_t offset )
      {
          return i_remove( &(*st)->p, &(*st)->p_sz, offset );
      }
      
      bool i_1D_replace( i_1D_t *st0, i_1D_t *st1 )
      {
          return i_replace( st0->p, st0->p_sz, st1->p, st1->p_sz );
      }
      
      bool i_1D_init( i_1D_t *st, int v )
      {
          return i_init( st->p, st->p_sz, v );
      }
      
      i_1D_t *i_1D_rshift( i_1D_t *st, size_t shift )
      {
          i_1D_t *st_out = NULL;
          if ( ( st_out = i_1D_create_null() ) )
          {
              if ( (st_out->p = i_rshift( st->p, st->p_sz, shift ) ) )
              {
                  st_out->p_sz = st->p_sz;
                  st_out->d_sz = st->d_sz;
              }
          }
          return st_out;
      }
      
      i_1D_t *i_1D_lshift( i_1D_t *st, size_t shift )
      {
          i_1D_t *st_out = NULL;
          if ( ( st_out = i_1D_create_null() ) )
          {
              if ( ( st_out->p = i_lshift( st->p, st->p_sz, shift ) ) )
              {
                  st_out->p_sz = st->p_sz;
                  st_out->d_sz = st->d_sz;
              }
          }
          return st_out;
      }
      
      bool i_1D_rshift_self( i_1D_t *st, size_t shift )
      {
          return i_rshift_self( st->p, st->p_sz, shift );
      }
      
      bool i_1D_lshift_self( i_1D_t *st, size_t shift )
      {
          return i_lshift_self( st->p, st->p_sz, shift );
      }
      
      i_1D_t *i_1D_sort( i_1D_t *st, unsigned short order )
      {
          i_1D_t *st_out = NULL;
          if ( ( st_out = i_1D_create_null() ) )
          {
              if ( ( st_out->p = i_sort( st->p, st->p_sz, order ) ) )
              {
                  st_out->p_sz = st->p_sz;
                  st_out->d_sz = st->d_sz;
              }
          }
          return st_out;
      }
      
      bool i_1D_sort_self( i_1D_t *st, unsigned short order )
      {
          bool b = false;
          if ( st )
              b = i_sort_self( st->p, st->p_sz, order );
      
          return b;
      }
      
      void i_2D_display( int **p, size_t x, size_t *y )
      {
          printf("\n x=%zu", x);
          if ( p && x > 0 && y != NULL )
          {
              unsigned int digit = 0;
              unsigned int digitInt = log10(pow(2,8*sizeof(*p    )))+1;
              unsigned int digitSiz = log10(pow(2,8*sizeof(size_t)))+1;
              if ( digitInt < digitSiz )
                  digit = digitSiz;
              else  digit = digitInt;
      
              size_t max = 0;
              sz_search_limit( y, x, &max, 1 );
      
              //Displaying rows
              printf("\n%*s", digit+2, " ");
              for ( size_t i=0 ; i<max ; i++ )
                  printf("%*zu", digit+2, i);
      
              // Displaying separator
              printf("\n%*s", digit+2 ,"");
              for ( size_t i=0 ; i<max ; i++ )
                  printf("%*s", digit+2 ,"-");
      
              // Displaying data
              for ( size_t i=0 ; i<x ; i++ )
              {
                  printf("\n%*zu -", digit+1, i);
                  for ( size_t j=0 ; j<y[i] ; j++ )
                      printf("%*d ", digit+1, p[i][j]);
              }
          }
      }
      
      void i_2D_deallocate( int **p, size_t x )
      {
          if (p)
          {
              size_t i;
              for ( i=0 ; i<x ; i++ )
                  free(p[i]), p[i]=NULL;
      
              free(p), p=NULL;
          }
      }
      
      int **i_2D_allocate( size_t x, size_t **y )
      {
          int **p = NULL;
          if ( x > 0 )
          {
              if ( ( p = malloc( x*sizeof( *p ) ) ) )
              {
                  if ( (*y) != NULL )
                  {
                      for ( size_t i=0 ; i<x ; i++ )
                      {
                          if ( ( p[i] = i_allocate( (*y)[i] ) ) )
                              for ( size_t j=0 ; j<(*y)[i] ; j++ )
                                  p[i][j] = 0;
      
                          else i_2D_deallocate( p, i-1 ), i=x;
                      }
                  }
                  else
                  {
                      if ( ( (*y) = malloc( x*sizeof( size_t ) ) ) )
                          for ( size_t i=0 ; i<x ; i++ )
                              p[i] = NULL, (*y)[i]=0;
                  }
              }
          }
          return p;
      }
      
      int **i_2D_allocate_random( size_t x, size_t *y, int bound, int sign )
      {
          int **p = NULL;
          srand(time(NULL));
          if ( x > 0 )
          {
              if ( ( p = malloc( x*sizeof( **p ) ) ) )
              {
                  if ( y > 0 )
                      for ( size_t i=0 ; i<x ; i++ )
                      {
                          if ( ( p[i] = i_allocate( y[i] ) ) )
                              for ( size_t j=0 ; j<y[i] ; j++ )
                              {
                                  int r, q = sign;
                                  if ( sign == 2 )
                                      q = rand()%2;
      
                                  r = pow ( -1., q%2 );
                                  p[i][j] = r*rand()%bound;
                              }
      
                          else i_2D_deallocate( p, i-1 ), i=x;
                      }
      
                  else
                      for ( size_t i=0 ; i<x ; i++ )
                          p[i] = NULL;
              }
          }
          return p;
      }
      
      void i_2D_line_add_( int ***p, size_t *p_sz, size_t p_sz_new, size_t **y )
      {
          int **p_realloc = NULL;
          if ( ( p_realloc = realloc( (*p), p_sz_new*sizeof( **p ) ) ) )
          {
              *p = p_realloc;
              for ( size_t i=*p_sz ; i<p_sz_new ; i++ )
                   (*p)[i] = NULL;
      
              size_t *y_realloc = NULL;
              if ( ( y_realloc = realloc( (*y), p_sz_new*sizeof( *y ) ) ) )
              {
                  (*y) = y_realloc;
                  for ( size_t i=*p_sz; i<p_sz_new ; i++ )
                      (*y)[i] = 0;
      
                  *p_sz = p_sz_new;
              }
          }
      }
      
      void i_2D_row_add_( int ***p, size_t x_index, size_t Y, size_t *y )
      {
          int *p_realloc = NULL;
          if ( ( p_realloc = i_allocate( Y ) ) ) //T0
          {
              (*p)[x_index] = p_realloc;
                 y[x_index] = Y;
          }
          else log_add( "ptr.c", "i_2D_add_row", "T0 :: i_allocate returned NULL" );
      }

       "PTR.H"

      #ifndef PTR_H
      #define PTR_H
      
      #include <stdbool.h>
      #include <stdio.h>
      #include <stdlib.h>
      #include <limits.h>
      #include <time.h>
      #include <math.h>
      #include <string.h>
      #include "log.h"
      
      typedef struct
      {
         int *p;
         size_t p_sz;
         size_t d_sz;
      }i_1D_t;
      
      void  g_deallocate      ( void *p );
      
      void  i_display         ( int *p, size_t p_sz );
      int  *i_allocate        ( size_t p_sz );
      int  *i_allocate_random ( size_t p_sz, int bound, int sign );
      bool  i_realloc         ( int **p, size_t *p_sz, size_t new_sz );
      bool  i_insert          ( int **p, size_t *p_sz, size_t offset, int v );
      bool  i_replace_partial ( int *p0, int *p1, size_t i_for_start, size_t i_for_end, size_t i_p0, size_t i_p1, short p0_sign, short p1_sign );
      int  *i_extract         ( int *p, size_t p_sz, size_t i0, size_t i1, size_t *q_sz );
      bool  i_split           ( int *p, size_t *p_sz, int **p1, size_t *p1_sz, int **p2, size_t *p2_sz, size_t offset );
      int  *i_merge           ( int *p0, size_t p0_sz, int *p1, size_t p1_sz);
      bool  i_remove          ( int **p, size_t *p_sz, size_t offset );
      int  *i_copy            ( int *p, size_t p_sz );
      bool  i_replace         ( int *p0, size_t p0_sz, int *p1, size_t p1_sz );
      bool  i_init            ( int *p, size_t p_sz, int v );
      int  *i_rshift          ( int *p, size_t p_sz, size_t shift );
      int  *i_lshift          ( int *p, size_t p_sz, size_t shift );
      bool  i_rshift_self     ( int *p, size_t p_sz, size_t shift );
      bool  i_lshift_self     ( int *p, size_t p_sz, size_t shift );
      int  *i_sort            ( int *p, size_t p_sz, unsigned short order );
      bool  i_sort_self       ( int *p, size_t p_sz, unsigned short order );
      bool i_search_limit     ( int *p, size_t p_sz, int *v, unsigned short limit );
      double i_average        ( int *p, size_t p_sz );
      double i_variance       ( int *p, size_t p_sz );
      double i_std_deviation  ( int *p, size_t p_sz );
      
      
      void    i_1D_display         ( i_1D_t *st );
      void    i_1D_destroy         ( i_1D_t *st );
      i_1D_t *i_1D_create_null     ( void );
      i_1D_t *i_1D_create          ( size_t x );
      i_1D_t *i_1D_create_random   ( size_t x, int bound, int sign );
      bool    i_1D_realloc         ( i_1D_t **st, size_t new_sz );
      bool    i_1D_insert          ( i_1D_t **st, size_t offset, int v );
      bool    i_1D_replace_partial ( i_1D_t *st1, i_1D_t *st2, size_t i_for_start, size_t i_for_end, size_t i_p0, size_t i_p1, short p0_sign, short p1_sign );
      i_1D_t *i_1D_extract         ( i_1D_t *st, size_t i0, size_t i1 );
      bool    i_1D_split           ( i_1D_t *st0, i_1D_t **st1, i_1D_t **st2, size_t offset );
      i_1D_t *i_1D_merge           ( i_1D_t *st0, i_1D_t *st1 );
      i_1D_t *i_1D_copy            ( i_1D_t *st );
      bool    i_1D_remove          ( i_1D_t **st, size_t offset );
      bool    i_1D_replace         ( i_1D_t *st0, i_1D_t *st1 );
      bool    i_1D_init            ( i_1D_t *st, int v );
      i_1D_t *i_1D_rshift          ( i_1D_t *st, size_t shift );
      i_1D_t *i_1D_lshift          ( i_1D_t *st, size_t shift );
      bool i_1D_rshift_self        ( i_1D_t *st, size_t shift );
      bool i_1D_lshift_self        ( i_1D_t *st, size_t shift );
      i_1D_t *i_1D_sort            ( i_1D_t *st, unsigned short order );
      bool i_1D_sort_self          ( i_1D_t *st, unsigned short order );
      
      /*2D*********************************************************************/
      
      typedef struct
      {
          int **da;
          size_t x;
          size_t *y;
      }i_2D_t;
      
      void  i_2D_display    ( int **p, size_t x, size_t *y );
      void  i_2D_deallocate ( int **p, size_t x );
      int **i_2D_allocate   ( size_t x, size_t **y );
      void  i_2D_line_add_  ( int ***p, size_t *p_sz, size_t p_sz_new, size_t **y );
      void  i_2D_row_add_   ( int ***p, size_t x_index, size_t Y, size_t *y );
      int **i_allocate_2D_random( size_t x, size_t y );
      
      #endif
      


      "LOG.C"

      #include <stdio.h>
      #include <stdlib.h>
      #include <stdbool.h>
      #include "log.h"
      
      log_t *f_log;
      
      static size_t str_length( char *p )
      {
          size_t sz = 0;
          if ( p )
              while ( *p != '\0' )
                  p++, sz++;
      
          return sz;
      }
      
      static char *str_copy( char* p )
      {
          char *p_out = NULL;
          if ( p )
          {
              size_t p_sz = 0;
              if ( ( p_sz = str_length(p) ) > 0 )
              {
                  p_out = malloc( (p_sz+1)*sizeof(*p_out) );
      
                  size_t i;
                  for ( i = 0 ; i< p_sz ; i++ )
                      p_out[i] = p[i];
      
                  p_out[i]='\0';
              }
          }
          return p_out;
      }
      
      log_t *log_create_null( void )
      {
          log_t *p_log = malloc( sizeof(*p_log) );
          if ( p_log )
          {
              p_log->hea = NULL;
              p_log->fun = NULL;
              p_log->msg = NULL;
              p_log->sz = 1;
          }
          return p_log;
      }
      
      void log_destroy( log_t *p_log )
      {
          if ( p_log )
          {
              size_t i;
              if ( p_log )
              {
                  for ( i=0 ; i<p_log->sz ; i++ )
                  {
                      if ( p_log->hea )
                          free( p_log->hea[i] );
      
                      if ( p_log->fun )
                          free( p_log->fun[i] );
      
                      if ( p_log->msg )
                          free( p_log->msg[i] );
                  }
                  free(p_log->hea);
                  free(p_log->fun);
                  free(p_log->msg);
              }
              free(p_log);
          }
      }
      
      bool log_add( char *hea, char *fun, char *msg )
      {
          bool c = false;
          if ( hea && fun && msg )
          {
              log_t *a = NULL; //Testing pointer for the reallocation of f_log
              if ( f_log )
              {
                  a = f_log;
                  size_t sz_new = a->sz;
                  char **b = NULL; //Testing pointer for reallocation
                  if ( ( b = realloc( (a->hea), sz_new*sizeof(char*) ) ) )
                  {
                      a->hea = b;
                      if ( ( a->hea[sz_new-1] = str_copy(hea) ) )
                      {
                          if ( ( b = realloc( (a->fun), sz_new*sizeof(char*) ) ) )
                          {
                              a->fun = b;
                              if ( ( a->fun[sz_new-1] = str_copy(fun) ) )
                              {
                                  if ( ( b = realloc( (a->msg), sz_new*sizeof(char*) ) ) )
                                  {
                                      a->msg = b;
                                      if ( ( a->msg[sz_new-1] = str_copy(msg) ) )
                                      {
                                          (a->sz)++;
                                          c = true;
                                          f_log = a;
                                      }
                                  }
                                  else log_destroy(a);
                              }
                              else log_destroy(a);
                          }
                          else log_destroy(a);
                      }
                      else log_destroy(a);
                  }
                  else log_destroy(a);
              }
              else log_destroy(a);
          }
          return c;
      }
      
      void log_display( log_t *p )
      {
          if ( p )
          {
              size_t i;
              for ( i=0 ; i<p->sz-1 ; i++ )
                  printf("\n%20s - %20s - %s", p->hea[i], p->fun[i], p->msg[i]);
          }
      }
      
      

      "LOG.H"

      #ifndef LOG_H
      #define LOG_H
      
      typedef struct
      {
          char **hea;
          char **fun;
          char **msg;
          size_t sz;
      }log_t;
      
      extern log_t *f_log;
      
      log_t *log_create_null( void );
        bool log_add        ( char *hea, char *fun, char *msg );
        void log_display    ( log_t *p );
        void log_destroy    ( log_t *p_log );
      
      #endif
      
      






      • Partager sur Facebook
      • Partager sur Twitter
        20 avril 2019 à 13:30:27

        Merci pour votre réponse, cela m'aide beaucoup:D
        • Partager sur Facebook
        • Partager sur Twitter
          21 avril 2019 à 18:27:56

          > ça ne marche pas

          Quand on dit "ce que j'ai fait ne marche pas" et qu'on veut des explications de pourquoi comment, il vaut mieux montrer le code, histoire qu'on puisse diagnostiquer ce qui ne va pas, et proposer des corrections.

          Techniquement, quand on a fait un fichier truc.h pour y mettre des déclarations,   on met des #include "truc.h" dans les fichiers qui ont besoin de ces déclarations.

          Pour ce qui est du découpage, en C on procède généralement par "modules", c'est à dire qu'on fait un source c avec un ensemble de fonctions qui font des choses à peu près cohérentes (par exemple les fonctions qui servent à ranger/récupérer les données d'une appli sur un fichier).  Un autre module pourrait s'occuper de l'interface de dialogue avec l'utilisateur.

          Le fichier .h correspondant contient les déclarations de types, de constantes, et les prototypes de fonctions du module.

          Mais en C, le langage ne pousse pas à faire ce genre de découpage, donc souvent chez les débutants c'est le foutoir.

          En C++, c'est plus simple, la notion de classe suggère un découpage naturel (qu'on n'est pas obligé de respecter).

          -
          Edité par michelbillaud 21 avril 2019 à 18:29:06

          • Partager sur Facebook
          • Partager sur Twitter

          problème fichier .h et .c pour structures

          × Après avoir cliqué sur "Répondre" vous serez invité à vous connecter pour que votre message soit publié.
          × Attention, ce sujet est très ancien. Le déterrer n'est pas forcément approprié. Nous te conseillons de créer un nouveau sujet pour poser ta question.
          • Editeur
          • Markdown