Returning Int array pointers from functions 
Author Message
 Returning Int array pointers from functions

Hey All

I've a problem I can't figure out despite consulting my texts, faqs and plain
old trial & error.  

A brief description and then sample code.

I use strstr on a string to find a "keyword".  I then pass that pointer to a
function to parse out the keyword + : + SPACE  (e..g Date: xyz) so that the
return is the value that follows the keyword (e.g. Date: 08 31 and I get 08
31) this works fine.  Then I pass the pointer to that result (i.e. 08 31) to
another function to separate the month from the date and then convert both to
integers.  Everything is just peachy until I need to return the integer array
containing the month & the day (e.g. Date[0]=Month # and Date[1]=Date #) and
actually use the information parsed in my main function.

Now onto the code:

prototypes & variable declarations:

int *Day_Convert(char *Info)
int Deadline[2], *Ptr_2_Deadline;

Ptr_2_Deadline=Deadline;
Ptr_2_Deadline=Day_Convert(char *The_Date_String)
/*  This doesn't work nor does using a pointer below */
printf("(%d, %d) (month, day)\n", Deadline[0],Deadline[1]);
/*   Output tends to be (0,0) (month, day)  and inspection shows a unmodified
array of (0,0) */

int *Day_Convert(char *Info)
{

int When[2], *Ptr_2_When, Index=0;
char Month_Array[3], Day_Array[3];

Ptr_2_When=When;

/** Read in month and then read in day  **/
/**  Data is from a WWW form, so is predictable  **/

for(Index=0;Index<2;Index++)
        {
        Month_Array[Index]=*Info;
        Info++;
/**  BTW Can I safely say Month_Array[Index]=*Info++  ???? */
        }
Info++;

for(Index=0;Index<2;Index++)
        {
        Day_Array[Index]=*Info;
        Info++;
        }

When[0]=atoi(Month_Array);
When[1]=atoi(Day_Array);

/*  When inspecting during debugging sessions, everything works till here  */
/*  I.e. When[0]=8 and When[1]=31 which are correct for the data given  */

return When;

Quote:
}

I've also tried a slightly different approach by using the target for the
return as an argument to the function (i.e. void Day_Convert(char *Info, int
*Day_of_Deadline);

and I get the same results it doesn't seem to work.  

Thanks also better cc via e-mail as I'm not yet confident about my ISPs
ability to collect all the news out there.

Take Care!


Spam {*filter*}y trap!!!

FROM:  Mark Allen Framness


HTTP:   http://www.*-*-*.com/ ~farmer/index.html

To reply via e-mail, delete the given reply to address and delete the NO_SPAM
from the above addresses.
                                        &
All standard disclaimers apply.  Anyone who says likewise is itching for a
fight.



Sun, 12 Dec 1999 03:00:00 GMT  
 Returning Int array pointers from functions

[ snip ]

Quote:
> Now onto the code:

> prototypes & variable declarations:

> int *Day_Convert(char *Info)
> int Deadline[2], *Ptr_2_Deadline;

> Ptr_2_Deadline=Deadline;
> Ptr_2_Deadline=Day_Convert(char *The_Date_String)

Uhhh! this last affectation overrides the previous one. As declared, the
function Day_Convert returns a pointer to int, so the question is :
"Does the value returned point to dynamically allocated memory, or does
it point to some static data ?". In your 2 above statements, you seem to
mix the two points.

Quote:
> /*  This doesn't work nor does using a pointer below */
> printf("(%d, %d) (month, day)\n", Deadline[0],Deadline[1]);
> /*   Output tends to be (0,0) (month, day)  and inspection shows a unmodified
> array of (0,0) */

> int *Day_Convert(char *Info)
> {

> int When[2], *Ptr_2_When, Index=0;
> char Month_Array[3], Day_Array[3];

> Ptr_2_When=When;

> /** Read in month and then read in day  **/
> /**  Data is from a WWW form, so is predictable  **/

> for(Index=0;Index<2;Index++)
>         {
>         Month_Array[Index]=*Info;
>         Info++;
> /**  BTW Can I safely say Month_Array[Index]=*Info++  ???? */

Yes, no problem.

- Show quoted text -

Quote:
>         }
> Info++;

> for(Index=0;Index<2;Index++)
>         {
>         Day_Array[Index]=*Info;
>         Info++;
>         }

> When[0]=atoi(Month_Array);
> When[1]=atoi(Day_Array);

> /*  When inspecting during debugging sessions, everything works till here  */
> /*  I.e. When[0]=8 and When[1]=31 which are correct for the data given  */

> return When;
> }

The problem is here : "When" is an automatic variable, it is allocated
on the stack and its life duration is the scope of the function
Day_Convert. The returned value has no meaning outside the function and
invokes undefined behaviour. If you choose to return a pointer to
dynamically allocated data, you have to change the declaration of the
variable "When", use (m)alloc, and through Ptr_2_When :

       int *When;

       When = malloc(2 * sizeof *When);

Quote:
> I've also tried a slightly different approach by using the target for the
> return as an argument to the function (i.e. void Day_Convert(char *Info, int
> *Day_of_Deadline); and I get the same results it doesn't seem to work.

This is an usual approach and I think it is better in your case, because
it avoids dynamic memory allocation. From this point of view, it is not
to the callee to do the memory allocation, but this is left to the
caller. It can be used when the size of the data is either known at
compile time, or is limited to an (reasonable) upper bound.

Such a function should work :

void Day_Convert(char *Info, int *Day_of_Deadline)
using Deadline instead of When in the function body

Day_Convert(The_Date_String, Deadline);

--
Herve Couppe de Lahongrais (SEU)       | Eurocontrol Experimental Centre



Tue, 14 Dec 1999 03:00:00 GMT  
 Returning Int array pointers from functions


Quote:
>int *Day_Convert(char *Info)
>{

>int When[2], *Ptr_2_When, Index=0;

<snip>

Quote:
>return When;
>}

One problem is returning a pointer to a local array (see section
7.5 of the C FAQ). Instead, pass the address of an int array to
your function and have it store the results there.

Here is a very simplistic example:

#include <stdio.h>

void test( int *a );

int main()
{
   int a[2];

   test( a );
   printf("%d %d\n", a[0], a[1] );

   return 0;

Quote:
}

void test( int *a )
{
   a[0] = 546;
   a[1] = 611;
Quote:
}

--
Scott Allen            



Tue, 14 Dec 1999 03:00:00 GMT  
 Returning Int array pointers from functions

Something like this ought to work:

#include <stdio.h>

void Day_Convert( char *date, int *deadline )
{
  sscanf( date, "%d %d", deadline, deadline + 1 );

Quote:
}

int main(void)
{
  int deadline[2];
  char *date  = "08 31";

  Day_Convert( date, deadline );

Quote:
}

Implement your own error checking.



Quote:
> Hey All

> I've a problem I can't figure out despite consulting my texts, faqs and
plain
> old trial & error.  

> A brief description and then sample code.

> I use strstr on a string to find a "keyword".  I then pass that pointer
to a
> function to parse out the keyword + : + SPACE  (e..g Date: xyz) so that
the
> return is the value that follows the keyword (e.g. Date: 08 31 and I get
08
> 31) this works fine.  Then I pass the pointer to that result (i.e. 08 31)
to
> another function to separate the month from the date and then convert
both to
> integers.  Everything is just peachy until I need to return the integer
array
> containing the month & the day (e.g. Date[0]=Month # and Date[1]=Date #)
and
> actually use the information parsed in my main function.

> (whole buch of code deleted)



Tue, 14 Dec 1999 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Pointer to function returning pointer to function returning...

2. Returning an array of int from a function

3. Returning an array of int from a function

4. int pointer to int array

5. Pointers: return of pointer to array of pointers to main

6. Question about signal()/pointers to functions that return pointers to functions

7. Returning pointer to array of structure from function

8. Arrays, Functions, and Returning Pointers via Parameters

9. function returning pointer array

10. Pointers, Arrays, and function returns

11. Returning and manipulating array/pointer from function to main

12. Function returns pointer to dynamic array

 

 
Powered by phpBB® Forum Software