0

I made a c 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.

4
  • 1
    you have some wrong dereferencing here. have you checked the warnings with -Wall ? Commented Oct 2, 2017 at 12:51
  • 2
    You need to start by understanding pointers. By looking at your code, it's clear that you are not fully understanding them. Commented Oct 2, 2017 at 12:51
  • try a debugger like gdb or lldb. you can run your code and find out which line actually broke. Commented Oct 2, 2017 at 13:38
  • There are multiple problems (at least half a dozen, on a quick count) in your code. Explaining them would take someone a significant amount of time, and you probably wouldn't understand the explanations anyway. Turn up warnings in your compiler, and try getting the code to compile without warnings. Work on ONE problem at a time. Commented Oct 2, 2017 at 13:39

2 Answers 2

1

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 chars 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

Sign up to request clarification or add additional context in comments.

4 Comments

While you're at it you should also remove these hardcoded values such as 4, 6. 6 is even duplicated.
@MichaelWalz I used hard-coded values myself here, to match the OP. Don't want to confuse too much. Also for such small code snippets, I don't consider simple magic numbers a bad practice
Thank you! You made this much more clear to me. The hardcoded values were quickly placed there. This was a quick program me trying to understand pointers and malloc.
@CIsForCookies OK, you're right, but mentionning it could be a good idea anyway.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.