CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 13 of 13

Threaded View

  1. #1
    Join Date
    Mar 2011
    Location
    Delhi India
    Posts
    110

    Lightbulb what is bug in this code

    see it there is a bug in returning from function because pointer value in function and main is different

    #include<iostream>
    #include<cstring>
    using namespace std;
    int* space(char[]);
    char* answer(char[]);
    int main(void)
    {
    char arraym[100];
    cout<<"Input string(max 100 character and max 10spaces):\n";
    cin.getline(arraym,100,'\n');
    int *pa=space(arraym);
    for(int i=0;i<10;i++)
    cout<<"space location"<<(pa[i])<<endl;
    return 0;
    }
    int* space(char arrays[])
    {
    int p=0;
    int ans[10]={0};
    int* pans=ans;
    cout<<arrays<<endl;
    // int tmp=sizeof(arrays)/sizeof(arrays[0]);
    for(unsigned int i=0;i<(strlen(arrays));i++)
    {
    cout<<"\n*****"<<" ";
    if(arrays[i]==' ')
    {
    cout<<"SPACE FOUND AT"<<i<<"th LOCATION"<<endl;
    pans[p]=i;
    p++;
    }
    }
    for(int i=0;i<10;i++)
    {
    cout<<"result in function body"<<pans[i]<<endl;
    }
    return pans;
    }

    in simple words my question is why value of pans and pa is different give any suggestion to make their value same.

    I am making a program which returns the number of words seprately on every next call. It is half of that program .
    What is problem in this program? when i compile and run value given by pans in function is different from the pa but i think these should same since function return pans which is transfered to pan.

    Thanks for replying.
    Last edited by vkash; May 25th, 2011 at 04:55 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured