【问题标题】:How to reuse a Array variable in C using pointers如何使用指针在 C 中重用数组变量
【发布时间】:2014-01-19 20:09:22
【问题描述】:

我需要回答这个问题来测试这个:

练习 19-16:修复示例中的代码,以便在 while 循环运行之前保存示例变量的值,然后再恢复。在while循环执行完毕后,在代码中添加puts(sample)语句,证明变量的原始地址已经恢复。

**Example:**
     #include <stdio.h>

    int main()
    {
        char *sample = "From whence cometh my help?\n";

        while(putchar(*sample++))
            ;
        return(0);
    }

我想检查一下我的答案是否正确,可能你可以给出解释,因为我对指针和变量没有清楚的理解。 这是我的答案:

     #include <stdio.h>

        int main()
        {
            char *sample = "From whence cometh my help?\n"; //This is my string
            char StringSample[31];
            int index = 0;

            while(sample[index] != '\0') //while the char is NOT equal to empty char (the last one)
            {
                index++; //increase the index by 1
                StringSample[index] = sample[index]; //put the char "n" inside the stringSample //
                putchar(*sample++);  //write it to the screen
            }
            putchar('\n'); //A new line

            //to put the entire string on screen again
            for (index=0; index<60; index++)
            {
                putchar(StringSample[index]); 
            }
            return(0);
        }

这是我的输出:

我不知道为什么将字符串拆分为 From whence co 以及为什么其余文本(如您所见)没有意义。

我使用的是 Xcode 5.02

【问题讨论】:

    标签: c++ c coding-style programming-languages


    【解决方案1】:

    问题是您试图引用您的 sample 变量作为索引以及指针。这会导致你得到错误的结果。

    while(sample[index] != '\0')   
    {
        index++;
        StringSample[index] = sample[index];  // indexed access
        putchar(*sample++);                   // incrementing pointer sample. 
    }   
    

    你可以简单地实现你的目标

    #include <stdio.h>
    
    int main()
    {
        char *sample = "From whence cometh my help?\n";
        char *ptr = sample;  // Saving the value of sample
    
         while(putchar(*sample++))
            ;
        putchar('\n');
    
        sample = ptr;        // Restoring sample's value.
        puts(sample);
    
        return(0);
    }  
    

    【讨论】:

      【解决方案2】:
      char StringSample[31];
      ....
      for (index=0; index<60; index++)
      {
          putchar(StringSample[index]);
      }
      

      你的数组有 31 个单元,但你迭代很远直到 60。天知道你可以访问什么。

      【讨论】:

        【解决方案3】:

        您让这种方式变得更加复杂,超出了它的需要。只需将指针保存在另一个变量中并在完成后将其复制回来。

         #include <stdio.h>
        
        int main()
        {
            char *sample = "From whence cometh my help?\n";
            char *temp = sample;
        
            while(putchar(*sample++))
                ;
        
            sample = temp;
            puts(sample);
            return(0);
        }
        

        输出:

        From whence cometh my help?
        From whence cometh my help?
        

        【讨论】:

          猜你喜欢
          • 2014-03-29
          • 2021-12-25
          • 1970-01-01
          • 2015-09-05
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多