Thread: using a structure..

  1. #16
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>Does this make sense?
    Now it does. Although I'm unclear on exactly how "another application" feeds data into the vector, I'll assume it's not relevant to the problem. On the other hand, why can't you just directly modify the elements in the vector? Seems like a waste to copy them out and then copy them back in. And have you considered my suggestion for why you might be getting garbage values yet?
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  2. #17
    Registered User
    Join Date
    Dec 2004
    Posts
    77
    I think I can simplify the problem. When I copy the data from a vector into a local structure, the data looks great in the local variables that are contained in the structure. The problem is this. In the same file in another procedure I am trying to use this local structure and copy the data. When I do this the data is all garbage (int's are equal to the max negative value ot an int type, etc, etc). I'm losing the data because it is local to the other procedure where I declared it. How can I fix this?

  3. #18
    Registered User
    Join Date
    Nov 2004
    Posts
    49
    Hmmm, Im not a great programmer at all. But I think you tring to take data from a Vector, Modify the data. Then copy the data back to the vector? Maybe the values are garbage cause when you modified it, you curropted the data. The only way I can see it containing garbage is if you modified it the wrong way, you using uninitialized variables to modify the data, You are not accessing the real data cause you using pointer stuff or array stuff that is at the wrong location. Those are my thoughts on the matter. Not sure how to help with the code. I suck at programming lol. Hope you solve your problem. Im unsure how helpful everyone can be without seeing the real code though.

    Is this code any close to what you were tring to do?

    Code:
    #include <iostream>
    using namespace std;
    struct names_a {
    	names_a();
    static int number_of_names;
    char name[10];
    };
    int names_a::number_of_names=0;
    names_a::names_a(){
    	number_of_names++;
    }
    struct names_b {
    names_b();
    static int number_of_names;
    char name[10];
    };
    int names_b::number_of_names=0;
    names_b::names_b(){
    	number_of_names++;
    }
    
    int main(){
    names_a Kim;
    names_b Modify_Kim;
    for(int i=0;i<10;i++){
    Kim.name[i]=NULL;
    }
    Kim.name[0]='K';
    Kim.name[1]='i';
    Kim.name[2]='m';
    for(int i=0;i<10;i++){
    Modify_Kim.name[i]=NULL;
    }
    for(int i=0;i<10 && Kim.name[i]!= NULL ;i++){
    Modify_Kim.name[i]=Kim.name[i];
    }
    Modify_Kim.name[0]='H';
    for(int i=0;i<10;i++){
    Kim.name[i]=Modify_Kim.name[i];
    }
    for(int i=0;i<10 && Kim.name[i]!=NULL ;i++){
    cout<<Kim.name[i];
    }
    cout<<endl;
    cout<<Kim.number_of_names;
    system("PAUSE");
    return 0;
    }

  4. #19
    Registered User
    Join Date
    Dec 2004
    Posts
    77
    no this is not whta's happening at all. The data looks good when I copy it from the vector into the structure. I'll try to post code later. thanks!

  5. #20
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    First and foremost, using two structures with identical members sounds suspicious because I get the impression you're trying to do something like this when you say you're copying from one to the other:
    Code:
    struct names_a a;
    struct names_b b;
    
    fill_a(&a);
    b = a; // Big no-no
    Two instances of two structures are never compatible, even if they appear to be declared the same way. That's my first question about your playing with two structure types for the same data.

    As I understand it, this is your problem: You have a vector of name_a objects that are already filled with valid data. You want to take each of these objects and copy the data into a name_b object presumably because you have a function that only works with the name_b type. Then you want to copy the modified data back into the original vector of name_a objects.

    If that's a correct assessment of your issue, you have no choice but to copy data manually for a conversion:
    Code:
    name_a makea(name_b& b)
    {
      name_a ret;
    
      ret.number_of_names = b.number_of_names;
      strcpy(ret.name, b.name);
    
      return ret;
    }
    
    name_b makeb(name_a& a)
    {
      name_b ret;
    
      ret.number_of_names = a.number_of_names;
      strcpy(ret.name, a.name);
    
      return ret;
    }
    
    ...
    
    for (int i = 0; i < vsize; i++) {
      name_a a = makea(modify_data(makeb(v[i])));
      v[i] = a;
    }
    My best code is written with the delete key.

  6. #21
    Registered User
    Join Date
    Dec 2004
    Posts
    77
    thanks Prelude! I already fixed the problem.

  7. #22
    Registered User
    Join Date
    May 2003
    Posts
    82
    for the record, isn't prelude a guy?

  8. #23
    Registered User
    Join Date
    Dec 2004
    Posts
    77
    I just went by the picture Prelude has under the profile. I could be mistaken and if so, I do apologize to Prelude. No offense intended.

  9. #24
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>for the record, isn't prelude a guy?
    No, but you're not the first person to ever think so

    @JerryKelleyJr: Out of curiosity, what was the problem?
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  10. #25
    Registered User
    Join Date
    Dec 2004
    Posts
    77
    hunter...The problem was me. I did not state the problem clearly...this is due to my lack of experience. I sat down and gave what I was really trying to accomplish more thought. The solution was to copy one buffer to another using memcpy. See my next post on "memcpy use?" Off Topic, I didn't think Prelude was a guy...afterall there are a lot of masculine looking women who are very attractive...not that I'm saying Prelude's appearance is masculine.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem referencing structure elements by pointer
    By trillianjedi in forum C Programming
    Replies: 19
    Last Post: 06-13-2008, 05:46 PM
  2. Replies: 5
    Last Post: 02-14-2006, 09:04 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Serial Communications in C
    By ExDigit in forum Windows Programming
    Replies: 7
    Last Post: 01-09-2002, 10:52 AM
  5. C structure within structure problem, need help
    By Unregistered in forum C Programming
    Replies: 5
    Last Post: 11-30-2001, 05:48 PM