【问题标题】:How do I assign to a variable in a for loop in C?如何在 C 的 for 循环中分配给变量?
【发布时间】:2019-08-14 09:26:44
【问题描述】:

Noob,正在研究加密问题,我想遍历 C 中数组中的组合,即 aaa、aab、aac、aba 等,然后将每个组合传递给一个函数(以检查该组合是否是正确的代码)。

我可以打印我想要控制台的内容没有问题,即 aa、ab、ba、bb,但无法将这些值放入我的临时变量中。

#include <stdio.h>

int main(void) {
    char *word = "ab";

    char * temp[3];
    //temp[2] = '\0'; 

    for (int i = 0; i < 2; i++){
        for (int j = 0; j < 2; j++){
            temp[0] = &word[i];
            temp[1] = &word[j];
            printf("%s\n", *temp);
            // printf("%c", word[i]);
            // printf("%c\n", word[j]);
            // pass_temp_to_function(temp);
        }
    }

    return 0;
}

当我应该得到 aa、ab、ba、bb(使用我上面的代码)时,我得到 ab、ab、b、b,但不知道为什么或如何更正它或以其他方式找到答案,因此是我的 Noobish 问题。

【问题讨论】:

  • 我想你想要一个char temp[3];
  • 别忘了初始化temp,因为C语言中的字符串需要以null结尾。

标签: c for-loop variable-assignment


【解决方案1】:

看起来你已经仔细编码了它以创建零警告,即使它是非常错误的,各种字符串数组和指向字符的指针在周围飞来飞去。

您希望有一个 单个字符串,包含 2 个字符,因为 3 个字符的数组就足够了:

char temp[3];
temp[2] = '\0'; 

...
    temp[0] = word[i];
    temp[1] = word[j];

    puts(temp); // less typing but essentially the same as printf("%s\n", temp);

【讨论】:

    【解决方案2】:

    temp 是指向char 的指针。我不知道为什么你的代码是这样构造的,所以我只是假设有一个原因。

                // printf("%c", word[i]);
                // printf("%c\n", word[j]);
    

    是的,这些行不起作用。一个小错误。轻松修复:

                printf("%c", temp[i][0]);
                printf("%c\n", temp[j][0]);
    

    我们必须再次取消引用char* 才能返回char。我本来可以写*(temp[i]),但temp[i][0] 更容易阅读。

    【讨论】:

    • 您的“修复”没有意义。 wordchar * 类型,所以 word[i] 已经是 char 并且您不能取消引用它。你的意思是有temp吗?
    • @NateEldredge 已修复
    【解决方案3】:

    我会推荐Kernighan and Ritchie的原始海报评论第5章。

    /* main.c - This is a novice program. */
    /* Copyright (C) 2019 Robin Miyagi https://www.linuxprogramming.ca/
     *
     * This program is free software; you can redistribute it and/or
     * modify it under the terms of the GNU General Public License as
     * published by the Free Software Foundation; either version 3 of the
     * License, or (at your option) any later version.
     *
     * This program is distributed in the hope that it will be useful, but
     * WITHOUT ANY WARRANTY; without even the implied warranty of
     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     * General Public License for more details.
     *
     * You should have received a copy of the GNU General Public License
     * along with this program; if not, write to the Free Software
     * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
     */
    
    #ifndef _GNU_SOURCE
    # define _GNU_SOURCE
    #endif
    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>
    #include <errno.h>
    #include <error.h>
    
    /*
     * Declared global so that the linker can find it.....................
     */
    int main (void);
    
    /*
     * Implementation.....................................................
     */
    int
    main
    (void)
    {
      char temp[3];
      int i;
      int j;
    
      temp[2] = '\0';
      for (i = 0; i < 2; ++i)
        for (j = 0; j < 2; j++)
          {
            temp[0] = 'a' + i;
            temp[1] = 'a' + j;
            printf ("%s\n", temp);  /* temp ---> beginning of array of char. */
          }
      return EXIT_SUCCESS;
    }
    

    【讨论】:

      【解决方案4】:

      在上述帮助下,我的代码如下所示:

      #include <stdio.h>
      #include <string.h>
      
      int main(void) {
      
          char * word = "abc";
      
          char temp[3];
          temp[2] = '\0';
      
          for (int i = 0; i < strlen(word); i++){
              for (int j = 0; j < strlen(word); j++){
                  temp[0] = word[i];
                  temp[1] = word[j];
                  puts(temp);
              }
          }
          printf("%s", temp);
      
          return 0;
      }
      

      //显示aa、ab、ac、ba、bb、bc、ca、cb、cc、cc(重复)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-04-26
        • 1970-01-01
        • 2015-07-07
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多