using pointer this way

This is a discussion on using pointer this way within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> void test(int * total); int main(void) { int total = 0; void test(&total); printf("The total is : ...

  1. #1
    Registered User
    Join Date
    Jun 2012
    Posts
    82

    Question using pointer this way

    Code:
    #include <stdio.h>
    void test(int * total);
    
    int main(void)
    {
    int total = 0;
    void test(&total);
    printf("The total is : %d", total);
    return 0;
    }
    
    void test(int * total)
    {
    int number;
    printf("Enter a number : ");
    scanf("%d", &number);
    *total = *total + number;
    }
    Is it okay if I use it like this? Will there be any other issues that I will need to be worried about?

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    2,244
    Is it okay if I use it like this?
    Yes - but for one detail...

    Code:
    int main(void)
    {
    int total = 0;
    void test(&total);
    printf("The total is : %d", total);
    return 0;
    }
    Only specify the return type ("void" in this case) in the function declaration and the function definition, *not* when actually calling the function.

    Will there be any other issues that I will need to be worried about?
    When you get deeper into learning about pointers? Yes. But with this batch of code - no.

  3. #3
    Registered User
    Join Date
    Jun 2012
    Posts
    82
    Oops! What a silly mistake there for specifying void return type while calling the function. Thanks for the correction!
    Hmm, I was worrying about not declaring the pointer and use it just like that.
    And, the 'other issues' when I get deeper into learning about pointers have anything to do with the concept I used in the code?
    *Not just the code, but the (int * total), without typing int *total in main/anywhere*

    One question, when I type (int * total) , does that means that I declared total as the pointer at the same time?

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    2,244
    I was worrying about not declaring the pointer and use it just like that.
    You declared the pointer in "test()" in the function definition.

    You didn't declare a pointer to 'total' in main, but that's okay, because you passed the (&) address of 'total' to the function (which is equally valid - after all, a pointer holds the "address of" a variable).

    This is very common - you should recognize this frequent use of code:

    Code:
    int main(void)
    {
        int x;
    
        printf("Enter an integer:  ");
        scanf("%d",&x);      // <--- right there!
        printf("You entered %d\n",x);
    
        return 0;
    }
    And, the 'other issues' when I get deeper into learning about pointers have anything to do with the concept I used in the code?
    No, that was just my cute and ominous way of saying that the concept of pointers, and their applications, takes a while to fully grasp with full appreciation. But you're showing a great understanding so far, so keep going with it!

    One question, when I type (int * total) , does that means that I declared total as the pointer at the same time?
    No "same time." You only declared a pointer to an integer (not an integer).

    The variable "total" in "main()" (for which you pass the address) and the variable "total" in the "test()" function are different variables - it's a matter of scope.

  5. #5
    Registered User
    Join Date
    Jun 2012
    Posts
    82
    Thanks! But still not 'great' enough. Still have to go through more basic concepts for pointer.

    The variable "total" in "main()" (for which you pass the address) and the variable "total" in the "test()" function are different variables
    Mm, I think I got what you're trying to say there..

  6. #6
    Registered User
    Join Date
    Jun 2011
    Posts
    2,244
    But still not 'great' enough
    I said, "so far" =)
    But you're doing fine!

    Code:
    void function1(int x);  // <--- this 'x'...
    
    int main(void)
    {
        int x = 0;  // <--- ...is not the same as this one
        int carrot = 1;
        function1(carrot);
        return 0;
    }
    
    void function1(int x)
    {
        printf("X in function1 = carrot in main = %d\n",x);
    }
    
    // Output:  X in function1 = carrot in main = 1

  7. #7
    Registered User
    Join Date
    Jun 2012
    Posts
    82
    Quote Originally Posted by Matticus View Post
    I said, "so far" =)
    But you're doing fine!

    Code:
    void function1(int x);  // <--- this 'x'...
    
    int main(void)
    {
        int x = 0;  // <--- ...is not the same as this one
        int carrot = 1;
        function1(carrot);
        return 0;
    }
    
    void function1(int x)
    {
        printf("X in function1 = carrot in main = %d\n",x);
    }
    
    // Output:  X in function1 = carrot in main = 1
    Haha, thanks!

    Ahh I see now! So I can just replace void function(int *x); with any other variables I see. Thanks man!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 21
    Last Post: 01-22-2012, 10:35 AM
  2. Replies: 3
    Last Post: 10-30-2009, 04:41 PM
  3. Replies: 9
    Last Post: 06-13-2009, 02:31 AM
  4. Replies: 1
    Last Post: 03-24-2008, 10:16 AM
  5. Replies: 4
    Last Post: 08-27-2007, 11:51 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21