Thread: whats the problem

  1. #1
    Registered User fouzimedfouni's Avatar
    Join Date
    Oct 2014
    Posts
    61

    Exclamation whats the problem

    I tried to use pointer with a function but I guess I am missing out on something

    Code:
    #include<stdio.h>
    int add(int *a,int *b){
        int *c,*d;
        *c = *c+1;
        *d = *d-1;
    
    };
    main()
    {
        //int Fun;
        int num1,num2;
        num1 = 10;
        num2 = 10;
        add(&num1,&num2);
        printf("    %i        %i   " ,num1,num2);
    
    }
    and this one too

    Code:
    #include<stdio.h>
    void add(int *a){
        int *c;
        c = c+1;
    };
    main()
    {
        int num1;
        num1 = 10;
        add(&num1);
        printf("    %i " ,num1);
        getch();
    
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    In your first program, you forgot to make your pointers point to an object before dereferencing them.

    In your second program, you incremented c, but that is not useful because the pointer does not point to the first element in an array, and besides, you only changed the local pointer.
    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

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,667
    Perhaps you should stop declaring local pointer variables that point nowhere, and do something like this, with the parameter

    *a = *a + 1;
    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.

  4. #4
    Registered User fouzimedfouni's Avatar
    Join Date
    Oct 2014
    Posts
    61
    Quote Originally Posted by laserlight View Post
    In your first program, you forgot to make your pointers point to an object before dereferencing them.

    In your second program, you incremented c, but that is not useful because the pointer does not point to the first element in an array, and besides, you only changed the local pointer.
    dont understand , need an example or the code debugged

  5. #5
    Registered User fouzimedfouni's Avatar
    Join Date
    Oct 2014
    Posts
    61
    Quote Originally Posted by Salem View Post
    Perhaps you should stop declaring local pointer variables that point nowhere, and do something like this, with the parameter

    *a = *a + 1;
    an example please

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Here is an example:
    Code:
    #include<stdio.h>
    
    int add(int *a, int *b)
    {
        *a += 1;
        *b += 2;
    }
    
    int main(void)
    {
        int num1, num2;
        num1 = 10;
        num2 = 10;
        add(&num1, &num2);
        printf("%d %d\n", num1, num2);
        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

  7. #7
    Registered User fouzimedfouni's Avatar
    Join Date
    Oct 2014
    Posts
    61
    tried that and worked
    Code:
    #include<stdio.h>
    void add(int *c){
        
        *c = *c+1;
    };
    main()
    {
        int num1;
        num1 = 10;
        add(&num1);
        printf("    %i " ,num1);
        getch();
    
    }

  8. #8
    Registered User fouzimedfouni's Avatar
    Join Date
    Oct 2014
    Posts
    61
    Quote Originally Posted by laserlight View Post
    Here is an example:
    Code:
    #include<stdio.h>
    
    int add(int *a, int *b)
    {
        *a += 1;
        *b += 2;
    }
    
    int main(void)
    {
        int num1, num2;
        num1 = 10;
        num2 = 10;
        add(&num1, &num2);
        printf("%d %d\n", num1, num2);
        return 0;
    }
    thank you so much

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I'm new to this ;(, whats the problem with this????????
    By MrHoboMe in forum C++ Programming
    Replies: 6
    Last Post: 12-07-2010, 04:26 PM
  2. strcmp problem, whats the problem, i cant figure it out!
    By AvaGodess in forum C Programming
    Replies: 14
    Last Post: 10-18-2008, 06:45 PM
  3. whats the problem....
    By vapanchamukhi in forum C Programming
    Replies: 3
    Last Post: 09-05-2008, 12:19 PM
  4. Whats my problem here?
    By SebastionV3 in forum C++ Programming
    Replies: 18
    Last Post: 05-05-2006, 10:16 PM