pointer problem - passing contents back to calling function. 
Author Message
 pointer problem - passing contents back to calling function.

Here is a simplification of a problem I'm having with pointers to
structures. I'm trying to:

1. Pass a pointer from main() to function xxx()
2. allocate memory from xxx()
3. assign values to the newly allocated memory
4. have these new values passed back to main() and the "head" pointer
5. I dont want to use the return() function to achieve this and I
dont' want to use a global variable as my first link.

I did manage to get a fix of sorts by using **ptr in the xxx()
function but couldn't work out the syntax of the scanf using that
method.

struct data
{
   int number;
   struct data *next;

Quote:
};

typedef struct data  RECORD;

void xxx(RECORD *);

int main(void)
{
   RECORD *head = NULL;

   xxx(head);

   return(0);

Quote:
}

void xxx(RECORD *ptr)
{
  ptr = (RECORD *)malloc(sizeof(RECORD));

  scanf("%d", &ptr->number);
  ptr->next = NULL;

Quote:
}



Thu, 24 Aug 2000 03:00:00 GMT  
 pointer problem - passing contents back to calling function.

[...]

Quote:
>1. Pass a pointer from main() to function xxx()
>2. allocate memory from xxx()
>3. assign values to the newly allocated memory
>4. have these new values passed back to main() and the "head" pointer
>5. I dont want to use the return() function to achieve this and I
>dont' want to use a global variable as my first link.

>I did manage to get a fix of sorts by using **ptr in the xxx()
>function but couldn't work out the syntax of the scanf using that
>method.

[...]

Okay, I snipped a lot since you were on the right track already. Just
use ...

Quote:
>  scanf("%d", &ptr->number);

   scanf("%d", &(*ptr)->number);
... here.

Bye, Juergen

--
\ Real name     : Juergen Heinzl     \       no flames      /

  \ Phone Private : +49 911-4501186    \                  /



Thu, 24 Aug 2000 03:00:00 GMT  
 pointer problem - passing contents back to calling function.

: Here is a simplification of a problem I'm having with pointers to
: structures. I'm trying to:

: 1. Pass a pointer from main() to function xxx()
: 2. allocate memory from xxx()
: 3. assign values to the newly allocated memory
: 4. have these new values passed back to main() and the "head" pointer
: 5. I dont want to use the return() function to achieve this and I
: dont' want to use a global variable as my first link.

: I did manage to get a fix of sorts by using **ptr in the xxx()
: function but couldn't work out the syntax of the scanf using that
: method.

That's what you need to do; pass a pointer to head to the function,
and update whatever that pointer points to.  Hence scanf() would b:
  scanf("%d", &(*ptr)->number);

Will

: struct data
: {
:    int number;
:    struct data *next;
: };

: typedef struct data  RECORD;

: void xxx(RECORD *);

: int main(void)
: {
:    RECORD *head = NULL;

:    xxx(head);

:    return(0);
: }

: void xxx(RECORD *ptr)
: {
:   ptr = (RECORD *)malloc(sizeof(RECORD));

:   scanf("%d", &ptr->number);
:   ptr->next = NULL;
: }



Thu, 24 Aug 2000 03:00:00 GMT  
 pointer problem - passing contents back to calling function.

Quote:

>struct data
>{
>   int number;
>   struct data *next;
>};

>typedef struct data  RECORD;

>void xxx(RECORD *);

>int main(void)
>{
>   RECORD *head = NULL;

>   xxx(head);

>   return(0);
>}

>void xxx(RECORD *ptr)
>{
>  ptr = (RECORD *)malloc(sizeof(RECORD));

>  scanf("%d", &ptr->number);
>  ptr->next = NULL;
>}

I think it should be RECORD *xxx(void)   or void xxx(RECORD **ptr).
'head' will not be modified with your current implementation.

Aloha



Fri, 25 Aug 2000 03:00:00 GMT  
 pointer problem - passing contents back to calling function.


: [...]
: >1. Pass a pointer from main() to function xxx()
: >2. allocate memory from xxx()
: >3. assign values to the newly allocated memory
: >4. have these new values passed back to main() and the "head" pointer
: >5. I dont want to use the return() function to achieve this and I
: >dont' want to use a global variable as my first link.
: >
: >I did manage to get a fix of sorts by using **ptr in the xxx()
: >function but couldn't work out the syntax of the scanf using that
: >method.
: [...]

: Okay, I snipped a lot since you were on the right track already. Just
: use ...
: >  scanf("%d", &ptr->number);
:    scanf("%d", &(*ptr)->number);
: ... here.

: Bye, Juergen

: --
: \ Real name     : Juergen Heinzl     \       no flames      /

:   \ Phone Private : +49 911-4501186    \                  /

Are you got it right ?

The function xxx simply won't do it for ptr is of local scope. One idea is
to put the malloc right before the call to xxx and a free after the call.

Any better idea ?

Regards,

/ming



Sat, 26 Aug 2000 03:00:00 GMT  
 pointer problem - passing contents back to calling function.

Quote:

> Here is a simplification of a problem I'm having with pointers to
> structures. I'm trying to:

> 1. Pass a pointer from main() to function xxx()
> 2. allocate memory from xxx()
> 3. assign values to the newly allocated memory
> 4. have these new values passed back to main() and the "head" pointer
> 5. I dont want to use the return() function to achieve this and I
> dont' want to use a global variable as my first link.

> I did manage to get a fix of sorts by using **ptr in the xxx()
> function but couldn't work out the syntax of the scanf using that
> method.

> struct data
> {
>    int number;
>    struct data *next;
> };

> typedef struct data  RECORD;

> void xxx(RECORD *);

> int main(void)
> {
>    RECORD *head = NULL;

>    xxx(head);

>    return(0);
> }

> void xxx(RECORD *ptr)
> {
>   ptr = (RECORD *)malloc(sizeof(RECORD));

>   scanf("%d", &ptr->number);
>   ptr->next = NULL;
> }

This will modify the passed in RECORD pointer and
access the structure member.

Remember that with a pointer to a pointer it is the
contents of the pointer, that is the address of the
object you are interested in.

ptr --PointsTo--> AnAddress --ThatPointsTo--> an object
                  ^^^^^^^^^
     you need this address (*ptr) to
     access items in your structure.

void xxx(RECORD **);

 int main(void)
 {
    RECORD *head = NULL;

    xxx(&head);

    return(0);
 }

 void xxx(RECORD **ptr)
 {
   *ptr = (RECORD *)malloc(sizeof(RECORD));

   printf ("Enter a record number: ");
   scanf("%d",&((*ptr)->number));
   printf ("Entered value is %d\n", (*ptr)->number);
   (*ptr)->next = NULL;
 }

--
 _______________________________________________________________

       "Make things as simple as possible-but no simpler."
                                            -- ALBERT EINSTEIN
 _______________________________________________________________



Mon, 28 Aug 2000 03:00:00 GMT  
 pointer problem - passing contents back to calling function.

[SNIP]

Quote:
> Here is a simplification of a problem I'm having with pointers to
> structures. I'm trying to:

> 1. Pass a pointer from main() to function xxx()
> 2. allocate memory from xxx()
> 3. assign values to the newly allocated memory
> 4. have these new values passed back to main() and the "head" pointer
> 5. I dont want to use the return() function to achieve this and I
> dont' want to use a global variable as my first link.
[SNIP]
> I did manage to get a fix of sorts by using **ptr in the xxx()
> function but couldn't work out the syntax of the scanf using that
> method.

[SNIP]

Hello Ann,

Pointer to pointer to record ! What a mouthful ! I am glad you found a
solution to your problem :) I had a go at your problem too and came up
with a slightly different program. Perhaps you may find something useful
in what I have written.

/* ann.c - a program for Ann */

/* Library */
#include < stdio.h >
#include < stdlib.h >

/* Preprocessor Directive */
#define MAX 100

/* Structure Prototype */
struct data{
    int number;
    struct data *next;

Quote:
};

/* Type Definition */
typedef struct data RECORD;
typedef enum{ FAILURE, SUCCESS } status;

/* Function Prototype */
status insert( RECORD **theHeadPtr );

/* Console */
int main( void ){
    /* declare variable */
    status result = FAILURE;
    RECORD *head = NULL;

    /* insert record */
    result = insert( &head );

    /* insert successful ? */
    if( result == SUCCESS )
        printf( "insert( ) was successful !\n" );
    else
        printf( "insert( ) was unsuccessful !\n" );

    /* end program */
    return EXIT_SUCCESS;

Quote:
}

/* Function Declaration */
status insert( RECORD **theHeadPtr ){
    /* declare variable */
    char buffer[ MAX ] = { 0 };

    /* allocate memory */
    if( *theHeadPtr == NULL )
        *theHeadPtr = malloc( sizeof( RECORD ) );

    /* allocation successful ? */
    if( *theHeadPtr ){
        printf( "Enter a number : " );
        fflush( stdout );
        fgets( buffer, sizeof( buffer ), stdin );
        sscanf( buffer, "%d", &((*theHeadPtr)->number) );
        ( *theHeadPtr )->next = NULL;
        return SUCCESS;
    }
    return FAILURE;

Quote:
}



Tue, 05 Sep 2000 03:00:00 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. how do you pass in a call back function to a member fnc in C++

2. FAQ: Problems passing results from a function back, gcc and SunOS 4.1.4

3. Problems in calling back Lisp functions from C

4. passing a pointer to a calling function

5. Call Back Functions call from within a DLL

6. Passing MFC Classes to Threads and call backs

7. passing pointers back to main.. [beginner -intermediate]

8. Passing back a typed interface pointer

9. problem passing unmanaged pointer from one managed member function to another

10. Problems passing pointers between an application and dll functions

11. C++ function pointers versus C function pointer problem

12. Pass contents of an array to a function?

 

 
Powered by phpBB® Forum Software