-2
int delete_node(node* vertex)
{
    
edgesnode* p = vertex -> edges;
while(p != NULL)
{
    p -> user -> edges= delete_from_edges_list(p -> vertex -> friends, vertex);
    p = delete_from_edges_list(p,p -> vertex);
}
free(vertex);
vertex = NULL;
**if(vertex == NULL)
{
    printf("Cass\n");
}**
return 0;
}

When I set the vertex to NULL at the end of the program it says it is null. but in main when i test again using the original pointer to vertex it doesnt give me null. And i don't understand why.

This is my main function code:

node* vertex1 = create_user("Blah");
node* vertex2 = create_user("BlahBlah");
add_edge(vertex1, vertex2);

print_user_data(vertex1);
delete_user(vertex1);
print_user_data(vertex2);   
if(vertex1 == NULL)
{
    printf("Passed\n");
}

The function above is meant to delete a node from a undirected unweighted graph.

  • Because the pointer in `main` is a different pointer? See [mcve]. – user3386109 Dec 05 '20 at 23:43
  • I updated my code to include my main function. is there something wrong? – Allp Raplsf Dec 05 '20 at 23:49
  • is this Better? – Allp Raplsf Dec 05 '20 at 23:55
  • 1
    C passes arguments by value, so `vertex = NULL;` sets to `NULL` the *copy* of the pointer that was passed to `delete_node`. It does not (and could not) affect the value of the pointer in the caller. – dxiv Dec 05 '20 at 23:58
  • So how do I set the value I want to null? – Allp Raplsf Dec 05 '20 at 23:59
  • 1
    @AllpRaplsf Pass the *address* of the pointer, as you would do with any variable that you want to modify from a called function. Don't know and can't guess the specifics, since the code you posted does not call `delete_node` anywhere. See [How to create a Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example) then [edit](https://stackoverflow.com/questions/65163115/delete-a-node-of-a-graph-isnt-setting-value-to-null) your question and add all that's missing. – dxiv Dec 06 '20 at 00:05

1 Answers1

0

A pointer is a variable that holds an address in the memory space (the value of a variable that It points to). passing the pointer as argument will create a copy of that pointer holding the same value (the same address)

So when setting the second pointer(the copy) to NULL the first one is not affected

EX:
If pointer A holds the value (address) 0x000CF This value is an address in the memory.
that address contains another variable. X for example
X in your case is an integer, because the pointer is of type int (*int)

When passing A as an argument, B which is a copy of A is being created that holds the same value 0x000CF.
So when the function executes, It changes in B
So setting B to NULL will only change the value of B from 0x000CF to NULL
A will remain unaffected

The thing you can do is:

Like @dixv mentioned in the comment:

@AllpRaplsf Pass the address of the pointer, as you would do with any variable that you want to modify from a called function. Don't know and can't guess the specifics, since the code you posted does not call delete_node anywhere. See How to create a Minimal, Reproducible Example then edit your question and add all that's missing.

Or change the variable pointed to by the pointer:

To change the variable pointed by these pointers (the variable located at the address that these 2 pointers have as a value) to be NULL

Ex: *B= NULL and because A points to the same address (which is now NULL) *A will be equal to NULL

EEAH
  • 715
  • 4
  • 17
  • It doesn't work, it gives me the error: incompatible types when assigning to type 'User' {aka 'struct user_struct'} from type 'void *', I changed the vertex value after freing node like this :*(q) = NULL; – Allp Raplsf Dec 06 '20 at 02:03
  • any idea why this is happening? – Allp Raplsf Dec 06 '20 at 17:05
  • i tried to do *(vertex) = NULL before freeing and agter freeing the node. it gave me the same error and I am not sure what to do. – Allp Raplsf Dec 06 '20 at 17:27
  • `NULL`is of type `void*` and `User` is of type `struct`. You can't assign a pointer to a non-pointer value. https://stackoverflow.com/a/11416425/13695921 Also there is a difference between `free` and `delete`. If you are pointing to a newly created object (`object* p = new Object ()`) `delete` should be used. https://stackoverflow.com/a/2910694/13695921 Maybe the easiest method is lole @divx said in the comment to just pass the pointer as reference (pass the address of that pointer), or use pointers of type struct user_struct (struct user_struct *user) instead of non-po – EEAH Dec 06 '20 at 17:46
  • Note that the previous comment (about changing the variable before freeing) is not correct. Sorry, you can't free a pointer (an address) after setting that pointer to `NULL`. https://stackoverflow.com/a/2910694/13695921 – EEAH Dec 06 '20 at 17:50
  • So what do I do to set it to NULL. Beacuse it is a pointer and I am trying to set it to NULL or empty it using the methods I know. But I am so lost. Usually I would return NULL to the original pointer and it would set it to null. But now I cant do that. – Allp Raplsf Dec 06 '20 at 17:57
  • lease man I genuinely dont know what to do, been at it for hours – Allp Raplsf Dec 07 '20 at 00:16