复制C中的数组 [英] Copying an array in C

查看:160
本文介绍了复制C中的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始阅读K&放学习C,R,并通过一些练习下去。有些挣扎之后,我终于能够完成练习1-19与下面的code:

I'm starting to learn C by reading K&R and going through some of the exercises. After some struggling, I was finally able to complete exercise 1-19 with the code below:

/* reverse: reverse the character string s */
void reverse(char s[], int slen)
{
  char tmp[slen];
  int i, j;

  i = 0;
  j = slen - 2;    /* skip '\0' and \n */

  tmp[i] = s[j];
  while (i <= slen) {
    ++i;
    --j;
    tmp[i] = s[j];
  }

  /* code from copy function p 29 */
  i = 0;
  while ((s[i] = tmp[i]) != '\0')
    ++i;

}

我的问题是关于code的最后一点,其中 TMP 字符数组复制到取值 。为什么没有一个简单的 S = tmp目录; 而不是工作?为什么人必须通过索引通过数组复制指数迭代?

My question is regarding that last bit of code where the tmp char array is copied to s. Why doesn't a simple s = tmp; work instead? Why does one have to iterate through the array copying index by index?

推荐答案

也许我只是老脾气暴躁,但我看到其他的答案似乎完全错过了这一点。

Maybe I'm just old and grumpy, but the other answers I've seen seem to miss the point completely.

C没有做数组赋值,期限。你无法通过简单的赋值分配一个数组另一个数组,不像一些其他语言(PL / 1中,例如,Pascal和它的许多后裔太 - 阿达,MODULA,奥伯伦等)。也不ç真的有一个字符串类型。它只有字符数组,而你不能没有使用循环或函数调用复制的字符数组(任何比你更可以复制任何其他类型的数组)。 [字符串文字并不真正算作一个字符串类型。]

C does not do array assignments, period. You cannot assign one array to another array by a simple assignment, unlike some other languages (PL/1, for instance; Pascal and many of its descendants too - Ada, Modula, Oberon, etc.). Nor does C really have a string type. It only has arrays of characters, and you can't copy arrays of characters (any more than you can copy arrays of any other type) without using a loop or a function call. [String literals don't really count as a string type.]

阵列复制唯一的一次,当阵列嵌入在结构和你做一个结构分配。

The only time arrays are copied is when the array is embedded in a structure and you do a structure assignment.

在我的K&放复印件; R第2版,运动1-19询问功能反向(S);在我的K&放复印件; R第1版,它是运动1-17,而不是1-19,但同样有人问

In my copy of K&R 2nd Edition, exercise 1-19 asks for a function reverse(s); in my copy of K&R 1st Edition, it was exercise 1-17 instead of 1-19, but the same question was asked.

由于指针尚未包括在此阶段,溶液应使用索引,而不是指针。笔者认为导致:

Since pointers have not been covered at this stage, the solution should use indexes instead of pointers. I believe that leads to:

#include <string.h>
void reverse(char s[])
{
    int i = 0;
    int j = strlen(s) - 1;
    while (i < j)
    {
        char c = s[i];
        s[i++] = s[j];
        s[j--] = c;
    }
}

#ifdef TEST
#include <stdio.h>
int main(void)
{
    char buffer[256];
    while (fgets(buffer, sizeof(buffer), stdin) != 0)
    {
        int len = strlen(buffer);
        if (len == 0)
            break;
        buffer[len-1] = '\0';  /* Zap newline */
        printf("In:  <<%s>>\n", buffer);
        reverse(buffer);
        printf("Out: <<%s>>\n", buffer);
    }
    return(0);
}
#endif /* TEST */

编译这跟-DTEST包括测试程序,没有以刚才的功能反向()定义。

随着问题中给出的函数签名,你避免调用的strlen()输入每行的两倍。注意使用与fgets() - 即使是在测试程序,这是一个坏主意,用获得()。的与fgets的缺点()相比,获得()与fgets()不删除尾随的换行符,其中获得()一样。 与fgets的上升空间()是你没有得到数组溢出,你可以告诉程序是否找到了一个新行或是否之前跑出空位(或数据)遇到一个换行符。

With the function signature given in the question, you avoid calling strlen() twice per line of input. Note the use of fgets() — even in test programs, it is a bad idea to use gets(). The downside of fgets() compared to gets() is that fgets() does not remove the trailing newline where gets() does. The upsides of fgets() are that you don't get array overflows and you can tell whether the program found a newline or whether it ran out of space (or data) before encountering a newline.

这篇关于复制C中的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆