pointer to struct I'm confused 
Author Message
 pointer to struct I'm confused

I have this program which for some reason, if I make the pointer to
structs global it works, but if I pass them as parameters, like so, it
doesn't work.  This has been driving me nuts, and any help would be
greatly appreciated.  I thought if you pass a structure, it modifies the
actual structure member addresses, so if you pass a pointer to struct,
even as parameters, wouldn't it change the value overall??  It's not,
after it exits the add_node function, it sets the pointer to structs
back to NULL  Why?

/* Library files */

#include <stdio.h>
#include <stdlib.h>

/* Function prototypes */

int add_node(int number, struct temp *start_pointer, struct temp
*current_pointer);
int transverse_list(struct temp *start_pointer);

/* Global Structure, but no memory set aside for no structure variables
were declared globally */

struct temp
{
 int number;
 struct temp *next;

Quote:
};

/*************************************************************************/

main()
{


initialized to NULL values */

 struct temp *start_pointer = NULL;
 struct temp *current_pointer = NULL;

 int number;

 printf("Please enter a series of numbers.  Please enter 4 to stop.\n");

 scanf("%d", &number);

 while(number != 4)
 {
/* Function that passes users entered number as well as the start and
current pointer which are both initialized to NULL */

add_node(number, start_pointer, current_pointer);
  scanf("%d", number);
 }
 transverse_list(start_pointer);
return(0);

Quote:
}

/*************************************************************************/

/* recieves number integer, start and current pointer */

int add_node(int number, struct temp *start_pointer, struct temp
*current_pointer)
{
 struct temp *temp_pointer;
 temp_pointer = (struct temp *)malloc(sizeof(struct temp));

 if(temp_pointer == NULL)
 {
  printf("Sorry, not enough memory avalaible for allocation.");
  exit(1);
 }

 if(start_pointer == NULL)
 {
  start_pointer = temp_pointer;
 }
 else
 {
  current_pointer->next = temp_pointer;
 }

 current_pointer = temp_pointer;
 current_pointer->number = number;
 current_pointer->next = NULL;

/* I thought the values were changed for the linked list, and that all
pointers were fine, but once it hits the return statement, these
pointers go back to NULL values for some reason */

return(0);

Quote:
}

/*************************************************************************/

int transverse_list(struct temp *start_pointer)
{
 struct temp *temp_pointer = start_pointer;

 printf("\n\nThe data you entered was:\n");

 while(temp_pointer != NULL)
 {
  printf("%d\n", temp_pointer->number);
  temp_pointer = temp_pointer->next;
 }
return(0);

Quote:
}

/*************************************************************************/

Why doesn't this work?  As far as I know structures are treated as
arrays, arrays are treated as pointers, and when pointers are passed,
they are passed by reference, so why doesn't the values change and stay
changed?? Once again any help will be appreciated!!!

Thanks for all your Time, Help and Co-Operation!!!

P.S.  If you could carbon copy (send) a reply to my e-mail as well, that
would be greatly appreciated!

Shane Escher



Fri, 22 Dec 2000 03:00:00 GMT  
 pointer to struct I'm confused
Let say you have an integer.  If you want to change the value of that
integer from inside a function you are calling, then you have send the
address of the integer.  If you want to change the value of a pointer to a
structure, then you have to do the same thing.  What you are doing is
passing NULL and not the address of your structure.  Here are some examples
of what you should be doing.  NOTE: My examples are not meant to actually
build any functional linked list.  I wrote them this way just to show how to
make the calls and how to pass the variables.  As written, this code works,
but it is junk and it is meant to be junk.

#include <stdio.h>
#include <stdlib.h>

struct temp
{
    int number;
    struct temp *next;

Quote:
};

void Func( struct temp **v, int *count );

int main()
{
    struct temp *v = NULL;
    int count = 0;

    Func( &v, &count );
    printf( "%d %d\n", v->number, count );

    Func( &v, &count );
    printf( "%d %d\n", v->next->number, count );

    free( v->next );
    free( v );

    return 0;

Quote:
}

void Func( struct temp **v, int *count )
{
    if ( *v == NULL )
    {
        *v = malloc( sizeof (struct temp) );  /* Check the return value. */
        (*v)->number = 10;
        (*v)->next = NULL;
        ++(*count);
    }
    else
    {
        (*v)->next = malloc( sizeof (struct temp) );
        (*v)->next->number = 20;
        (*v)->next->next = NULL;
        ++(*count);
    }

Quote:
}

--
Increase the Peace!
Charles LaCour

Quote:

>I have this program which for some reason, if I make the pointer to
>structs global it works, but if I pass them as parameters, like so, it
>doesn't work.  This has been driving me nuts, and any help would be
>greatly appreciated.  I thought if you pass a structure, it modifies the
>actual structure member addresses, so if you pass a pointer to struct,
>even as parameters, wouldn't it change the value overall??  It's not,
>after it exits the add_node function, it sets the pointer to structs
>back to NULL  Why?

>/* Library files */

>#include <stdio.h>
>#include <stdlib.h>

>/* Function prototypes */

>int add_node(int number, struct temp *start_pointer, struct temp
>*current_pointer);
>int transverse_list(struct temp *start_pointer);

>/* Global Structure, but no memory set aside for no structure variables
>were declared globally */

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

>/*************************************************************************/

>main()
>{


>initialized to NULL values */

> struct temp *start_pointer = NULL;
> struct temp *current_pointer = NULL;

> int number;

> printf("Please enter a series of numbers.  Please enter 4 to stop.\n");

> scanf("%d", &number);

> while(number != 4)
> {
>/* Function that passes users entered number as well as the start and
>current pointer which are both initialized to NULL */

>add_node(number, start_pointer, current_pointer);
>  scanf("%d", number);
> }
> transverse_list(start_pointer);
>return(0);
>}

>/*************************************************************************/

>/* recieves number integer, start and current pointer */

>int add_node(int number, struct temp *start_pointer, struct temp
>*current_pointer)
>{
> struct temp *temp_pointer;
> temp_pointer = (struct temp *)malloc(sizeof(struct temp));

> if(temp_pointer == NULL)
> {
>  printf("Sorry, not enough memory avalaible for allocation.");
>  exit(1);
> }

> if(start_pointer == NULL)
> {
>  start_pointer = temp_pointer;
> }
> else
> {
>  current_pointer->next = temp_pointer;
> }

> current_pointer = temp_pointer;
> current_pointer->number = number;
> current_pointer->next = NULL;

>/* I thought the values were changed for the linked list, and that all
>pointers were fine, but once it hits the return statement, these
>pointers go back to NULL values for some reason */

>return(0);
>}

>/*************************************************************************/

>int transverse_list(struct temp *start_pointer)
>{
> struct temp *temp_pointer = start_pointer;

> printf("\n\nThe data you entered was:\n");

> while(temp_pointer != NULL)
> {
>  printf("%d\n", temp_pointer->number);
>  temp_pointer = temp_pointer->next;
> }
>return(0);
>}

>/*************************************************************************/

>Why doesn't this work?  As far as I know structures are treated as
>arrays, arrays are treated as pointers, and when pointers are passed,
>they are passed by reference, so why doesn't the values change and stay
>changed?? Once again any help will be appreciated!!!

>Thanks for all your Time, Help and Co-Operation!!!

>P.S.  If you could carbon copy (send) a reply to my e-mail as well, that
>would be greatly appreciated!

>Shane Escher




Sat, 23 Dec 2000 03:00:00 GMT  
 pointer to struct I'm confused

Quote:

> [snip]

> int add_node(int number, struct temp *start_pointer, struct temp
> *current_pointer)
> {
>  struct temp *temp_pointer;
>  temp_pointer = (struct temp *)malloc(sizeof(struct temp));

>  if(temp_pointer == NULL)
>  {
>   printf("Sorry, not enough memory avalaible for allocation.");
>   exit(1);
>  }

>  if(start_pointer == NULL)
>  {
>   start_pointer = temp_pointer;
>  }
>  else
>  {
>   current_pointer->next = temp_pointer;
>  }

>  current_pointer = temp_pointer;
>  current_pointer->number = number;
>  current_pointer->next = NULL;

> /* I thought the values were changed for the linked list, and that all
> pointers were fine, but once it hits the return statement, these
> pointers go back to NULL values for some reason */

> return(0);
> }

  When you write current_pointer = temp_pointer; you change the copy of pointer
which was given to function by the caller (main). So you must use the addresses
of pointer to structures. Ex.

main() {
struct temp *start_pointer = NULL, *current_pointer = NULL;
...
add_node(number, &start_pointer, &current_pointer);
...

Quote:
}

int add_node(int number, struct temp **start_pointer, struct temp **current_pointer)
{
struct temp *temp_pointer;

 temp_pointer = (struct temp *)malloc(sizeof(struct temp));

 if(temp_pointer == NULL) {
   printf("Sorry, not enough memory avalaible for allocation.");
   exit(1);
 }

 if(*start_pointer == NULL) *start_pointer = temp_pointer;
 else                       (*current_pointer)->next = temp_pointer;

 *current_pointer = temp_pointer;
 (*current_pointer)->number = number;
 (*current_pointer)->next = NULL;

  return(0);

- Show quoted text -

Quote:
}



Sat, 23 Dec 2000 03:00:00 GMT  
 pointer to struct I'm confused

    Shane> I have this program which for some reason, if I make the pointer
    Shane> to structs global it works, but if I pass them as parameters,
    Shane> like so, it doesn't work.  This has been driving me nuts, and
    Shane> any help would be greatly appreciated.  I thought if you pass a
    Shane> structure, it modifies the actual structure member addresses, so
    Shane> if you pass a pointer to struct, even as parameters, wouldn't it
    Shane> change the value overall??  It's not, after it exits the
    Shane> add_node function, it sets the pointer to structs back to NULL
    Shane> Why?

[code snipped]

    Shane> Why doesn't this work?  As far as I know structures are treated
    Shane> as arrays, arrays are treated as pointers, and when pointers are
    Shane> passed, they are passed by reference, so why doesn't the values
    Shane> change and stay changed?? Once again any help will be
    Shane> appreciated!!!

The reason your code doesn't work is this: Somebody told you utter nonsense
about the C language. Function arguments are *always* passed by *value*,
not by reference. If you really want the pointer you pass to be updated,
you have to pass a pointer to the pointer, or return the new value of the
pointer.

    Shane> P.S.  If you could carbon copy (send) a reply to my e-mail as
    Shane> well, that would be greatly appreciated!

Post news, read news.

--
Nils Goesche
My obscure opinions are my very own.



Sat, 23 Dec 2000 03:00:00 GMT  
 pointer to struct I'm confused
Dunno what the problemo is dude but try tidying up your code by defineing a
type for your structure rather than . I.e. use
typedef struct { ... }  mytype ;
rather than
struct my struct { ... };
that way you can define variables using
 mytype myvar;
or
mytype * mypointer;
makes the whoe structure - pointer thang a lot easier to breath around.

Later

Julian

Quote:

>I have this program which for some reason, if I make the pointer to
>structs global it works, but if I pass them as parameters, like so, it
>doesn't work.  This has been driving me nuts, and any help would be
>greatly appreciated.  I thought if you pass a structure, it modifies the
>actual structure member addresses, so if you pass a pointer to struct,
>even as parameters, wouldn't it change the value overall??  It's not,
>after it exits the add_node function, it sets the pointer to structs
>back to NULL  Why?

>/* Library files */

>#include <stdio.h>
>#include <stdlib.h>

>/* Function prototypes */

>int add_node(int number, struct temp *start_pointer, struct temp
>*current_pointer);
>int transverse_list(struct temp *start_pointer);

>/* Global Structure, but no memory set aside for no structure variables
>were declared globally */

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

>/*************************************************************************/

>main()
>{


>initialized to NULL values */

> struct temp *start_pointer = NULL;
> struct temp *current_pointer = NULL;

> int number;

> printf("Please enter a series of numbers.  Please enter 4 to stop.\n");

> scanf("%d", &number);

> while(number != 4)
> {
>/* Function that passes users entered number as well as the start and
>current pointer which are both initialized to NULL */

>add_node(number, start_pointer, current_pointer);
>  scanf("%d", number);
> }
> transverse_list(start_pointer);
>return(0);
>}

>/*************************************************************************/

>/* recieves number integer, start and current pointer */

>int add_node(int number, struct temp *start_pointer, struct temp
>*current_pointer)
>{
> struct temp *temp_pointer;
> temp_pointer = (struct temp *)malloc(sizeof(struct temp));

> if(temp_pointer == NULL)
> {
>  printf("Sorry, not enough memory avalaible for allocation.");
>  exit(1);
> }

> if(start_pointer == NULL)
> {
>  start_pointer = temp_pointer;
> }
> else
> {
>  current_pointer->next = temp_pointer;
> }

> current_pointer = temp_pointer;
> current_pointer->number = number;
> current_pointer->next = NULL;

>/* I thought the values were changed for the linked list, and that all
>pointers were fine, but once it hits the return statement, these
>pointers go back to NULL values for some reason */

>return(0);
>}

>/*************************************************************************/

>int transverse_list(struct temp *start_pointer)
>{
> struct temp *temp_pointer = start_pointer;

> printf("\n\nThe data you entered was:\n");

> while(temp_pointer != NULL)
> {
>  printf("%d\n", temp_pointer->number);
>  temp_pointer = temp_pointer->next;
> }
>return(0);
>}

>/*************************************************************************/

>Why doesn't this work?  As far as I know structures are treated as
>arrays, arrays are treated as pointers, and when pointers are passed,
>they are passed by reference, so why doesn't the values change and stay
>changed?? Once again any help will be appreciated!!!

>Thanks for all your Time, Help and Co-Operation!!!

>P.S.  If you could carbon copy (send) a reply to my e-mail as well, that
>would be greatly appreciated!

>Shane Escher




Sun, 31 Dec 2000 03:00:00 GMT  
 pointer to struct I'm confused

Quote:

>>/* recieves number integer, start and current pointer */

>>int add_node(int number, struct temp *start_pointer, struct temp
>>*current_pointer)
>>{
>> struct temp *temp_pointer;
>> temp_pointer = (struct temp *)malloc(sizeof(struct temp));

>> if(temp_pointer == NULL)
>> {
>>  printf("Sorry, not enough memory avalaible for allocation.");
>>  exit(1);
>> }

>> if(start_pointer == NULL)
>> {
>>  start_pointer = temp_pointer;
>> }
>> else
>> {
>>  current_pointer->next = temp_pointer;
>> }

>> current_pointer = temp_pointer;
>> current_pointer->number = number;
>> current_pointer->next = NULL;

>>/* I thought the values were changed for the linked list, and that all
>>pointers were fine, but once it hits the return statement, these
>>pointers go back to NULL values for some reason */

>>return(0);
>>}

Hello friend
the problem is that the above function is not really updating the
start_pointer or the current_pointer from  your main because they are
not passed in!!!!!! if you have a look at faq 4.8 you should be able to
figure it out. The problem is that you are passing in copies of your
pointers wheras you should have been passing in references to your
pointers so that the original pointers get updated.
so your fuction decl, shoulod be

int add_node(int number, struct temp **start_pointer, struct temp
**current_pointer);

what's probably cleaner is if you returned a pointer to the new node
from add_node and updated your main() pointers from within main, this
would also be quicker as you wouldn't need to pass in the two pointers.

Ted
"shame on you Michael  Abrash, tempted by  micro$oft, but your black
book is good anyway!!!!!!!!!!!!!!!!!!!! "
--
Ted



Wed, 17 Jan 2001 03:00:00 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. I'm confused with typedef struct...

2. pointers to functions in structs which take a pointer to that struct as an arg

3. cast pointer to struct to pointer to type of first member of struct

4. Pointers assigned to pointers - confused

5. confused null pointer with null pointer constant

6. free()'ing structs containing pointers to memory?

7. memory leak: pointer->pointer->pointer->struct

8. memory Leak: pointer->pointer->pointer->struct

9. Confused about struct

10. confused about structs

11. Confused between 0 and '\0'

12. Need DllImport expert to convert c++ to c# - same struct pointer in struct

 

 
Powered by phpBB® Forum Software