Thread: pointer to a pointer

  1. #1
    Registered User
    Join Date
    Oct 2018
    Posts
    10

    pointer to a pointer

    so i have a function with a pointer input, and im able to change the pointer value but whenever i get out of the function, the pointer doesnt hold the value i changed it to and keeps the old value. i tried to make a pointer to pointer but couldnt figure it out. can someone see my code and help please? thanks.

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    can someone see my code and help please? thanks.
    What code?

  3. #3
    Registered User
    Join Date
    Oct 2018
    Posts
    10
    pointer to a pointer-ex1problem1-jpg

  4. #4
    Registered User
    Join Date
    Oct 2018
    Posts
    10
    the function returns 0 which means it did get into the "if" , also it prints the right type but outside the function, the pointer i send stays NULL.

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    Please post your code, inside code tags, as text. IMO, your picture is too hard to see.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    A function can never change the value of the thing it is passed. Since you pass it ppp, the value of ppp cannot change. If you want the value of ppp to be changed by the function, you will have to pass a pointer to ppp (via &ppp) and adjust the type of the parameter in the function appropriately.

  7. #7
    Registered User
    Join Date
    Oct 2018
    Posts
    10
    insert
    Code:
    const airplane airplaneTypes[3] = {
    									{.type = "737" ,.destinations = "Larnaca,Athens,Budapest,Zurich,London,Paris,Rome"},
    									{.type = "747" ,.destinations = "London,New York,Bangkok"},
    									{.type = "787" ,.destinations = "London,New York,Los Angeles,Hong Kong,Miami"}
    };
    
    
    int GetAirplaneType(const char *destination, char **pTotype) 
    {
    	
    	
    	char *n = NULL;
    	char *p;
    	int i;
    	char *pDestinationsList = NULL;
    	for (i = 0; i < 3; i=i+1) 
    	{
    		pDestinationsList = (airplaneTypes[i].destinations);
    		p = strstr(pDestinationsList, destination);
    		if (p!=n )
    		{
    			printf("type print: %s\n", airplaneTypes[i].type);
    			pTotype = &airplaneTypes[i].type;
    			printf("print of the pointer value inside the function: %s\n", pTotype);
    			return (0);
    		}
    	}
    	return (-1);
    }
    
    
    void main() 
    {
    	int i = 3;
    	char **ppp=NULL ;
    	const char *des = "New York";
    	i=GetAirplaneType(des, ppp);
    	printf("function returns: %d\n", i);
    	printf("print of the pointer value outside the function: %s\n", ppp);
    }

  8. #8
    Registered User
    Join Date
    Oct 2018
    Posts
    10
    can u tell me which adjustment are to make please? if im sending *ppp to the function.

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    First you are going to have to decide what you want ppp to be, as you are using it in two different ways inside your main() function (or rather, you are defining it one way, as a char**, and using it a different way, as a char * in your printf). Once you know what type you want ppp to be, then you can make the parameter in your function be a pointer to that type.

  10. #10
    Registered User
    Join Date
    Oct 2018
    Posts
    10
    sorry for the mess.
    i want a function that gets a pointer to char (or a pointer to pointer) and i want the pointer that sent to the function to point on a new thing, and use its new value on the main.
    can u tell me what adjustments are to be done?

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by sagi View Post
    sorry for the mess.
    i want the pointer that sent to the function to point on a new thing
    Well, you can't change the value of a variable sent to a function. So if you send a pointer to a function, you cannot change where that pointer points to; you can only change the value being pointed at. So if you had:
    Code:
    int x;
    int* y = &x;
    foo(y);
    where foo is some function that takes a pointer-to-int, there is nothing foo can do to change where y points; it will still point to x. foo can, though, change the value of x, via the pointer y.

    You still need to decide what ppp is supposed to be; if you want it to represent a string, then it should be a char*. You would then have
    Code:
    char *ppp;
    foo (&ppp);
    and then you can change the value of ppp (via the pointer that was passed in).

  12. #12
    Registered User
    Join Date
    Oct 2018
    Posts
    10
    i really try to understand and figure it out. i thought i did but still when i print ppp from the main it stays null and when i print my pTotype inside the function it points to the string i wanted it to.

  13. #13
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    Okay, let's start with a simpler example.

    Code:
    #include <stdio.h>
    
    void GetAirplaneType(char **dest)
    {
        printf("print of the pointer value inside the function: %s %p\n", *dest, (void*)(*dest));
        char new_dest[] = "Big Deal";
        *dest = new_dest;
    }
    
    
    int main()
    {
        // Let's start with an array, not a const char*.
        char des[100] = "New York";
        printf("print of the pointer value outside the function: %s %p\n", des, (void*)des);
    
        // Create a pointer that is pointing to the variable des.
        char *n_des = des;
      
        GetAirplaneType(&n_des);
    
        printf("print of the pointer value outside the function: %s %p\n", n_des, (void*)n_des);
    
    }
    Note: You probably shouldn't be playing with pointers but instead copy the C-strings.

    Code:
    #include <stdio.h>
    #include <string.h>
    
    void GetAirplaneType(char *dest)
    {
        strcpy(dest, "Big Deal");
    }
    
    
    int main()
    {
        char des[100] = "New York";
        printf("print of the pointer value before the function: %s \n", des);
    
        GetAirplaneType(des);
    
        printf("print of the value after the function: %s \n", des);
    
    }

  14. #14
    Registered User
    Join Date
    Oct 2018
    Posts
    10
    thank you guys very more for explanations!
    im new here, how can i thumbs up for you guys?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 08-05-2018, 12:14 AM
  2. Replies: 4
    Last Post: 08-18-2015, 03:13 PM
  3. Replies: 8
    Last Post: 03-01-2015, 12:39 AM
  4. Replies: 3
    Last Post: 10-30-2009, 04:41 PM
  5. Replies: 4
    Last Post: 08-27-2007, 11:51 PM

Tags for this Thread