Thread: Help printing a pointer

  1. #1
    Registered User
    Join Date
    Sep 2022
    Posts
    5

    Help printing a pointer

    I am currently experimenting with pointers, in my program I am trying to display the pointer variable's value in the standard output. Only a blank line appears, does someone know how to fix this and display 42?

    Code:
    #include <stdio.h>
    
    void    point_funct(int *********nbr)
    {
        *********nbr = 42;
    }
    
    
    int main()
    {
        int *********i;
        
        point_funct(i);
        printf("%d", *********i);
        return (0);
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    I suggest you make it work with just one star first, before going all galactic.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    void foo ( int **p ) {
        **p = 42; // The answer
    }
    
    void bar ( int *p ) {
        *p = 88; // MPH
    }
    
    int main ( ) {
        int speed;
        bar(&speed);
        printf("How fast - %d\n", speed);
    
        int answer;
        int *p_answer = &answer;
        foo(&p_answer);
        printf("The question - %d\n", answer);
        return 0;
    }
    FYI, anything more than three stars is rare in the real world.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Dec 2017
    Posts
    1,633
    You are attempting to dereference i 9 times, but there are no addresses set up to allow this.
    How about:
    Code:
    #include <stdio.h>
     
    int main() {
        int      a = 42;
        int     *b = &a;
        int    **c = &b;
        int   ***d = &c;
        int  ****e = &d;
        int *****f = &e;
        printf("%d\n", *****f);
        return 0;
    }
    or
    Code:
    #include <stdio.h>
     
    void f(int ******x) {
        printf("%d\n", ******x);
    }
     
    void e(int *****x) { f(&x); }
    void d(int  ****x) { e(&x); }
    void c(int   ***x) { d(&x); }
    void b(int    **x) { c(&x); }
    void a(int     *x) { b(&x); }
     
    int main() {
        int x = 42;
        a(&x);
        return 0;
    }
    A little inaccuracy saves tons of explanation. - H.H. Munro

  4. #4
    Registered User
    Join Date
    Sep 2022
    Posts
    5
    I found this helpful, thank you!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Printing the value of a pointer
    By sockets12 in forum C Programming
    Replies: 4
    Last Post: 10-07-2014, 06:40 PM
  2. pointer printing
    By c_lady in forum C Programming
    Replies: 10
    Last Post: 06-10-2010, 08:15 AM
  3. pointer printing
    By c_lady in forum C Programming
    Replies: 7
    Last Post: 05-18-2010, 09:50 AM
  4. printing dereferenced pointer.
    By olliepa in forum C Programming
    Replies: 8
    Last Post: 10-18-2009, 05:03 PM
  5. pointer printing
    By bandal27 in forum C Programming
    Replies: 13
    Last Post: 12-29-2008, 07:46 AM

Tags for this Thread