Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Re: Pointer to structure Problems

Reply
Thread Tools

Re: Pointer to structure Problems

 
 
Robert W Hand
Guest
Posts: n/a
 
      07-17-2003
On Thu, 17 Jul 2003 11:11:26 +0100, "Steven Graham" <(E-Mail Removed)>
wrote:

> What is
>going on. BTW this little psudo-C code summarises my problem. (main is at
>the bottom if you want to start following from there)

<snipped pseudocode>

Pseudocode rarely helps in these type of problems. I have fleshed out
your code into something compilable by Comeau. I've included the
output below.

typedef struct
{
char item1;
int item2;
} A_STRUCT;

void function2(A_STRUCT *dest_struct)
{
//Put these into the strict that was called by reference
dest_struct->item1 = 'A';
dest_struct->item2 = 1000;

printf("f2: %c\n", dest_struct->item1); //Yey it changed!
}

void function1(void)
{
A_STRUCT my_struct = {'B', 1};
printf("f1: %c\n", my_struct.item1);
// This shows my original non-junk values

//now populate the struct
function2(&my_struct);

printf("f1: %c\n", my_struct.item1);
// It has have changed!!
}



int main(void)
{
function1();
return 0;
}

Output:
f1: B
f2: A
f1: A

It seems to work fine here. I would avoid the undefined behavior that
you invoked by not initializing your structure. Is there a difference
between your real code and my code?

Best wishes,

Bob
 
Reply With Quote
 
 
 
 
Steve Graham
Guest
Posts: n/a
 
      07-17-2003
Not sure what was cauing the problem, my program wasn't a simle DOS app
(custom startup code, loaded from a boot sector etc), what I have done is
create a global struct in the headder file and just using that in all
functions. Not as ellagant but it works. There could be an underlining
problem with this but I can't find the cause.

Thanks for your suggestions and your help
Steven Graham


 
Reply With Quote
 
 
 
Reply

Thread Tools

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Simple structure and copying data to pointer of the same structure A C++ 27 04-16-2011 11:07 PM
Pointer to pointer Vs References to Pointer bansalvikrant@gmail.com C++ 4 07-02-2009 10:20 AM
pointer to structure from pointer to member sieg1974@yahoo.com C Programming 6 08-27-2006 01:20 AM
passing the address of a pointer to a func that doesnt recieve a pointer-to-a-pointer jimjim C Programming 16 03-27-2006 11:03 PM
Pointer-to-pointer-to-pointer question masood.iqbal@lycos.com C Programming 10 02-04-2005 02:57 AM



Advertisments