如何使用指针复制字符串

时间:2013-09-21 23:36:52

标签: c string pointers

这是我编写的用于复制字符串常量的程序。

程序运行时崩溃。为什么会这样?

#include <stdio.h>

char *alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
char c;
char *l;

main(){
   while((c = *alpha++)!='\0')
       *l++ = *alpha;
   printf("%s\n",l);
}

7 个答案:

答案 0 :(得分:17)

执行此类手动复制

#include <stdio.h>
#include <stdlib.h>

int main()
{
    char* orig_str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    char* ptr = orig_str;

    // Memory layout for orig_str:
    // ------------------------------------------------------------------------
    // |0|1|2|3|4|5|6|7|8|9|10|11|12|13|14|15|16|17|18|19|20|21|22|23|24|25|26|  --> indices
    // ------------------------------------------------------------------------
    // |A|B|C|D|E|F|G|H|I|J|K |L |M |N |O |P |Q |R |S |T |U |V |W |X |Y |Z |\0|  --> data
    // ------------------------------------------------------------------------

    int orig_str_size = 0;
    char* bkup_copy = NULL;

    // Count the number of characters in the original string
    while (*ptr++ != '\0')
        orig_str_size++;        

    printf("Size of the original string: %d\n", orig_str_size);

    /* Dynamically allocate space for the backup copy */ 

    // Why orig_str_size plus 1? We add +1 to account for the mandatory 
    // '\0' at the end of the string.
    bkup_copy = (char*) malloc((orig_str_size+1) * sizeof(char));

    // Place the '\0' character at the end of the backup string.
    bkup_copy[orig_str_size] = '\0'; 

    // Current memory layout for bkup_copy:
    // ------------------------------------------------------------------------
    // |0|1|2|3|4|5|6|7|8|9|10|11|12|13|14|15|16|17|18|19|20|21|22|23|24|25|26|  --> indices
    // ------------------------------------------------------------------------
    // | | | | | | | | | | |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |\0|  --> data
    // ------------------------------------------------------------------------

    /* Finally, copy the characters from one string to the other */ 

    // Remember to reset the helper pointer so it points to the beginning 
    // of the original string!
    ptr = &orig_str[0]; 
    int idx = 0;
    while (*ptr != '\0')
        bkup_copy[idx++] = *ptr++;

    printf("Original String: %s\n", orig_str);   
    printf("Backup String: %s\n", bkup_copy);

    return 0;
}

答案 1 :(得分:15)

要在C中复制字符串,可以使用strcpy。这是一个例子:

#include <stdio.h>
#include <string.h>

const char * my_str = "Content";
char * my_copy;
my_copy = malloc(sizeof(char) * (strlen(my_str) + 1));
strcpy(my_copy,my_str);

如果您想避免意外的缓冲区溢出,请使用strncpy代替strcpy。例如:

const char * my_str = "Content";
const size_t len_my_str = strlen(my_str) + 1;
char * my_copy = malloc(len_my_str);
strncpy(my_copy, my_str, len_my_str);

答案 2 :(得分:3)

您需要为l分配空间。目前它指向内存中的随机位置,如果您尝试写入该位置,操作系统可能会关闭(AKA崩溃)您的应用程序。如果您希望代码按原样工作,请为l分配一些空格malloc()或创建l作为字符数组,并留有足够的空间来容纳"ABCDEFGHIJKLMNOPQRSTUVWXYZ"加上NULL终止子。

有关指针的参考资料,请参阅http://cslibrary.stanford.edu/106/

答案 3 :(得分:0)

  

复制字符串“constant / literal / pointer”

char *str = "some string thats not malloc'd";
char *tmp = NULL; 
int i = 0; 
for (i = 0; i < 6; i++) {
    tmp = &str[i];
}
printf("%s\n", tmp);
  

<强>反相

char *str = "some stupid string"; 
char *tmp, *ptr = NULL; 
ptr = str;
while (*str) { ++str; } 
int len = str - ptr;
int i = 0;
for (i = len; i > 11; i--) {
    tmp = &ptr[i];
} 
printf("%s\n", tmp); 

tmp = &blah[i]可以与tmp = &(*(blah + i))互换。

答案 4 :(得分:0)

-ifndef(LAGER).
-define(LAGER, 1).
-endif.

答案 5 :(得分:0)

您可以直接执行以下代码:

char *alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
char *l = alpha;

如果您的代码如下:

const char *alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
const char *l = alpha;

:)

答案 6 :(得分:0)

cpy函数将采用两个char指针,src指针将指向main函数中定义的src(char数组)的初始字符,与des指针相同将指向des(char数组)的初始位置在main函数中定义,src指针的while循环值会将值赋给des指针并将指针递增到下一个元素,这将发生,直到while循环遇到null并且循环出来并且des指针将简单在获取所有值后指定null。

#include<stdio.h>
void cpy(char *src,char *des)
{
     while(*(des++) = *(src++));
     *des = '\0';
}

int main()
{
     char src[100];
     char des[100];
     gets(src);
     cpy(src,des);
     printf("%s",des);
}

输出:Image