-4

What I want to do is take the string, save it into an array of strings and then modify each copy based on index.

I edited the question and code, it was messy and unclear. As the question is almost the same (I think now it's more precise), I thought I could edit it entirely here without creating a new question, but let me know if I have to do differently.

PROBLEM (EDIT): after reading the answer given, creating an MVCE, and reading this and some tips to debug, I think I am doing a mess with pointers and strcpy... Why does the following code (edited to be MVCE) gives this output?

abc
x 
x
y

It compiles and gives no debug errors, but I want the code to change the first char of the string in line_ret to "x" if index==0, and to "y" if index==1. I read here it's not possible to change a single char in what a pointer points to, but what if I don't know how many times I have to copy line_read into line_ret, thus don't know the maximum index size to declare the array line_ret?

Code (EDIT):

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

size_t len = 10;

int main(void){
    char *line_read = malloc(5);
    strcpy(line_read, "abc");
    char **line_ret = malloc(5 * sizeof(char*));
    int index = 0;

    while(index < 2){
        line_ret[index] = realloc(line_ret, 1*len);

        memcpy(&line_ret[index], &line_read, len);
        printf("%s\n", line_ret[index]);

        if(index == 0){
            strcpy(&line_ret[index][0], "x");
        } else if(index == 1){
            strcpy(&line_ret[index][0], "y");
        }
        printf("%s\n", line_ret[index]);

        index++;
    }

    free(line_read);
    free(line_ret);

    return 0;
}
Eli
  • 3
  • 3
  • 1
    That wall of text is unreadable and the very first line of code is already incorrect, if you are allocating a `char **`, the element size is `sizeof(char *)`, not `sizeof(char)`. – FBergo Jul 03 '18 at 22:50
  • If you're looking for help debugging your program, you must post an [MCVE]. Also, what exactly is the problem? Your post has one really long paragraph that is difficult to read, please try applying some punctuation and maybe break it up into multiple paragraphs. – jwdonahue Jul 03 '18 at 22:50
  • @FBergo I tried to describe the situation as I could, I will resize the text and give paragraphs. That was a misspell, sorry about that, I'll fix it now. – Eli Jul 03 '18 at 22:56
  • @jwdonahue the problem is written after the bold word "problem". Tell me if the question is still unclear. I will provide paragraphs now. – Eli Jul 03 '18 at 22:57
  • 1
    Post code that compiles and demonstrates the problem. By the time you strip it down to an MCVE you'll probably have found the defect on your own. – jwdonahue Jul 03 '18 at 23:01
  • @jwdonahue I will provide it soon, thanks. – Eli Jul 03 '18 at 23:05

1 Answers1

0

If you copy addresses (stored in pointers) to several entries in an array, but they all point to the same piece of memory (i.e. the addresses are same) then using the different entries in that array will always result in the same piece of memory being overwritten.

Debug by comparing/printing the addresses you store in the different entries in your array. The bug is where two array entries contain the same address.

To fix, make sure that the entries in the array receive different addresses, i.e. refer to separatly malloced pieces of memory.
This implies that each time you change the index used to access your array you also need to malloc a new piece of memory to use along with that different array entry.

Yunnosch
  • 26,130
  • 9
  • 42
  • 54
  • Uhm, I see, thanks a lot. I tried to realloc right after I enter the if but the process crashes. I'll debug and find some solution. Thanks again. – Eli Jul 04 '18 at 07:25
  • Thanks for (already) accepting. I was actually expecting you to provide more information, ideally an MCVE. – Yunnosch Jul 04 '18 at 07:26
  • Doing some debugging is an excellent idea. If you find something out and add it to the question it might get more, possibly more detailed, help. For some help with debugging, because I really want to help, have a look at these texts: https://ericlippert.com/2014/03/05/how-to-debug-small-programs/ and https://ericlippert.com/2014/03/21/find-a-simpler-problem/ (which I also consider debugging advice). – Yunnosch Jul 04 '18 at 07:29
  • I accepted because you provided me the correct information to proceed with working on the solution. I'm working on a MCVE but, as others pointed out, I may be able to find out the solution while creating it so.. I will edit my question as soon as I will have a MCVE though. – Eli Jul 04 '18 at 07:31
  • True. Making an MCVE often turns out to be an excellent debugging method. ;-) Good luck. – Yunnosch Jul 04 '18 at 07:32
  • @Yunnosh I really appreciate your help, I'm really sorry I couldn't provide an MCVE or a well written description of the problem. I'll read carefully that link, thanks again. – Eli Jul 04 '18 at 07:33