1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

pointer like argument in a function

Discussion in 'C' started by ricardo.crudo, Oct 1, 2010.

  1. ricardo.crudo

    ricardo.crudo New Member

    Joined:
    Oct 1, 2010
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Hello, I'm with difficult in this situation:
    Code:
     void teste(int *p_int)
    {      
        printf("&p_int = %li\n", (long int) &p_int);    // Line T1
        printf("p_int = %li\n", (long int) p_int);      // Line T2
        p_int = malloc(sizeof(int));
        (*p_int) = 123;
        printf("*p_int = %li\n", (long int) *p_int);    // Line T3
    
    }
    
    int main(void)
    {
        int *m;
    
        printf("&m = %li\n", (long int) &m);    // Line M1
        printf("m = %li\n", (long int) m);      // Line M2
            
        teste(m);
        
        printf("*m = %i\n", *m);        // Line M3
        free(m);
    
        
        return 0;
    }
    
    When I execute the result is "Segmentation Fault" when reached the "Line M3"

    My objetive is pass to m pointer the address of memory allocated by the function teste.
    If I do the function return the pointer, all work fine. Is possible make this passing the pointer by argument, like above code?

    Thank You.

    PS: I'm using gcc on Linux.
     
  2. techgeek.in

    techgeek.in New Member

    Joined:
    Dec 20, 2009
    Messages:
    572
    Likes Received:
    19
    Trophy Points:
    0
    Occupation:
    EOC (exploitation of computers)..i m a Terminator.
    Location:
    Not an alien!! for sure
    Home Page:
    what is "%li" ??
     

Share This Page