39(将数组按列存取到字符串中)

39 date:2021.3.22
在这里插入图片描述
要点:
b[k++] = s[i][j]; 而不是 b[k++] = s[j[i]; 你想这样的话行都有4行了,溢出了,就不对了!

详细代码如下:

#include<stdio.h>
#define  M  3
#define  N  4
void fun(char (*s)[N],char *b)
{
    
    
  /*
	analyse:

	按列遍历数组;
	存放入字符串;
  */
	
	int i,j,k = 0;

	for(j = 0; j<4; j++)
	{
    
    
		for(i = 0; i<3; i++)
		{
    
    
			b[k++] = s[i][j];
		}
	}
	b[k] = '\0';


}
void main()
{
    
     
  FILE *wf;
  char a[100],w[M][N]={
    
    {
    
     'W', 'W', 'W', 'W'},{
    
    'S', 'S', 'S', 'S'},{
    
    'H', 'H', 'H', 'H'}};
  int i,j;
  printf("The matrix:\n");
  for(i=0;i<M;i++)
     {
    
     for(j=0;j<N;j++) 
          printf("%3c",w[i][j]);
       printf("\n");
     }
  fun(w,a);
  printf("The A string:\n");
  puts(a);
  printf("\n\n");
/******************************/
  wf=fopen("out.dat","w");
  fprintf(wf,"%s",a);
  fclose(wf);
/*****************************/
}

猜你喜欢

转载自blog.csdn.net/weixin_44856544/article/details/115066049
今日推荐