0

我正在尝试使用指针算法将一个 char 数组复制到另一个 char 数组中。在 copy() 中似乎是正确的,但是我不明白它进入 main 后会发生什么。char a[] 即使我调用了该函数,也没有得到 char b[] 的值。我错过了什么吗?呵呵呵呵

    #include <stdio.h>

    void copy(char a[], char b[]){
        int *apoint = &a;
        printf("%d\n", apoint);
        printf("%d\n", &a);
        *apoint = b;
        printf("%d\n", *apoint);
        printf("%s\n", a);
        printf("%s\n", b);
    }

   int main(void){
        char a[100];
        char b[] = "bluhbluh";

        copy(a,b);
        printf("%d\n", a);
   }
4

6 回答 6

2

这真的不是很有意义的C。我怀疑它甚至没有在没有警告的情况下编译?

首先获取 array 的地址a,然后获取 array 的地址b,将其转换为整数(没有强制转换!),然后将该整数写入 array a。然后,在 main 结束时,您尝试将 char 数组打印为整数。

指针算术不太像那样工作。:)

我认为你真的打算做这样的事情:

void copy(char a[], char b[]){
    char *ptr_a = a;
    char *ptr_b = b;

    while (*ptr_b != '\0') {
        *ptr_a = *ptr_b;
        ptr_a++;
        ptr_b++;
    }
    *ptr_a = '\0';
}

int main(void){
    char a[100];
    char b[] = "bluhbluh";

    copy(a,b);
    printf("%s\n", a);
}

当然,正确的做法是这样的:

#include <string.h>

int main(void){
    char a[100];
    char b[] = "bluhbluh";

    strncpy(a, b, 100);
    printf("%s\n", a);
}
于 2012-11-14T11:19:52.803 回答
2

As ams points out in his/her answer, if you want a copy of the array, you need to duplicate each entry. Assuming you don't want to duplicate the array, but instead want a to point at b:

apoint doesn't do anything - it can't change the pointers of a or b. If you wanted to modify the address that a or b pointed to, you'd have to pass the address of them to your function. This won't work, however, as arrays are not pointers - it's why you can't reassign arrays in C. You can modify pointers though - the following is an example of modifying a pointer to a to point at b:

#include <stdio.h>

void redirect(char** a, char* b){
    *a = b;
}

int main(void){
    char a[100];
    char b[] = "bluhbluh";

    char* apt = a;
    redirect(&apt,b);
    printf("a=%s, b=%s", apt,b);

}
于 2012-11-14T11:25:27.507 回答
0

apoint应该是 a char*,你没有关于这个指针分配的警告吗?此外,您不会迭代字符串的所有字符...

于 2012-11-14T11:09:38.753 回答
0

也许这行int *apoint = &a是错误的,&a是指向 char 的指针,而apointis int *。你没有编译错误吗?

于 2012-11-14T11:12:58.777 回答
0

-Wall如果启用或在 gcc中复制 char 的方法不好-Werror,您会看到很多错误。

您想要的东西,只需复制 main 中的指针即可,无需调用copy (a,b);

char *a;
char b[]="bluhbluh"
a=b;

现在开始指向字符串“bluhbluh”

但我认为你不希望你希望每个字符都array b被复制到array a

你必须这样做

void copy(char *a,char *b)
{
  while(*b!='\0')
    *a++=*b++;
 }

int main()
{
  char a[100];
  char b[]="bluhbluh";
  copy(a,b);
  printf("%s\n",a);
}
于 2012-11-14T11:15:42.727 回答
0

您的代码中有一些重大错误,特别是这一行不正确int *apoint = &a;,应该是char* *apoint = &a;

//Corrected Code:
#include <stdio.h>

void copy(char a[], char b[]){
    char* *apoint = &a;
    printf("%d\n", apoint);
    printf("%d\n", &a);
    *apoint = b;
    printf("%d\n", *apoint);
    printf("%s\n", a);
    printf("%s\n", b);
}

int main(void){
    char a[100];
    char b[] = "bluhbluh";

    copy(a,b);
    printf("%d\n", a);
}

这将解决主要的代码问题,尽管我认为您的复制功能仍然不会给出您想要的结果,因为方法似乎不太正确。

于 2012-11-14T11:21:12.450 回答