繁体   English   中英

字符串中的二维数组在C中创建错误输出

[英]2-D array in string creating erroneous output in C

我正在尝试做一些简单的事情,如打印字符串的反向。 示例:

Hello World! This is me

需要的O / P:

me is This World! Hello

我的代码是这样的:

#include<stdio.h>
#include<string.h>

int main(){
 char *arr[20] ;
 int i,j;
 int size;
 char *revarr[20];
 printf(" enter the number of words\n");
 scanf("%d",&size);
 for(i=0;i<size;i++)
 scanf("%s",&arr[i]);
 for(i=0;i<size;i++)
 {

    printf("%s\n",&arr[size-1-i]); //overwritten words
    revarr[i]=arr[size-1-i];
 }
 printf(" the reversed sentence is %s\n",(char *)revarr);
}

我除了arr [0],arr [1]等是单独的实体,但在打印和存储它们时它们似乎重叠如下:i / p:

Hello World

O / P:

World
HellWorld
the reversed sentence is WorlHell@#$

我似乎无法弄清楚出了什么问题! 提前致谢!

编辑:打印时

printf(&arr[0]);
printf(&arr[1]);

我明白了:

HellWorld
World

我期望它打印的是

Hello
World

你将arrrevarr声明为char指针数组。 您需要为其元素动态分配内存。
另请注意,您不需要& in语句

scanf("%s",&arr[i]);  

printf("%s\n", &arr[size-1-i]);  
//             ^No need of &  

这是您的代码的修改版本。 请注意,不需要使用revarr来反转字符串。

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

int main(){
    size_t i, size;
    printf("Enter the number of words\n");
    scanf("%d", &size);
    char *arr[size] ;  // Variable length array. Supported by C99 and latter

    for(i = 0; i < size; i++) 
    {
        arr[i] = malloc(20); // Assumimg words are no longer than 20 characters
        scanf("%s", arr[i]);
    }

    printf("The reversed sentence is:\n");  
    for(i = size-1; i >= 0; i--)  // Run loop in reverse order and print words
        printf("%s ", arr[i]); 
}

在使用它们读取字符串之前arr[0], arr[1],你没有为arr[0], arr[1],等分配内存

scanf("%s",&arr[i]);

这是未定义行为的原因。 你需要这样的东西:

int main(){
   char *arr[20] ;
   int i,j;
   int size;
   char *revarr[20];
   printf(" enter the number of words\n");
   scanf("%d",&size);
   for(i=0;i<size;i++)
   {
      // Allocate memory.
      // make it large enough to hold the input
      arr[i] = malloc(100);
      scanf("%s", arr[i]);
   }
   for(i=0;i<size;i++)
   {
      revarr[i]=arr[size-1-i];
   }

   printf(" the reversed sentence is: ");
   for(i=0;i<size;i++)
   {
       printf("%s ", revarr[i]);
   }
   printf("\n");


   // Deallocate the memory.
   for(i=0;i<size;i++)
   {
      free(arr[i]);
   }

   return 0;
}

这是解决问题的好方法:

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

int main(int argc, char *argv[])
{
     /* the string line will contain the line of the  input */
     char line[100];
     /* read the string with the function f gets */
     fgets(line,100,stdin);
   /* the tab will contain all the string of the variable  line */
     char *tab[20];
     /* the variable p will point to each string of the  line */
     char *p=NULL;
     /* we extract the strings of the line via the function strtok */
     p=strtok(line," ");
     int nb=-1;
     while (p!=NULL)
     {
         nb++;
         /* we allocate a space memory fo every str ing  */
            tab[nb]=malloc(sizeof(char)*100);
            strcpy(tab[nb],p);
            p=strtok(NULL," ");
     }
     /* there is an exception with the last string of the line we need to take care o f it */
     tab[nb][strlen(tab[nb])-1]='\0';
     int i;
     /* print the strings in reverse or der  */
     for (i=nb;i>=0;i--)
     {
         printf("%s ",tab[i]);
        /* dont forget to free the space memory at the end of the prog ram  */
         free(tab[i]);
     }
     printf("\n");

     return 0;
}

您需要以下内容

#include <stdio.h>
#include <string.h>

int main(void) 
{
    size_t size;

    printf( "enter the number of words: " );
    scanf( "%zu", &size );

    char arr[size][20];
    char revarr[size][20];

    for ( size_t i = 0; i < size; i++ ) scanf( "%s", arr[i] );

    printf( "\n" );

    for ( size_t i = 0; i < size; i++ ) strcpy( revarr[i], arr[size-i-1] );

    printf( "the reversed sentence is"  );

    for ( size_t i = 0; i < size; i++ ) printf( " %s", revarr[i] );
    printf( "\n" );

    return 0;
}

如果要进入

2
Hello World

然后输出

World Hello

考虑到只有在编译器支持C99时才会编译代码。 否则,您必须为字符数组动态分配内存。

至于你的代码,它有未定义的行为,整体上是无效的。 您没有为数组arrrevarr每个元素分配内存。 您不能将一个数组分配给另一个数组。 相反,你必须使用标准函数strcpy等。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM