Thread: /* Input */ codes stops working when I modularize the code?

  1. #1
    Registered User Witch's Avatar
    Join Date
    Mar 2010
    Location
    Stockholm, Sweden
    Posts
    15

    /* Input */ codes stops working when I modularize the code?

    Example code 1 - [ OK ]
    3Wah7S4K - Pastebin.com

    But when I try to modularize the /* Input */ codes then I get all sorts of warnings and errors. I managed to fix most of them but get stuck here where it complains about my n variable.

    Example code 2 - [ Error ]
    v4HKi1ae - Pastebin.com

    This is not the only warning but it's the first warning at this point.
    Code:
       C:\Test_Array\example_code_02.c In function `input': 
    90 C:\Test_Array\example_code_02.c [Warning] passing arg 1 of `calloc' makes integer from pointer without a cast

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Code:
    void input(double *array, double *array_copy, int *n)
    {
         int i;
     //    double * array = NULL;
     //    double * array_copy = NULL;
     
        printf("How many numbers do you want to input? --> "); scanf("%d",&n);
        // calloc = Memory allocation for array.
    
        array = calloc(n, sizeof *array);
        array_copy = calloc(n, sizeof *array_copy);
    n is a pointer variable, so you must dereference the pointer variable to get the value to which it's pointing:

    Code:
    array = calloc(*n, sizeof *array);
    array_copy = calloc(*n, sizeof *array_copy);
    If you've got errors, then post the errors...we're not clairvoyant.

  3. #3
    Registered User Witch's Avatar
    Join Date
    Mar 2010
    Location
    Stockholm, Sweden
    Posts
    15
    Thanks for pointing me in the right direction. I googled and got help from Binky Pointer Fun Video along with the reading material Pointer Basics.
    Now I finally grasp the concept of pointers I think, thanks to the author differentiating and terming the words Pointers & Pointees.

    Here's the simplest pointer code I could find. Example code 3 works but when I try to modularize the same code into its own function in Example code 4 then it no longer works.
    Can somebody show me how to do this the correct way?


    Example code 3 - [ OK ]
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    void input(int *, int *);
    
    
    //int main(int argc, char *argv[])
    int main(void)
    {
    // 1
    	int* x;
    	int* y;
    
    
    // 2
    	x = malloc(sizeof(int));
    	y = malloc(sizeof(int));
    
    
    // 3
        input(x, y);
    
    
    // 4
    	*x = 1;
    	*y = 3;
    
    
    // 5
    //	x = y;
    
    
    // 6
        printf("x = %d\n", *x);
        printf("y = %d\n\n", *y);
    
        system("PAUSE");	
        return 0;
    }
    
    /*************
    * Functions
    *************/
    /* ___________________________________________________________________________*/
    /* Input */
    void input(int* x, int* y)
    {
    
    
    
    }
    /* ___________________________________________________________________________*/



    Example code 4 - [ Error ]
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    void input(int *, int *);
    
    
    //int main(int argc, char *argv[])
    int main(void)
    {
    // 1
    	int* x;
    	int* y;
    
    
    // 2
    //	x = malloc(sizeof(int));
    //	y = malloc(sizeof(int));
    
    
    // 3
        input(x, y);
    
    
    // 4
    //	*x = 1;
    //	*y = 3;
    
    
    // 5
    //	x = y;
    
    
    // 6
        printf("x = %d\n", *x);
        printf("y = %d\n\n", *y);
    
        system("PAUSE");	
        return 0;
    }
    
    /*************
    * Functions
    *************/
    /* ___________________________________________________________________________*/
    /* Input */
    void input(int* x, int* y)
    {
    // 1
    //	int* x;
    //	int* y;
    
    
    // 2
    	x = malloc(sizeof(int));
    	y = malloc(sizeof(int));
    
    
    // 4
    	*x = 1;
    	*y = 3;
    
    
    // 5
    
    
    }
    
    
    
    /* ___________________________________________________________________________*/

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I suggest a simpler solution that does not use dynamic memory allocation:
    Code:
    #include <stdio.h>
    
    void input(int* x, int* y);
    
    int main(void)
    {
        int x;
        int y;
    
        input(&x, &y);
    
        printf("x = %d\n", x);
        printf("y = %d\n\n", y);
    
        return 0;
    }
    
    void input(int* x, int* y)
    {
        /* Replace these with actual input reading functionality */
        *x = 1;
        *y = 3;
    }
    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

  5. #5
    Registered User Witch's Avatar
    Join Date
    Mar 2010
    Location
    Stockholm, Sweden
    Posts
    15
    Thanks laserlight that code looks cool.
    Could somebody also show me how the same code, should be solved using the dynamic-memory-allocation-technique?

    These two ways of dealing with Pointers & Pointees confuses the heck out of me. My teacher mixes these two kind of coding styles without explaining when he uses which in his newbie materials. So I have a hard time understanding this subject, when tutorials I google does the same mistake also.


    source: Pointer Basics
    Another way to play with pointers in C (or C++) is using the ampersand (&) operator to compute a pointer to local memory in the stack. However, pointees dynamically allocated in the heap are the most common, so that's what we show.

  6. #6
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Static memory allocation:

    int x[100]; /* this memory is allocated on the stack */

    Dynamic memory allocation:

    int *x;

    x = malloc(sizeof(int) * 100); /* this memory is allocated on the heap */

  7. #7
    Registered User Witch's Avatar
    Join Date
    Mar 2010
    Location
    Stockholm, Sweden
    Posts
    15

    Question

    So why doesn't my Dynamic-memory-allocation code above connect when I modularize it into a function? It works when I run it without using a function.

    Example code 4 - [ Error ]
    You can find the complete code in a previous post above.


    Also why does allocation of pointers look different in 3 different ways when I look at C study materials? Triple confusion.

    int* x;
    int *x;
    int * x;
    Last edited by Witch; 03-28-2010 at 04:11 PM.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Witch
    So why doesn't my Dynamic-memory-allocation code above connect when I modularize it into a function? It works when I run it without using a function.
    The problem is that you are trying to change a pointer (two of them, actually). This means that you need to pass a pointer to a pointer, e.g.,
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    void input(int** x, int** y);
    
    int main(void)
    {
        int* x;
        int* y;
    
        input(&x, &y);
    
        printf("x = %d\n", *x);
        printf("y = %d\n\n", *y);
    
        free(x);
        free(y);
    
        return 0;
    }
    
    void input(int** x, int** y)
    {
        *x = malloc(sizeof(**x));
        *y = malloc(sizeof(**y));
    
        /* Assume that malloc does not return a null pointer
         * Replace these with actual input reading functionality */
        **x = 1;
        **y = 3;
    }
    Quote Originally Posted by Witch
    Also why does allocation of pointers look different in 3 different ways when I look at C study materials? Triple confusion.
    Except in some cases, e.g., within string literals, whitespace is not significant. You should be aware of this when you read those material, but for your own code you should pick one style and stick with it.
    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

  9. #9
    Registered User Witch's Avatar
    Join Date
    Mar 2010
    Location
    Stockholm, Sweden
    Posts
    15

    Thumbs up [solved]

    Thanks laserlight that code rocks!
    Last edited by Witch; 03-29-2010 at 04:39 AM. Reason: [SOLVED]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 03:25 PM
  2. Need some help with C program writing
    By The_PC_Gamer in forum C Programming
    Replies: 9
    Last Post: 02-12-2008, 09:12 PM
  3. About aes
    By gumit in forum C Programming
    Replies: 13
    Last Post: 10-24-2006, 03:42 PM
  4. Working Code Samples Wanted
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 02-13-2002, 10:05 PM