简体   繁体   English

在c中声明和复制char字符串数组

[英]Declaring and copying an array of char strings in c

I made ac program that attempts to add the values of one string array to another using a separate method: 我制作了一个ac程序,尝试使用单独的方法将一个字符串数组的值添加到另一个字符串数组中:

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

void charConv(char *example[])
{
  example= (char* )malloc(sizeof(char[4])*6);
  char *y[] = {"cat", "dog", "ate", "RIP", "CSS", "sun"};

  printf("flag\n");

  int i;
  i=0;

  for(i=0; i<6; i++){
    strcpy(example[i], y[i]);
  }
}

int main() {
  char *x[6];
  charConv( *x[6]);

  printf("%s\n", x[0]);
}

However it keeps returning a segmentation fault. 但是,它一直返回分段错误。 I'm only beginning to learn how to use malloc and c in general and its been puzzeling me to find a solution. 我才刚刚开始学习一般如何使用malloc和c,这一直困扰着我寻找解决方案。

To pin-point your problem: you send *x[6] (here - charConv( *x[6]); ) which is the first char of the 7 'th (!!!) string (Remember, C is Zero-Base-Indexed ) inside an array of 6 string you didn't malloc -> using memory you don't own -> UB. charConv( *x[6]);问题所在:您发送*x[6] (此处charConv( *x[6]); ),它是第7个(!!!)字符串的第一个字符(请记住,C 为零,您没有malloc >使用您不拥有的内存-> UB的6个字符串数组中的Base-Indexed )。

Another thing I should note is char[] vs char * [] . 我还要注意的另一件事是char[] vs char * [] Using the former, you can strcpy into it strings. 使用前,你可以strcpy到它的字符串。 It would look like this: 它看起来像这样:

'c' | 'a' | 't' | '\0' | 'd' | 'o' | 'g' | ... [each cell here is a `char`]

The latter ( what you used ) is not a contiguous block of char s but a array of char * , hence what you should have done is to allocate memory for each pointer inside your array and copy into it. 后者(您所使用的)不是char的连续块,而是char *的数组,因此您应该做的是为数组中的每个指针分配内存并复制到其中。 That would look like: 看起来像:

 0x100 | 0x200 | 0x300... [each cell is address you should malloc and then you would copy string into]

But, you also have several problems in your code. 但是,您的代码中也有几个问题。 Below is a fixed version with explanations: 以下是带说明的固定版本:

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

void charConv(char *example[])
{
  // example= (char* )malloc(sizeof(char[4])*6); // remove this! you don't want to reallocate example! When entering this function, example points to address A but after this, it will point to address B (could be NULL), thus, accessing it from main (the function caller) would be UB ( bad )

  for (int i = 0; i < 6; i++)
  {
    example[i] = malloc(4); // instead, malloc each string inside the array of string. This can be done from main, or from here, whatever you prefer
  }


  char *y[] = {"cat", "dog", "ate", "RIP", "CSS", "sun"};

  printf("flag\n");

 /* remove this - move it inside the for loop
  int i;
  i=0;
  */

  for(int i=0; i<6; i++){
    printf("%s\t", y[i]); // simple debug check - remove it 
    strcpy(example[i], y[i]);
    printf("%s\n", example[i]); // simple debug check - remove it 
  }
}

int main() {
  char *x[6];
  charConv( x); // pass x, not *x[6] !!

  for (int i = 0; i < 6; i++)
  {
    printf("%s\n", x[i]);  // simple debug check - remove it 
  } 
}

As @MichaelWalz mentioned, using hard-coded values is not a good practice. 正如@MichaelWalz提到的那样,使用硬编码值不是一个好习惯。 I left them here since it's a small snippet and I think they are obvious. 我把它们留在这里,因为这是一个小片段,我认为它们很明显。 Still, try to avoid them 不过,请尽量避免

You need to start by understanding the pointers and some other topics as well like how to pass an array of strings to a function in C etc. In your program, you are passing *x[6] in charConv() which is a character. 您需要首先了解指针和其他一些主题,以及如何将字符串数组传递给C中的函数等。在您的程序中,您正在charConv()中传递* x [6]作为字符。

Made corrections in your program - 在程序中进行了更正-

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

void charConv(char *example[], int num)
{
  int i;

  for (i = 0; i < num; i++){
      example[i] = (char* )malloc(sizeof(char)*4);
  }

  const char *y[] = {"cat", "dog", "ate", "RIP", "CSS", "sun"};

  for(i = 0; i < num; i++){
      strcpy(example[i], y[i]);
  }
}

int main() {
  char *x[6];
  int i = 0;

  charConv(x, 6);

  /* Print the values of string array x*/
  for(i = 0; i < 6; i++){
      printf("%s\n", x[i]);
  }

  return 0;
}

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

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