程序不会打印所需的字符

时间:2016-02-26 10:09:24

标签: c

我正在尝试打印矩形。如果我使用print语句,它可以工作但不打印。如果有人可以在仪式方向指导我。谢谢你的帮助

#include<stdio.h>
#include<stdlib.h>


char * drawRectangle(unsigned int height, unsigned int width){
    int row, col;
    char *myArray = malloc(100);

    int i = 0;

    while(myArray[i] != '\0'){
        for(row = 0; row<height; row++){
            printf("\n");
            for(col = 0; col < width; col++){
                if( row ==0 || row == height-1 || col == 0 || col == width-1){
                    //printf("*");
                    myArray[i++] = "*";
                }
                else{
                    //printf(" ");
                    myArray[i++] = " ";
                }
            }
        }
        i++;
    }
    return myArray;
}

int main() {
   char *c = drawRectangle(3,3);
   printf("%c", c);
   return (0);
}

这是该程序的链接

https://ideone.com/WxJuwx

2 个答案:

答案 0 :(得分:6)

  • 删除无用并调用未定义的行为循环。
  • 使用%s而不是%c,通过printf()打印字符串。
  • 不要忘记free()通过malloc()分配的内容。
  • 检查malloc()是否成功。
  • 将字符(而非指针)分配给myArray
  • 终止通过添加空字符创建的字符串。

更正后的代码:

#include<stdio.h>
#include<stdlib.h>

char * drawRectangle(unsigned int height, unsigned int width){
    unsigned int row, col;
    char *myArray = malloc(100); /* you should calculate the size of required buffer and use it here */

    int i = 0;

    if (myArray == NULL) return NULL;

    for(row = 0; row<height; row++){
        //printf("\n");
        myArray[i++] = '\n';
        for(col = 0; col < width; col++){
            if( row ==0 || row == height-1 || col == 0 || col == width-1){
                //printf("*");
                myArray[i++] = '*';
            }
            else{
                //printf(" ");
                myArray[i++] = ' ';
            }
        }
    }
    myArray[i] = '\0';
    return myArray;
}

int main(void) {
    char *c = drawRectangle(3,3);
    if (c != NULL) printf("%s", c);
    free(c);
    return (0);
}

答案 1 :(得分:3)

直接的方法可以采用以下方式

#include <stdio.h>

void drawRectangle( unsigned int height, unsigned int width, char c )
{
    for ( unsigned int i = 0; i < height; i++ )
    {
        for ( unsigned int j = 0; j < width; j++ )
        {
            int blank =  i > 0 && i < height - 1 && j > 0 && j < width - 1; 
            printf( "%c",  blank ? ' ' : c );
        }
        printf( "\n" );
    }
}

int main( void ) 
{
    drawRectangle( 3, 3, '*' );
}            

程序输出

***
* *
***

如果需要创建相应的数组,那么程序可能看起来像

#include <stdlib.h>
#include <stdio.h>

char ** drawRectangle( unsigned int height, unsigned int width, char c )
{
    char **rectangle = malloc( height * sizeof( char * ) );

    for ( unsigned int i = 0; i < height; i++ )
    {
        rectangle[i] = malloc( width * sizeof( char ) );
        for ( unsigned int j = 0; j < width; j++ )
        {
            int blank =  i > 0 && i < height - 1 && j > 0 && j < width - 1; 
            rectangle[i][j] = blank ? ' ' : c;
        }
    }

    return rectangle;
}

int main( void ) 
{
    unsigned int n = 3;
    char **rectangle = drawRectangle( n, n, '*' );

    for ( unsigned int i = 0; i < n; i++ )
    {
        for ( unsigned int j = 0; j < n; j++ ) printf( "%c", rectangle[i][j] );
        printf( "\n" );
    }

    for ( unsigned int i = 0; i < n; i++ ) free( rectangle[i] );
    free( rectangle );
}            

其输出与上述相同

***
* *
***

另一种方法是创建一个将(或不会)包含新行字符的字符串数组。

或者创建一个包含带有嵌入换行符的字符串的字符数组。

例如

#include <stdlib.h>
#include <stdio.h>

char * drawRectangle( unsigned int height, unsigned int width, char c )
{
    char *rectangle = malloc( height * ( width + 1 ) + 1 );

    unsigned int k = 0;
    for ( unsigned int i = 0; i < height; i++ )
    {
        for ( unsigned j = 0; j < width; j++ )
        {
            int blank =  i > 0 && i < height - 1 && j > 0 && j < width - 1; 
            rectangle[k++] = blank ? ' ' : c;
        }
        rectangle[k++] = '\n';
    }

    rectangle[k] = '\0';

    return rectangle;
}

int main( void ) 
{
    unsigned int n = 3;
    char *rectangle = drawRectangle( n, n, '*' );

    puts( rectangle );

    free( rectangle );
}