标签: strcpy

Strcpy()会破坏Solaris中复制的字符串,但不会损坏Linux

我正在为一个类编写C代码.这个类要求我们的代码在学校服务器上编译和运行,这是一个sparc solaris机器.我正在运行Linux x64.

我有这条线要解析(这不是实际的代码,但输入我的程序):

while ( cond1 ){ 
Run Code Online (Sandbox Code Playgroud)

我需要将"while"和"cond1"捕获到单独的字符串中.我一直在用strtok()这个.在Linux中,以下行:

char *cond = NULL;
cond = (char *)malloc(sizeof(char));
memset(cond, 0, sizeof(char));
strcpy(cond, strtok(NULL, ": \t\(){")); //already got the "while" out of the line
Run Code Online (Sandbox Code Playgroud)

将正确捕获字符串"cond1".然而,在solaris机器上运行它,给我字符串"cone1".

请注意,在我的程序中的许多其他情况下,字符串正在被正确复制.(例如,"while")被正确捕获.

有谁知道这里发生了什么?

c solaris sparc strcpy

3
推荐指数
1
解决办法
784
查看次数

为什么C的strcpy会因双重索引数组而失败?

以下代码似乎是段错误,我无法弄清楚原因.

#include <string.h>

static char src[] = "aaa";

int main()
{
   char* target[2] = {"cccc","bbbbbbbbbb"};
   strcpy(target[1],src);
   return 0;
}
Run Code Online (Sandbox Code Playgroud)

c arrays pointers cstring strcpy

3
推荐指数
1
解决办法
352
查看次数

为什么strcpy没有分段错误?

可能重复:
未定义,未指定和实现定义的行为

这应该是错误的.为什么不呢.

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

    char str1[] = "Sample string. Sample string. Sample string. Sample string. Sample string. ";
    char str2[2];

    int main ()
    {
      strcpy (str2,str1);
      printf("%s\n", str2);
      return 0;
    }
Run Code Online (Sandbox Code Playgroud)

我使用gcc版本4.4.3与以下命令:

    gcc -std=c99 testString.c -o test
Run Code Online (Sandbox Code Playgroud)

我也尝试将优化设置为o(-O0).

c stack-overflow c99 strcpy

3
推荐指数
1
解决办法
583
查看次数

strcpy和strcat的实现,它获取指针bug的引用

可能重复:
对此c函数copyString,concatString有任何更好的建议

这是一个面试的问题,我需要用特定的签名来实现它,这是我需要工作的代码:

int main(int argc, char *argv[])
{

    char *str = NULL;
    new_strcpy(&str , "string one");
    new_strcpy(&str , str +7);
    new_strcat(&str , " two");
    new_printf(&str , "%str !", s);
    puts(str ); 
    new_free(&str);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

这是我对new_strcpy的实现:

char* new_strcpy(char **dst,const char *source)
{

  char *ans=*dst;

  while(**dst++=*source++);

  return ans;

}
Run Code Online (Sandbox Code Playgroud)

但这个解决方案崩溃了,有人可以帮帮我吗?

c string strcpy strcat

3
推荐指数
1
解决办法
1903
查看次数

使用strcpy和equal运算符分配字符串

以下代码有什么区别?

1.

char *p;
strcpy(p,"String");
Run Code Online (Sandbox Code Playgroud)

2.

char *p;
p = "String";
Run Code Online (Sandbox Code Playgroud)

指针指向同一个字符串,但有什么区别吗?

c pointers strcpy

3
推荐指数
1
解决办法
2703
查看次数

strcpy在ios​​7上表现不同

IOS7似乎带有字符串strcpy的新实现(可能是优化).之前我能够从阵列的任何位置复制字符串,但现在如果我从任何位置开始复制(i%4!= 0)它将崩溃.

为了表明这一点,我在iOS6和7中运行了这个代码,它在7上崩溃了应用程序:

  char *x = malloc(1024);
  strcpy(x, "hello world");
  char *x2 = x + 1;
  strcpy(x, x2);
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?

c string crash strcpy ios7

3
推荐指数
1
解决办法
241
查看次数

比较两个字符串,strcmp的问题

我正在尝试检查从stdin读取的行是否以"login:"开头,但strcmp似乎不起作用.

char s1[20], s2[20];
fgets(s1, 20, stdin);
strncpy(s2,s1,6);
strcmp(s2, "login:");
if( strcmp(s2, "login:") == 0)
    printf("s2 = \"login:\"\n");
else
    printf("s2 != \"login:\"\n");
Run Code Online (Sandbox Code Playgroud)

我不关心"login:"之后会发生什么,我只是想确定命令是如何给出的.我究竟做错了什么?

c strcmp strcpy

3
推荐指数
1
解决办法
1万
查看次数

警告:内置函数'strlen'和'strcpy'的隐式声明不兼容

我刚刚完成了我的刽子手游戏,作为最后一步,我正在进行一些代码清理和优化,但我似乎无法理解为什么我会收到以下两个警告:

警告:内置函数'strlen'的不兼容隐式声明

警告:内置函数'strcpy'的隐式声明不兼容

使用它们的代码在这里:

for(i = 1; i <= random; i++)
    fgets(word, 100, f);
fclose(f);

for(i = 0; i < strlen(word); i++)
    if(word[i] == '\n')
        word[strlen(word)-1] = '\0';

lungime = strlen(word);
strcpy(word2, word);
Run Code Online (Sandbox Code Playgroud)

我所做的是使用fgets从文件中读取随机单词.在此之后,我删除了fgets自动放置的'\n'.最后,我在word2中复制了这个单词.

二手图书馆:

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
Run Code Online (Sandbox Code Playgroud)

变量声明:

int random, n = 0, i, j, lengh, tries = 5, ok = 0, error = 0;;
char word[100], word2[100], tried_letters[50], auxch;
Run Code Online (Sandbox Code Playgroud)

注意:我的程序中的一切都很完美,但我收到了两个我想要摆脱的警告.

提前致谢.

c warnings strlen strcpy

3
推荐指数
1
解决办法
1万
查看次数

C中的"中止陷阱:6"错误?

我是C的初学者,但我在终端上通过gcc在xcode上运行此代码:

#include <stdio.h>
#include <string.h> 
int main(){
    char name[12] = "Roman Mirov"; 
    printf("My name is %s\n", name);
    name[8] = 'k'; 
    printf("My name is %s\n", name);
    char greeting[] = "hello"; 
    printf("%s %s\n", greeting, name);
    strcpy(greeting, "greetings, "); 
    printf("%s%s\n", greeting, name);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

它输出这个:

My name is Roman Mirov
My name is Roman Mikov
hello Roman Mikov
Abort trap: 6
Run Code Online (Sandbox Code Playgroud)

我的问题确切地说,为什么它产生错误而不是显示最后一行作为输出"问候,罗马Mikov"?

c arrays strcpy

3
推荐指数
1
解决办法
1万
查看次数

如何正确使用strncpy?

我知道strncpy是一个更安全的版本,strcpy如说在这里.

但是,当我想要复制srcdst并且dst不是一个干净的缓冲区时,我会收到不需要的结果,这可以通过以下方式避免strcpy:

char *p = "123";
char a[10] = "aaaaaa";

strncpy(a,p,strlen(p));
printf("%s\n", a);   // 123aaa

strcpy(a,p);
printf("%s\n", a);   // 123 <- desired output, as the trailing a's are garbage
Run Code Online (Sandbox Code Playgroud)

在我的实际情况中,我知道strlen(src) < sizeof(dst)(至少,如果不是这样,程序会很快崩溃),所以我可以安全地strcpy.

但是,如果strncpy是我应该使用的,那么我必须在之后添加dst[strlen(src)] = '\0'以避免垃圾(或者更好的是,事先启动缓冲区吗?)?

c strcpy strncpy

3
推荐指数
2
解决办法
1708
查看次数

标签 统计

c ×10

strcpy ×10

arrays ×2

pointers ×2

string ×2

c99 ×1

crash ×1

cstring ×1

ios7 ×1

solaris ×1

sparc ×1

stack-overflow ×1

strcat ×1

strcmp ×1

strlen ×1

strncpy ×1

warnings ×1