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

在c语言中声明和复制字符字符串数组,c,arrays,malloc,C,Arrays,Malloc,我制作了一个c程序,试图使用一个单独的方法将一个字符串数组的值添加到另一个字符串数组: #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

我制作了一个c程序,试图使用一个单独的方法将一个字符串数组的值添加到另一个字符串数组:

#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]);
}
#包括
#包括
void charConv(char*示例[])
{
示例=(char*)malloc(sizeof(char[4])*6);
char*y[]={“猫”、“狗”、“吃”、“撕”、“CSS”、“太阳”};
printf(“flag\n”);
int i;
i=0;

对于(i=0;i来指出您的问题:您在一个由6个字符串组成的数组中发送
*x[6]
(此处-
charConv(*x[6]);
),这是7第(!!!)个字符串的第一个字符(请记住,C是零基索引的),该数组使用您不拥有的内存
malloc
->创建

我应该注意的另一件事是
char[]
vs
char*[]
。使用前者,您可以
strcpy
插入到它的字符串中。它如下所示:

'c' | 'a' | 't' | '\0' | 'd' | 'o' | 'g' | ... [each cell here is a `char`]
后者(您使用的)不是一个连续的
char
s块,而是一个
char*
数组,因此您应该为数组中的每个指针分配内存并复制到其中。这类似于:

 0x100 | 0x200 | 0x300... [each cell is address you should malloc and then you would copy string into]
但是,您的代码中也存在一些问题。下面是一个固定版本,并附有说明:

#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 
  } 
}
#包括
#包括
void charConv(char*示例[])
{
//示例=(char*)malloc(sizeof(char[4])*6);//删除此项!您不想重新分配示例!输入此函数时,示例指向地址A,但在此之后,它将指向地址B(可能为NULL),因此,从main(函数调用方)访问它将是UB(错误)
对于(int i=0;i<6;i++)
{
示例[i]=malloc(4);//取而代之的是malloc字符串数组中的每个字符串。这可以从main或从这里完成,您可以选择任何方式
}
char*y[]={“猫”、“狗”、“吃”、“撕”、“CSS”、“太阳”};
printf(“flag\n”);
/*移除此-将其移动到for循环内
int i;
i=0;
*/

对于(inti=0;i,您需要从理解指针和一些其他主题开始,以及如何将字符串数组传递给C中的函数等。 在程序中,您正在charConv()中传递*x[6],这是一个字符

在您的程序中进行了更正-

#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;
}
#包括
#包括
#包括
void charConv(char*示例[],int num)
{
int i;
对于(i=0;i
此处的解引用错误。是否使用
-Wall
检查警告?您需要从理解指针开始。通过查看代码,很明显您没有完全理解它们。请尝试使用类似于
gdb
lldb
的调试器。您可以运行代码并找出哪一行实际断开。其他e是多个问题(快速计算,至少有六个)在您的代码中。解释它们将花费某人大量的时间,而且您可能无论如何都不会理解这些解释。在编译器中打开警告,并尝试让代码在没有警告的情况下编译。一次只处理一个问题。同时,您还应删除这些硬编码值,例如4、6.6 i我在这里使用了硬编码的值来匹配OP。不想混淆太多。对于这样的小代码片段,我不认为简单的魔法数字是一个糟糕的实践手段。你让我更清楚了。硬编码的值很快放在那里。这是一个我试图理解的快速程序。指针和malloc.@CIsForCookies好的,你是对的,但是提到它可能是个好主意。