如何使用宏参数声明变量

时间:2019-03-21 21:46:50

标签: c

我为宏提供了一些描述,我必须定义它们。

DECL_LIST(类型,名称)

扩展为可调整大小的数组实现中使用的三个变量的声明语法, name_len,name_cap和名称(指针本身)。

CHECK_CAP(类型,名称) 扩展到代码以确保可调整大小的数组具有足够的容量来容纳另一个元素,并 如果没有,则将其放大。

•SIZE(name)

扩展为当前存储在数组中的元素数的表达式。

•FOR(var,limit)

扩展到for循环的第一行,该循环使用名为var的int变量从0迭代到(但不 包括)限制。

•SWAP(类型,a,b)

扩展为一段代码,该代码块交换给定类型的两个变量a和b的值。我们需要 类型参数,以便声明一个有助于交换值的临时变量。

此排序功能的逻辑已经存在。我只需要在操纵函数的宏中使用相同的逻辑:

// Put your macro definitions here.  That should be all
// you need to do to complete this exercise.
#define DECL_LIST(type, name) \
    int name ## _cap = 5;     \
    int name ## _len = 0;     \
    type *name = (type *) malloc(name ## _cap * sizeof(type)); 

#define CHECK_CAP(type, name)                                         \
if ( name ## _len >= name ## _cap ) {                                 \
name ## _cap *= 2;                                                    \
name = (type *) realloc(name, name ## _cap * sizeof(type)); \
}  

#define SIZE(name)    \
sizeof(name)/sizeof(name[0]) 


#define FOR(var, limit) { \
  int var = 0;  \
for( int i = 0; i < limit; i++) {          \
       var++;           \
}    \
}

#define SWAP(type, a, b)   \
type temp = a; \
   a = b; \
   b = temp; 


int main()
{
  // Make a resizable list.
  DECL_LIST( double, list );

  double val;
  while ( scanf( "%lf", &val ) == 1 ) {
    // Grow the list when needed.
    CHECK_CAP( double, list );

    // Add this item to the list
    list[ SIZE( list ) ] = val;
    SIZE( list ) += 1;
  }

  // Bubble-sort the list.
  FOR( i, SIZE( list ) )
    FOR( j, SIZE( list ) - i - 1 )
      if ( list[ j ] > list[ j + 1 ] )
        SWAP( double, list[ j ], list[ j + 1 ] );

  // Print out the resulting, sorted list, one value per line.
  FOR( i, SIZE( list ) )
    printf( "%.2f\n", list[ i ] );

  return EXIT_SUCCESS;
}

这是转换之前的原始主要功能:

#include <stdio.h>
#include <stdlib.h>
int main()
{
// Make a resizable list.
int list_cap = 5;
int list_len = 0;
double *list = (double *) malloc( list_cap * sizeof( double ) );
double val;
while ( scanf( "%lf", &val ) == 1 ) {
// Grow the list when needed.
if ( list_len >= list_cap ) {
list_cap *= 2;
list = (double *) realloc( list, list_cap * sizeof( double ) );
}
// Add this item to the list
list[ list_len ] =val;
list_len++;
}
// Bubble-sort the list.
for ( int i = 0; i < list_len; i++ )
for ( int j = 0; j < list_len - i - 1; j++ )
if ( list[ j ] > list[ j + 1 ] ) {
double tmp = list[ j ];
list[ j ] = list[ j + 1 ];
list[ j + 1 ] = tmp;
}
// Print out the resulting, sorted list, one value per line.
for ( int i = 0; i < list_len; i++ )
printf( "%.2f\n", list[ i ] );
return EXIT_SUCCESS;
}

这是进行一些建议更改后的编译器输出:

error: lvalue required as left operand of assignment
     SIZE( list ) += 1;
                  ^
sortList.c:50:8: warning: unused variable ‘i’ [-Wunused-variable]
   FOR( i, SIZE( list ) )
        ^
sortList.c:22:7: note: in definition of macro ‘FOR’
   int var = 0;  \
       ^
sortList.c:52:18: error: ‘j’ undeclared (first use in this function)
       if ( list[ j ] > list[ j + 1 ] )
                  ^
sortList.c:52:18: note: each undeclared identifier is reported only once for each function it appears in
sortList.c:53:15: error: expected expression before ‘double’
         SWAP( double, list[ j ], list[ j + 1 ] );
               ^
sortList.c:29:1: note: in definition of macro ‘SWAP’
 type temp = a; \
 ^
sortList.c:31:8: error: ‘temp’ undeclared (first use in this function)
    b = temp; 
        ^
sortList.c:53:9: note: in expansion of macro ‘SWAP’
         SWAP( double, list[ j ], list[ j + 1 ] );
         ^
sortList.c:56:8: warning: unused variable ‘i’ [-Wunused-variable]
   FOR( i, SIZE( list ) )
        ^
sortList.c:22:7: note: in definition of macro ‘FOR’
   int var = 0;  \
       ^
sortList.c:57:29: error: ‘i’ undeclared (first use in this function)
     printf( "%.2f\n", list[ i ] );

如果我当前的宏正确,有什么建议吗?我对描述有些困惑。

1 个答案:

答案 0 :(得分:2)

您的老师希望您使用C preprocessor token pasting创建一个替换代码的宏。例如,第一个宏看起来像这样

#define DECL_LIST(type, name) \
    int name ## _cap = 5; \
    int name ## _len = 0; \
    type *name = (type *) malloc(name ## _cap * sizeof(type));

并将替换此代码

// Make a resizable list.
int list_cap = 5;
int list_len = 0;
double *list = (double *) malloc( list_cap * sizeof( double ) );

关于您对CHECK_CAP宏的疑问,您的老师希望您用宏替换此代码

// Grow the list when needed.
if ( list_len >= list_cap ) {
    list_cap *= 2;
    list = (double *) realloc( list, list_cap * sizeof( double ) );
}

但请保留此代码

// Add this item to the list
list[ list_len ] =val;
list_len++;

稍后,您可以将list_len替换为SIZE宏。

希望这将帮助您理解为什么宏不需要修改list_len

FOR应该是

#define FOR(var, limit) \
    for (int var = 0; var < limit; var++)

您需要在SWAP周围使用大括号,因为它会扩展为多行:

  if ( list[ j ] > list[ j + 1 ] ) {
     SWAP( double, list[ j ], list[ j + 1 ] );
  }