简体   繁体   English

将单个字符从字符数组复制到 C 中的另一个字符数组

[英]Copy a single character from a character array to another character array in C

I am fairly new to C programming and trying to improve.我对 C 编程相当陌生,并试图改进。 I have seen a few question similar to this and tried to implement their suggestions using strncpy but it still won't work.我看到了一些与此类似的问题,并尝试使用 strncpy 实施他们的建议,但它仍然无法正常工作。 Can anyone give me a pointer in the right direction?谁能给我一个正确方向的指针? Thanks.谢谢。

The aim of the code is to be given a string and split it on finding a blank space.代码的目的是给出一个字符串并在找到空格时将其拆分。 I am deliberately not using strtok() .我故意不使用strtok()

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

char str[10] ;
char word[10] ;
int i, j, k, l ;

  int main()
{
  printf("Please enter a string of ten characters and I'll find the blanks\n") ;
  fgets(str,10,stdin) ;

  printf("Original string is %s\n", str) ;

  j = 0 ;

  for(i == 0 ; i < 10 ; i++){
    if(!isblank(str[i])){
      strncpy(word[j], &str[i], 1) ;
      printf("i = %d, j = %d, str[i] = %c, word[j] = %c\n",i, j, str[i], word[j]) ;
      j++ ;
    }else{
      printf("%s\n",word) ;
      j = 0 ;
    }
  }
  return 0 ;
}

The problem, as I see it, is in在我看来,问题在于

for(i == 0 ; i < 10 ; i++)
      ^^

You should be using the assignment ( = ), like您应该使用赋值( = ),例如

for(i = 0 ; i < 10 ; i++)

to set the initial value of i .设置i的初始值。 Otherwise, by accessing the value of an uninitialized local variable (whose value is indeterminate without an initialization), your code will invoke undefined behavior .否则,通过访问未初始化的局部变量的值(其值在没有初始化的情况下是不确定的),您的代码将调用未定义的行为

Then, the usage of strncpy() also looks wrong, you're not passing a char * , as required for the first argument.然后, strncpy()的用法看起来也有误,您没有按照第一个参数的要求传递char *

Now, that said, two more things,现在,也就是说,还有两件事,

  1. To copy a single char one at a time, you can simply use the assignment , like要一次复制一个char ,您可以简单地使用赋值,例如

    word[j] = str[i];
  2. You should only loop over the input array till the valid entries, not the whole size (10).您应该只循环输入数组直到有效条目,而不是整个大小(10)。 In case, the input is smaller, you'll again hit UB.如果输入较小,您将再次点击 UB。

I think your problem is no pointer to the dest argument in the strncpy function.我认为您的问题是没有指向 strncpy 函数中的 dest 参数的指针。

strncpy(word[j], &str[i], 1) ;
        ^

Add a pointer to the destination for the strncpy function为 strncpy 函数添加一个指向目标的指针

strncpy(&word[j], &str[i], 1) ;

as well as fixing the issue mentioned by Sourav Ghosh and you should have it.以及解决 Sourav Ghosh 提到的问题,您应该拥有它。

As i commented on the @James, this is a REAL issue on non-BSD where there is no strsep().正如我对@James 的评论,这是非 BSD 上没有 strsep() 的真正问题。 strtok() and strtok_r() are both IMO fundamentally flawed in that they unilaterally skip any sequence of >1 adjacent delimiters. strtok() 和 strtok_r() 都是 IMO 的根本缺陷,因为它们单方面跳过任何 >1 个相邻分隔符的序列。

I used this solution: https://www.geeksforgeeks.org/c-program-replace-word-text-another-given-word/ in order to fill the above with space gaps so that I can get a proper tokenization of ALL my payload.我使用了这个解决方案: https : //www.geeksforgeeks.org/c-program-replace-word-text-another-given-word/以便用空格填充上面的内容,以便我可以对所有内容进行适当的标记化我的有效载荷。 Reader beware, the solution above has a flaw...so you might need to call it more than once...Not optimal, but if you are in a hurry as I was, I used it and it works.读者要注意,上面的解决方案有一个缺陷......所以你可能需要多次调用它......不是最佳的,但是如果你像我一样匆忙,我使用了它并且它有效。

  while(strstr(token, ";;")!=NULL) {
                 token = replaceWord(token, ";;", "; ;");
                 (void) sprintf (v_sg_TempMsg, "Massaged the input agsId to token:[%s]", token);
                 printf(v_sg_TempMsg);
                 ++tokenCounter;
                 if(tokenCounter==15) {
                     break;
                 }
   }//while(strstr(token, ";;")!=NULL) {

...and the above is a hack to get call the code that is flawed to eventually remove all the occurrences of the delimiters...this is dirty dirty code, so please, no flamers...it is here to HELP people who are blocked because of a lack of an alternative to strtok() and strtok_r() skipping. ...以上是调用有缺陷的代码以最终删除所有出现的分隔符的技巧......这是肮脏的肮脏代码,所以请不要喷火......它在这里帮助那些由于缺少 strtok() 和 strtok_r() 跳过的替代方法而被阻止。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM