简体   繁体   English

如何在C中将char **转换为char *?

[英]How do I cast from char** to char* in C?

I have this problem: I have a matrix in which stores diferent characters. 我有这个问题:我有一个存储不同字符的矩阵。 Now I have to compare these characters to another one, but when compiling, it says strcmp recives char* and I have char**. 现在,我必须将这些字符与另一个字符进行比较,但是在编译时,它说strcmp接收char *,而我有char **。 So, how do I cast it? 那么,我该如何投放呢? This is the code I have: 这是我的代码:

For the matrix: 对于矩阵:

for (i = 0; i < x; i++){
        for (j = 0; j < y; j++){
            if (!fscanf(fol, "%c", &mat[i][j])){
                break;}
            //printf("%c", mat[i][j]);
            }
        }

The part I have problems with: 我遇到的问题是:

    for (x = 0; x < largo; x++){
        for (y = 0; y < ancho; y++){
            char *charcha;
            strcpy (charcha, mat[x][y]);

            //char *charcha = "%c", mat[x][y];
            int algo = strcmp(charcha, "0");
            if (algo == 0){
                printf (" ");
                }
            else{
            printf("%c", mat[x][y]);}
            }
            }

    printf ("\n");  

I tried with strcpy, but it failed as well :C 我尝试使用strcpy,但也失败了:C

Thanks 谢谢

If you want to compare strings, use: 如果要比较字符串,请使用:

for (x = 0; x < largo; x++)
{
   if ( strcmp(mat[x], "0") == 0 )
   {
      // Do whatever.
   }
   else
   {
      // Do whatever.
   }
}

If you want to compare characters, use: 如果要比较字符,请使用:

for (x = 0; x < largo; x++)
{
   for (y = 0; y < ancho; y++)
   {
      if (mat[x][y] == '0' )
      {
         // Do whatever.
      }
      else
      {
         // Do whatever.
      }
   }
}

To go from char** to char* use * 要从char **转换为char *,请使用*

example: 例:

char ** twoLevel = ..
char * oneLevel = *twoLevel;

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

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