Thread: Need function to show values

  1. #1
    Registered User
    Join Date
    Feb 2022
    Posts
    73

    Need function to show values

    I wrote code that store value at each function call.

    Code:
     #include<stdio.h>
    #include<stdlib.h>
    
    
    int *foo ( int *P, int V)
    {
        int *N = malloc (sizeof(*N)); 
    
    
        if ( N != NULL)
        {
            *N = V;
        }
    }
    
    
    int main()
    {
       int *P = NULL; 
       P = foo ( P, 1);
       P = foo ( P, 2);
       P = foo ( P, 3);
       P = foo ( P, 4);
       
        return 0;   
    }
    I want to write a function that print values. I don't know how to do it with function

  2. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    1,633
    You seem to be thinking of something like this:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
     
    typedef struct N
    {
        int v;
        struct N *x;
    } N;
     
    N *add(N *r, int v)
    {
        N *n = malloc(sizeof *n);
        if (!n) { perror("add"); exit(1); }
        n->v = v;
        n->x = r;
        return n;
    }
     
    void print(const N *r)
    {
        for ( ; r; r = r->x) printf("%d ", r->v);
        printf("\n");
    }
     
    int main()
    {
        N *r = NULL;
        r = add(r, 1);
        r = add(r, 2);
        r = add(r, 3);
        r = add(r, 4);
     
        print(r); // values are printed in reverse order since 'add'
                  // adds new elements to the head of the list
     
        return 0;
    }
    A little inaccuracy saves tons of explanation. - H.H. Munro

  3. #3
    Registered User
    Join Date
    Feb 2022
    Posts
    73
    you are talking about pointer to structure Whereas in my code there is a pointer to integer

    Can I see the values stored in my program with the help of a function?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    john.c introduced a struct type in order to construct a linked list, i.e., struct N represents a linked list node. This way, you can store the values in a linked list and thereby loop over the linked list in order to show them.

    You could say, store the pointers in an array of pointers instead, and thereby loop over the array in order to show the values that the pointers point to. For example:

    Code:
    #include <stdio.h>
    
    int *foo(int value)
    {
        int *p = malloc(sizeof(*p));
        if (p)
        {
            *p = value;
        }
        return p;
    }
    
    
    void print_values(int *values[], size_t num)
    {
        for (size_t i = 0; i < num; ++i)
        {
            if (values[i])
            {
                printf("%d\n", *values[i]);
            }
        }
    }
    
    
    int main(void)
    {
        int *p[4];
        p[0] = foo(1);
        p[1] = foo(2);
        p[2] = foo(3);
        p[3] = foo(4);
    
        print_values(p, sizeof(p) / sizeof(p[0]));
    
        // free what you malloc here
        // ...
    
        return 0;
    }
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function to Show Customer Order
    By KiritoEdward202 in forum C Programming
    Replies: 0
    Last Post: 04-15-2020, 03:22 AM
  2. Replies: 1
    Last Post: 07-24-2014, 11:57 AM
  3. Replies: 13
    Last Post: 07-27-2013, 10:20 AM
  4. Generic Function to show grid.
    By sergioms in forum C Programming
    Replies: 2
    Last Post: 07-20-2011, 12:41 PM
  5. how to show incoming binary data as hex values
    By khalid10 in forum C# Programming
    Replies: 8
    Last Post: 05-10-2010, 11:55 PM

Tags for this Thread