Thread: Basic pointer question

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    21

    Basic pointer question

    Hi all,

    I have what i think should be a very basic question about pointers I just can't seem to get my head around it at the moment! The code below illustrates the problem on a simpler scale:

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    struct node {
    	int i;
    	char *s;
    };
    
    void f1(struct node *ptr) {
    	ptr = malloc(sizeof(struct node));
    	ptr->i = 2;
    	printf("%s\n", ptr);
    
    }
    
    int main() {
    	
    	struct node *ptr=NULL;
    	
    	f1(ptr);
    	
    	printf("%s\n", ptr);
    	
    	return 0;
    }
    Basically printf within 'f1' doesn't show the ptr as being NULL yet the printf in main shows as '(null)'. Why is this - shouldn't changing the ptr in f1 reflect in main?

    I am compiling using -Wall -Wextra -pedantic -ansi using gcc on windows if this helps?

    Cheers,
    James

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    First of all, you really shouldn't be printing the pointer as a character string. Use the %p specifier, or similar. Anyway, the issue is that in C all things are passed to functions by value - pointers included. So, from main, the pointer itself (the variable) is located at address 0xdeadbeef and contains the value of 0. When you pass it to the function, the *value* of the pointer (eg: 0) is copied, but the address of the local variable within the function is *not* 0xdeadbeef, so the changes made to it will obviously not be reflected in the variable within main. Hope that makes sense. Anyway, the solution is simply one more level of indirection - in other words, pass the *address* of the pointer:

    Code:
    
    
    #include <stdlib.h>
    #include <stdio.h>
    
    struct node {
    	int i;
    	char *s;
    };
    
    void f1(struct node **ptr) {
    	(*ptr) = malloc(sizeof(struct node));
    	(*ptr)->i = 2;
    	printf("%p\n", ptr);
    
    }
    
    int main() {
    	
    	struct node *ptr=NULL;
    	
    	f1(&ptr);
    	
    	printf("%p\n", &ptr);
    	
    	return 0;
    }

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by james123 View Post
    Why is this - shouldn't changing the ptr in f1 reflect in main?
    No. You are passing a value, which is a memory location. That value is assigned to the variable, "ptr". You can modify the contents of that location using ptr and that will be reflected in main.

    But that's not what you do -- you reassign the variable to something else:

    Code:
    ptr = malloc(sizeof(struct node));
    So the value of ptr is no longer the same as it is in main.

    An assignment/reassignment is anything that involves
    ptr =
    If you do that, you need to return the value, so your function would work more like:
    Code:
    ptr = f1();
    So you would create a struct pointer in f1, malloc it space, give it contents, etc, and return the pointer. (This is a bit more straightforward and normative than Sebastiani's solution )
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    Registered User
    Join Date
    Dec 2009
    Posts
    16
    wt type of output u getting?

  5. #5
    Registered User
    Join Date
    Oct 2009
    Posts
    21
    Thanks to everyone who has replied - I went for Sebastiani's solution because in my real code the function already returns an int value so returning is not an option - This is what I have done in the past hence why I was stumped when this didn't work!

    Anyway the problem is sorted now and more importantly I understand why it works!

    Thanks,
    James

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Quick Pointer Question
    By gwarf420 in forum C Programming
    Replies: 15
    Last Post: 06-01-2008, 03:47 PM
  2. Ban pointers or references on classes?
    By Elysia in forum C++ Programming
    Replies: 89
    Last Post: 10-30-2007, 03:20 AM
  3. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  4. pointer to pointer as argument question
    By Lateralus in forum C Programming
    Replies: 4
    Last Post: 07-21-2005, 05:03 PM
  5. basic pointer question need help
    By venckman in forum C Programming
    Replies: 4
    Last Post: 04-23-2003, 11:25 AM