简体   繁体   中英

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:

#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.

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.

Another thing I should note is char[] vs char * [] . Using the former, you can strcpy into it strings. 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. 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. 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.

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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