C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old December 9th, 2005, 09:54 AM
crazyone crazyone is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2005
Posts: 75 crazyone User rank is Corporal (100 - 500 Reputation Level)crazyone User rank is Corporal (100 - 500 Reputation Level)crazyone User rank is Corporal (100 - 500 Reputation Level)crazyone User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 18 h 56 m 9 sec
Reputation Power: 9
Forcing copy of array item

Hi all,

IM currently programming good old ANSI C for a PALM application. My problem is i have a global var in the form of an array containing several structs.

For the sake of simplicity and to save the current settings the user might have entered previously, i make a copy of the current struct he wants to modify (typedef struct {...} medication from the array "normal_medications" to a new standalone var called "current_medication".

Problem is, it seems its copying a pointer of the struct from the array into my standalone variable. Needless to say that this can't work since my main objective is to have a temp copy of this item at hand.

So i tried to copy each element of the struct manually item by item but it still copies pointers to these items. I'm clueless as to what to do. I remember from a long time ago that there is supposed to be an operator in the assignement i can use to force a copy of this item to be passed from the array to my standalone variable.

Here is some code that can help you understand my prob:

MAIN.C
-------

medication normal_start[4];


MAIN.H
-------

//A product chosen in the start portion is described by these params
typedef struct {
Boolean hasBeenInit; //SelfExplains
Boolean substSet; //Since 0 is still valid for subst, we need a flag to make sure it's been set
Boolean stypeSet; //Since 0 is still valid for stype, we need a flag to make sure it's been set
Boolean vaSet; //Since 0 is still valid for va, we need a flag to make sure it's been set
UInt16 subst; //Contains a substance (See defines below)
UInt16 va; //Contains the VA(See defines below)
UInt16 stype; //Contains the stype (See defines below)
char name[32]; //Contains the commercial name of the product chosen
double dosage; //Contains the dose administered
char finalconstr[32]; //Contains the final constructed string
} medication;


MEDFORM.C
-----------
...
...
extern medication normal_start[4];
medication current_medication;
....
....
switch(curMedFormState.substatepos){
case state_main:
current_medication = normal_start[curMedFormState.medicationIndex];
break;
case state_interdose:
current_medication = interdose_start[curMedFormState.medicationIndex];
break;
}
....
....

Reply With Quote
  #2  
Old December 9th, 2005, 10:21 AM
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Apr 2004
Posts: 1,676 Dave Sinkula User rank is First Lieutenant (10000 - 20000 Reputation Level)Dave Sinkula User rank is First Lieutenant (10000 - 20000 Reputation Level)Dave Sinkula User rank is First Lieutenant (10000 - 20000 Reputation Level)Dave Sinkula User rank is First Lieutenant (10000 - 20000 Reputation Level)Dave Sinkula User rank is First Lieutenant (10000 - 20000 Reputation Level)Dave Sinkula User rank is First Lieutenant (10000 - 20000 Reputation Level)Dave Sinkula User rank is First Lieutenant (10000 - 20000 Reputation Level)Dave Sinkula User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 1 Month 3 Days 8 h 23 m 46 sec
Reputation Power: 132
Structure assignment should make a bytewise copy. I don't supposed you could strip the issue down to a canned code sample that demonstrates the problem? (That is, make some example that is not scattered in separate files with some example data and perhaps a few printf's -- the answer may pop out at you while doing so, and if not it would be easier for others here to try to compile it and give you an answer.)

[edit]For example,
Code:
#include <stdio.h>

struct T
{
   short    changed;
   unsigned check;
   double   digits;
   char     broiled[20];
};

struct T array[] =
{
   {42, 12345, 67.89, "Element 0"},
   {-1, 23456, 78.91, "Element 1"},
   {10, 34567, 89.12, "Element 2"},
   {-5, 45678, 90.23, "Element 3"},
};

void print(struct T *item)
{
   printf("changed = %hd, check = %u, digits = %g, broiled = %s\n",
          item->changed, item->check, item->digits, item->broiled);
}

int main()
{
   size_t i;
   for ( i = 0; i < sizeof array / sizeof *array; ++i )
   {
      struct T copy = array[i];
      print(&copy);
   }
   return 0;
}

/* my output
changed = 42, check = 12345, digits = 67.89, broiled = Element 0
changed = -1, check = 23456, digits = 78.91, broiled = Element 1
changed = 10, check = 34567, digits = 89.12, broiled = Element 2
changed = -5, check = 45678, digits = 90.23, broiled = Element 3
*/
__________________
Any advertisement triggered by IntelliTxt in this post is not endorsed by the author of this post.

Last edited by Dave Sinkula : December 9th, 2005 at 10:35 AM.

Reply With Quote
  #3  
Old December 9th, 2005, 04:33 PM
crazyone crazyone is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2005
Posts: 75 crazyone User rank is Corporal (100 - 500 Reputation Level)crazyone User rank is Corporal (100 - 500 Reputation Level)crazyone User rank is Corporal (100 - 500 Reputation Level)crazyone User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 18 h 56 m 9 sec
Reputation Power: 9
For some reason i don't know, i changed nothing at all in the code except the index of the array which was 1 based at the time but since being 1 based in a 4 item array, the item #1 wasnt technically wrong... but since i changed it to a 0 based array (changed my for loop) everything worked... strange...

CrazyOne

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Forcing copy of array item

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap