【问题标题】:How to store characters into a char pointer using the strcpy() function如何使用 strcpy() 函数将字符存储到 char 指针中
【发布时间】:2013-10-13 05:23:25
【问题描述】:

我试图弄清楚为什么我不能使用 strcpy() 命令将字符存储到我的 char 指针中。运行以下代码时出现段错误。

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

int main(int argc, const char *argv[])                                               
{                                                                                    
   char *str1, *str2;                                                               
   int ret;                                                                         

   strcpy(str1, "abcdefg"); // stores string into character array str1              
   strcpy(str2, "abcdefg");                                                         

   printf("contents of %s \n", str1);                                               

   ret = strncmp(str1, str2, strlen(str2)); /* compares str1 to str2 */             

   if (ret > 0) {                                                                   
       printf("str1 is less than str2\n");                                          
  }                                                                                
  else if (ret < 0) {                                                              
      printf("str2 is less than str1\n");                                          
  }                                                                                
  else if (ret == 0) {                                                             
      printf("str1 is equal to str2\n");                                           
  }                                                                                

  return 0;                                                                        
  }   

谢谢!

【问题讨论】:

  • 您尝试在网上搜索 strcpy 吗?有很多文档描述了如何使用它。
  • 你可以,因为指针指向任何东西。
  • 我的意思是指向某物,即它已初始化。

标签: c segmentation-fault


【解决方案1】:

现在,str1 和 str2 只是指向一个字符的指针。 当您执行strcpy(str1, "abcdefg") 时,它会尝试将字符串“abcdefg”中的字符写入str1 指向的内存中,并且由于str1 指向未知内存,因此您可能没有任何写入权限,你得到一个分段错误。

修复它的一种方法是在堆上分配内存,然后存储这些字符串。

#include <stdlib.h>
...
/* assuming the max length of a string is not more than 253 characters */
char *str1 = malloc(sizeof(char) * 254);
char *str2 = malloc(sizeof(char) * 254);

您也可以使用strdup 来复制Gangadhar 提到的字符串。

另一种方法是在编译过程中将str1 和str2 声明为数组,建议使用Bryan Ash

char str1[] = "abcdefg";              
char str2[] = "abcdefg";

如果您想动态分配字符串而不是在堆上,您可以使用alloca(更多详细信息请阅读http://man7.org/linux/man-pages/man3/alloca.3.html),如kfsone 所述

【讨论】:

  • 不需要使用 sizeof char,在标准中它是 1。在 malloc 之后使用 calloc 或将第一个索引设置为 0 也是避免未终止字符串错误的好主意。
  • 谢谢大家!这个网站太棒了!
  • 有人仍在使用sizeof(char),因为随着时间的推移它可能会在新标准中发生变化
  • @boaz322 我想知道为什么我得到以下“警告:内置函数'malloc'的不兼容隐式声明[默认启用] char *str1 = malloc(sizeof(char) * 254 ); "
  • 你必须添加#include &lt;stdlib.h&gt; :)
【解决方案2】:

用-Wall 命令编译它会给出一个有用的提示

test.c:12:10: warning: 'str1' is used uninitialized in this function [-Wuninitialized]
    printf("contents of %s \n", str1);
          ^
test.c:14:17: warning: 'str2' is used uninitialized in this function [-Wuninitialized]
    ret = strncmp(str1, str2, strlen(str2)); /* compares str1 to str2 */

【讨论】:

  • 我认为这是回答此类问题的好方法!但也许还要告诉如何初始化字符串,这在 C 中有点不直观。
  • @hyde 感谢我刚开始使用 C/C++ 我希望我早点知道这一点
【解决方案3】:

鉴于这个例子,你甚至不需要 strcpy,你可以使用:

char str1[] = "abcdefg";              
char str2[] = "abcdefg";

如果您想了解有关指针的更多信息,请参阅斯坦福 CS 教育图书馆提供的名为 Pointers and Memory 的优秀免费电子书。

【讨论】:

  • 我昨天学习了指针,我想真正掌握它,所以我开始尝试在我的所有程序中使用它们。
猜你喜欢
  • 2015-04-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-11-14
  • 1970-01-01
  • 2017-03-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多