NO

Author Topic: returning the contents of a structure pointer from a function  (Read 8953 times)

tpekar

  • Guest
Can someone tell me how to return the allocated contents of a pointer within a structure from a function back to main?  Here is my code.

Code: [Select]
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct temp {
  char part1[3];
  char part2[5];
  char *ptr;
} test;
char source[129];
main() {
sub(test);
printf("\ntest.ptr=%s|",test.ptr);
}

sub(struct temp test1) {
test1.ptr=malloc(10);
strcpy(test1.ptr,"contents");
printf("\ntest1.ptr=%s|",test1.ptr);
}

Many thanks!
« Last Edit: July 28, 2012, 07:38:14 AM by Stefan Pendl »

CommonTater

  • Guest
Re: returning the contents of a structure pointer from a function
« Reply #1 on: July 27, 2012, 11:02:08 PM »
Can someone tell me how to return the allocated contents of a pointer within a structure from a function back to main?  Here is my code.

First lets format your code so it's readable...
Code: [Select]
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct temp {
  char part1[3];
  char part2[5];
  char *ptr;
} test;

char source[129];

main()
{
sub(test);
printf("\ntest.ptr=%s|",test.ptr);
}

sub(struct temp test1)
{
test1.ptr=malloc(10);
strcpy(test1.ptr,"contents");
printf("\ntest1.ptr=%s|",test1.ptr);
}

First ... your program structure is all wrong...
it's int main (void) and the function returns an integer to the OS...
Code: [Select]
int main (void)
  {
     int ErrorLevel = 0;
 
     // whatever you're doing
 
    return ErrorLevel;
  }

Second your subroutine will not be recognized by the compiler... It reads top down so you have two choices... either place the function above main or add a prototype for the function above main.
 
Like this...
Code: [Select]
#include <stdio.h>
 
int Add (int a, int b)
  {
     int c;
 
     c = a + b;
     
     return c;
  }
 
int main (void)
  {
     int ErrorLevel = 0;
     int x;
 
     x = Add (11, 31);
     
    return ErrorLevel;
  }

or like this...
 
Code: [Select]
#include <stdio.h>
 
int Add (int a, int b);
 
int main (void)
  {
     int ErrorLevel = 0;
     int x;
 
     x = Add (11, 31);
     
    return ErrorLevel;
  }
 
 
int Add (int a, int b)
  {
     int c;
 
     c = a + b;
     
     return c;
  }

You can see in the function how to return a value so that should also answer your question...
 

tpekar

  • Guest
Re: returning the contents of a structure pointer from a function
« Reply #2 on: July 27, 2012, 11:26:17 PM »
I think I misspoke.  Its not a return value I'm interested in.  But the pointer that is allocated to the value "contents" in the function has a value of void when it returns back to main.  I want to know how a pointer in a structure can be allocated in a function and still point to the value given in the function when the program returns control back to main. 

tpekar

  • Guest
Re: returning the contents of a structure pointer from a function
« Reply #3 on: July 27, 2012, 11:38:00 PM »
Here is the code in a more readable form that illustrates my problem.

Code: [Select]
#include <stdlib.h>
#include <string.h>
struct temp {
  char part1[3];
  char part2[5];
  char *ptr; // pointer
} test;

char source[129];

void sub(struct temp test1);

int main(int argc, char *argv[]) {
sub(test);
printf("\nback to main -> test.ptr=%s|",test.ptr); // this is null
}

void sub(struct temp test1) {
test1.ptr=malloc(10);
strcpy(test1.ptr,"contents");
printf("\nin function sub -> test1.ptr=%s|",test1.ptr); // this print 'contents'
}

« Last Edit: July 28, 2012, 07:39:36 AM by Stefan Pendl »

CommonTater

  • Guest
Re: returning the contents of a structure pointer from a function
« Reply #4 on: July 28, 2012, 12:31:38 AM »
Here is the code in a more readable form that illustrates my problem.

That's not the same code you started with.

Anyhow, the problem is that test is a global struct.  When you pass it into the function you are making a COPY of the struct that is only valid inside the function.  The copy outside the function is NOT changed by this. 
 
Also when you use malloc() on the copy inside the function, that copy is destroyed when the function exits; the pointer is lost, the memory is not released and your program is leaking memory as a result.
 
If you want this to work you have two choices... either pass a pointer to the struct into the function and operate on the prime copy --or-- return the copy of the struct back out of the function to your main procedure. 
 
 


EDIT... actually there is a third option.  Since the struct is global, you can operate on it directly inside the function without passing it in.  Passing a copy in actually hides the one at global scope.
 
« Last Edit: July 28, 2012, 01:27:35 AM by CommonTater »

JohnF

  • Guest
Re: returning the contents of a structure pointer from a function
« Reply #5 on: July 28, 2012, 01:56:00 PM »
tpekar,

This is the way I would do it.

Code: [Select]
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct TEMP{
char part1[3];
char part2[5];
char *ptr; // pointer
};

void sub(struct TEMP * test);

int main(void)
{
struct TEMP test;
sub(&test);
printf("back to main -> test.ptr = %s\n", test.ptr);
free(test.ptr);
        return 0;
}

void sub(struct TEMP * test1)
{
test1->ptr = malloc(10);
strcpy(test1->ptr, "contents");
printf("in function sub -> test1->ptr = %s\n", test1->ptr);
}

EDIT: added return 0; to main and included stdio.h

John

« Last Edit: July 29, 2012, 03:52:03 PM by JohnF »

CommonTater

  • Guest
Re: returning the contents of a structure pointer from a function
« Reply #6 on: July 28, 2012, 04:49:53 PM »
tpekar,

This is the way I would do it.

Me too, with one small change....

Code: [Select]
#include <stdlib.h>
#include <string.h>

struct TEMP{
   char part1[3];
   char part2[5];
   char *ptr;   // pointer
};

void sub(struct TEMP * test);

int main(void)
{
   struct TEMP test;
   sub(&test);
   printf("back to main -> test.ptr = %s\n", test.ptr);
   free(test.ptr);
   
   return 0;    // for the os
}

void sub(struct TEMP * test1)
{
   test1->ptr = malloc(10);
   strcpy(test1->ptr, "contents");
   printf("in function sub -> test1->ptr = %s\n", test1->ptr);   
}

Only thing is --and in all respect for you my friend-- tpekar won't learn from ready to use answers.  He'll just copy and paste your solution and never give it another thought...  I call it "scoop and poop coding".  Sometimes I think we're to eager to solve the problem and forget to teach... ( and now I'm sitting here thinking about all the things you and Frankie and Ralf could teach me...  :D )


« Last Edit: July 28, 2012, 04:53:49 PM by CommonTater »

JohnF

  • Guest
Re: returning the contents of a structure pointer from a function
« Reply #7 on: July 28, 2012, 05:08:59 PM »
You are right I forgot the return.

However I think newbies need a helping hand to start with - they can be put off by a too critical reply. I've seen a few lately that have received a less than helpful start.

John



CommonTater

  • Guest
Re: returning the contents of a structure pointer from a function
« Reply #8 on: July 28, 2012, 05:58:57 PM »
You are right I forgot the return.

However I think newbies need a helping hand to start with - they can be put off by a too critical reply. I've seen a few lately that have received a less than helpful start.

At the risk of hijacking the thread...

Your point is taken.  What would you suggest as a happy medium? 

I don't want to be doing these guy's homework for them --that is almost sabotage, since they don't learn-- but at the same time I do want to help out.  I've always taken the tack of getting them to think about the problem and do a little reading to find their answers but perhaps a somewhat different tack would be more helpful.

FWIW... My experience has been that those who actually want to learn are mostly looking for a nudge in the right direction.  They will actually look forward to the challenge of understanding the problem (a programmer's best skill) but aren't sure where to start looking.

JohnF

  • Guest
Re: returning the contents of a structure pointer from a function
« Reply #9 on: July 28, 2012, 08:03:46 PM »
To presume a newbie will not learn at the first hurdle is a little presumptuous.

If however he/she continues to make the same mistakes, in other words does not learn then a more strident approach would seem appropriate.

EDIT: I still remember when I was learning!

John
« Last Edit: July 28, 2012, 08:05:20 PM by JohnF »

CommonTater

  • Guest
Re: returning the contents of a structure pointer from a function
« Reply #10 on: July 28, 2012, 10:21:16 PM »
To presume a newbie will not learn at the first hurdle is a little presumptuous.

If however he/she continues to make the same mistakes, in other words does not learn then a more strident approach would seem appropriate.

EDIT: I still remember when I was learning!

Hmmm... being self-educated, first in Pascal now in C... I wonder if there isn't a difference in learning experiences I should be taking into account.  In any case I started a thread in general discussions about homework policy (etc) and would very much appreciate seeing what others think...

Thanks for the responses.  We should most likely let this get back to tpekar's problem...

tpekar

  • Guest
Re: returning the contents of a structure pointer from a function
« Reply #11 on: July 30, 2012, 03:14:40 PM »
JohnF and CommonTator,

First, let me thank you for your very helpful comments and especially, examples.  I find an example worth a thousand words.  Going to the forum is always a last resort for me.  Its after I have tried everything I can think of.  The actual program is much more complex than the example I put out there.  But I wanted to illustrate the problem in the simplest manner possible.

For what its worth, let me mention that I, too, am self taught in C.  One of the things I struggled with for months was the difference between the single quote and the double quote.  Then, casually in a class I was in, I asked another C programmer who explained the difference to me.  In less than a minute I got the answer I struggled with for months.

So, now being faced with learning the Windows API, I have learned to overcome my stubborn "I'll do it myself" attitude.  I think the reason we have teachers is to shorten the learning curve.  Now, if I could just get my kids to listen to what I tell them.

CommonTater

  • Guest
Re: returning the contents of a structure pointer from a function
« Reply #12 on: July 30, 2012, 07:04:50 PM »
JohnF and CommonTator,
First, let me thank you for your very helpful comments and especially, examples.

No worries... I think you would do well to read up on C's rules of scope.  That appears to be what you are tripping over.

Quote
I find an example worth a thousand words. 

Unfortunately many people ignore the thousand words and simply copy the examples as a means to avoid having to think or learn and we have no way to know the difference... (Actually my crystal ball is in for an upgrade)

Quote
So, now being faced with learning the Windows API, I have learned to overcome my stubborn "I'll do it myself" attitude.  I think the reason we have teachers is to shorten the learning curve.  Now, if I could just get my kids to listen to what I tell them.

First make extra double certain sure you are comfortable in console mode... You will need a better than average understanding of pointers, scopes and structs if you are to get very far in Windows API.

TheForger's tutorial is going to be your best starting point... of course there are more advanced books you should also study page by page.... The secret is to NOT try to make this new information fit into the old way of doing things.  Windows GUI mode programming is an entire horse of a different colour, so be ready to learn from scratch.
 
Good luck on your journeys...