0

I'm working in c++, so I started to make a system that open a sentence in a 2d array of chars. After I trying hard to solve problems with it, I got it working, but not in the way it was supposed.

#include <iostream>
#include <cstring>

using namespace std;

void backword(char* input, char* output[])
{
    for(int i = 0, c = 0; strlen(input)>i; i++)
    {
        if(input[i] == ' ')
        {
            c++;
            i++;
        }
        output[c] += input[i];
    }
    cout << output[1]; //debug
}

int main()
{
    char** output = new char*[30];
    backword("dfs sdfsdfsd dsffsdf", output);
    cout << output[1]; //dubug
    return 0;
}

So whats the issue?: It don't show anything, after my debug it seems my array is not changed

More details:This code I tried in visual studio, not working, tried in Code::Blocks, same result. Initially was intended to return a pointer char**, but I started with this method, and I didn't got any compile errors.

P.S:Sorry if it's a dumb mistake, but I didn't work with chars in this way before.

Thank you.

  • 2
    You never allocate any memory for the pointers held in `output`. – πάντα ῥεῖ Mar 07 '17 at 09:44
  • 2
    As this is C++ you shouldn't use raw char pointers, but you should work with `std::string`. Your way is rather the C way. And you forgot to mention what your program is supposed to do. – Jabberwocky Mar 07 '17 at 09:46
  • I guess you are missing dereference at line `cout << *output[1]; //debug`, then you are passing temporary object to function, I think it is destroyed after function finish and I'm not sure with memory allocation... – Jirka Picek Mar 07 '17 at 09:51
  • 2
    Whatever this program is supposed to do: the `+=` operator on a `char*`does not do what you think. – Jabberwocky Mar 07 '17 at 09:59
  • 1
    you're calculating the length of input again and again which is not good – phuclv Mar 07 '17 at 10:01
  • @LưuVĩnhPhúc correct, but I think that's the least if his problems. – Jabberwocky Mar 07 '17 at 10:02

1 Answers1

1

I am not sure i correctly understand the problem. It seems like you are trying to split the sentence to the array of words.

A little strange you are not using string functions provided by c++, but lets say you have the reasons to do that.

If you want to split the sentence, you should do something like that:

Disclaimer: The solution works for words shorter than 30 symbols, less than 30 words in sentence, memory corruption otherwise...

void backword(char* input, char output[][30])
{
    int index_c = 0;
    int c = 0;
    for (int i = 0;  strlen(input) > i; i++)
    {

        if (input[i] == ' ')
        {
            c++;
            index_c = 0;
        }
        else
            output[c][index_c++] = input[i];
    }

    cout << output[1]; //debug
}

int main()
{
    char output[30][30];
    memset(output, 0, sizeof(output));
    backword("dfs sdfsdfsd dsffsdf", output);
    cout << output[1]; //dubug
    return 0;
}

But i think you better use strings....