0

I have defined the struct chararray as follows.

struct chararray
{
    char* lhs;
    char* rhs;
};

Now, I have two arrays la[l] and ra[l](both have all l values defined). I assign that to the struct,and return the function.

chararray s;
s.lhs = new char[l];
s.rhs = new char[l];
s.lhs = &la[0]; 
s.rhs = &ra[0];
return s;

In main , I ouptut

for(int i = 1; i < l;i++){
    cout << *(s.rhs + i);
}

Now,I get gibberish as output. But if I output *(s.rhs + 0) and *(s.rhs + 1) and ... [ the values 0,1,.. are explicitly included ], I get correct output!

Why doesnt the for loop work?

PS - My entire code is -

 #include <iostream>
#include <cstring>
#include <vector>
#include <cmath>

using namespace std;

struct chararray
{
    char* lhs;
    char* rhs;
};

chararray getcenter(char in[],int l){
    int open_count = 0;
    int open = 0;
    int first = 0;
    int last = 0;
    int limit;
    int close_count = 0;
    char lhs[l],rhs[l];
    for(int i = 0; i < l;i++){
        lhs[i] = ' ';
        rhs[i] = ' ';
    }
    for(int i = 0; in[i]!='\0';i++){
        int flag = 0;
        if(in[i] == '('){
            if(flag == 0){first = i;flag = 1;}
            open++;
        }
        if(in[i] == '('){
            last = i;
        }
        limit = i;
    }
    for(int i = 0; in[i]!='\0';i++)
    {
        //cout << "entrt" << endl;
        int temp;
        if(in[i] == '(') { open_count++; }
        if(in[i] == ')') { close_count++; }
        if((open_count == close_count) && (open_count != 0) && (open_count != open))
        {
            //cout << open_count << endl;
            for(int j = 0;j < i+1;j++)
            {
                lhs[j] = in[j];
            }

            lhs[i+1] = ' ';
            lhs[i+2] = ' ';
            for(int j = i+3; in[j]!='\0';j++)
            {
                lhs[j] = ' ';
                rhs[j-i-3] = in[j];//Assuming no space between -> and rhs
                temp = j;
            }
            for(int j = temp;rhs[j] != '\0';j++){
                rhs[j] = ' ';
            }
            for(int j = temp;lhs[j] != '\0';j++){
                lhs[j] = ' ';
            }
            break;
        }
    }
    chararray s;
    s.lhs = new char[l];
    s.rhs = new char[l];
    s.lhs = &lhs[0];    
    s.rhs = &rhs[0];
    return s;
}

int main()
{

    string input;
    cin >> input;
    int l=input.length()+1;
    char in[l];
    strcpy(in, input.c_str());
    chararray s = getcenter(in,l);
    for(int i = 1; i < l;i++){
        //cout << *(s.rhs + i); - Doesnt work
    }
    cout << *(s.lhs + 5); // Works!

    return 0;
}
  • 1
    In the statement `s.lhs = &lhs[0]`, what is `lhs` on the right side of `=`? That's the important part, since you're taking its address. Please show a complete example including the definition of this `lhs`. – Tyler McHenry Mar 26 '14 at 04:41
  • nope. lhs[l] is an array defined inside the function. Dont confuse between their names,they are entirely different! – user2381205 Mar 26 '14 at 04:42
  • 1
    What is `l` equal to (in the second part of the for loop)? Also, why does `i` start at 1 and not 0? Where are `lhs` and `rhs` defined? What are you overwriting `s.lhs` and `s.rhs` (which will cause memory leaks)? – Dithermaster Mar 26 '14 at 04:43
  • Then how are we suppose to know what lhs and rhs is...? – Ben Mar 26 '14 at 04:43
  • You don't really mean `s.lhs = &lhs[0];` do you? it throws away the array you just created. You really mean `s.lhs[0] = lhs[0];` don't you? – Jerry Jeremiah Mar 26 '14 at 04:47
  • they are just arrays of length l. I have written it - – user2381205 Mar 26 '14 at 04:47
  • Now, I have two arrays lhs[l] and rhs[l](both have all l values defined) – user2381205 Mar 26 '14 at 04:48
  • It can start at 0 also. It was just a parsing requirement to not consider the first character. – user2381205 Mar 26 '14 at 04:48
  • I'll edit the code.Now,there wont be any confusino – user2381205 Mar 26 '14 at 04:49
  • possible duplicate of [Can a local variable's memory be accessed outside its scope?](http://stackoverflow.com/questions/6441218/can-a-local-variables-memory-be-accessed-outside-its-scope) – Raymond Chen Mar 26 '14 at 04:57
  • NO! It has not been defined in the local memory. So,no question of being out of scope! ( I am returning the struct,rem! ) and it is outputting correctly,if I use a variable instead of a number! – user2381205 Mar 26 '14 at 05:01
  • Try answering it,if you think its that trivial. – user2381205 Mar 26 '14 at 05:01

1 Answers1

0

Your for loop is starting at i=1, and when you explicitly set the values you are using 0 and 1. Decide where you want to start, and try it again.

Try this:

for(int = 0 ; i < l; i++)
    cout << *(s.rhs + i);

Note that you are actually risking a memory leak with these lines of code:

s.lhs = new char[l];
s.rhs = new char[l];
s.lhs = &lhs[0];    
s.rhs = &rhs[0];

You are allocating 1 char and storing its address in s.lhs, then resetting s.lhs to the address of lhs[0]. There is no need for the two first lines with the new in them. So replace that code with this: (also note that &lhs[0] is the same as lhs)

s.lhs = lhs;
s.rhs = rhs;
brettwhiteman
  • 4,210
  • 2
  • 29
  • 38
  • I am going on. I want all values upto l. Manually specifying them gives me correct answer. but using for loop to do that for me,doesnt. – user2381205 Mar 26 '14 at 04:50
  • The first element of an array has an offset of 0, so `i` should start at 0, should it not? So, try using `i=0`. – brettwhiteman Mar 26 '14 at 04:52