Thread: What did I do wrong?

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    23

    What did I do wrong?

    The assignment wants me to print the value and the address of a and b, i dont know why I am having so much trouble with this it seems simple, i just need some direction.
    Code:
    #include <stdio.h>
    void print_info(int a, int *b);
    int 
    main(void) 
    {
    	int x;
    	
    	printf("Enter a value for x: ");
    	scanf("%d", &x);
    	
     print_info(x, &x);
    
    	return 0;
    }
    
    void 
    print_info(int a, int *b)
    {
    printf("%d", a);
    printf("%p", &a);
    printf("%d", *b);
    printf("%p", b);
    }

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    My guess is that your "trouble" concerns the variable a inside print_info(). You will find that &a inside print_info() will have a different address from b (since a is passed by value, it becomes a variable with the same value as x, but at a different location in memory). So the two values produced using %d will be the same, but the two addresses formatted using %p will differ.

    For future reference, it would have helped if you actually described what you meant by "so much trouble". Forum members are not mindreaders, you know, and forcing people to guess what your problem is is not an effective way of getting help. Most forum members will simply not bother to help. The only reason you got a response from me is that I happened - by accident - to be able to make a guess quickly.

    In particular, you should have described what output are you expected your code to produce, and provided an example if the output differs from your expectations.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Yep that program looks fine to me also.
    I too believe that the problem is with your expectations.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    Registered User
    Join Date
    Jun 2012
    Posts
    39
    Well I am pretty sleep deprived right now, but I am pretty sure that the int on line 3 should not be there. Can you give a bit more info on your actual problem?

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by Codegeek892 View Post
    Well I am pretty sleep deprived right now, but I am pretty sure that the int on line 3 should not be there.
    That "int" on line 3 can be left out (it's optional in C) but doing so is often considered to be bad practice.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  6. #6
    Registered User
    Join Date
    Sep 2012
    Posts
    357
    Quote Originally Posted by Codegeek892 View Post
    Well I am pretty sleep deprived right now, but I am pretty sure that the int on line 3 should not be there. Can you give a bit more info on your actual problem?
    That "int" on line 3 is sometimes (often) written as the beginning of line 4, before what is already there
    Code:
    int main(void)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 11-14-2011, 06:35 PM
  2. wrong wrong with my xor function?
    By Anddos in forum C++ Programming
    Replies: 5
    Last Post: 04-26-2009, 01:38 PM
  3. What's Wrong With This?
    By pobri19 in forum C Programming
    Replies: 12
    Last Post: 05-26-2008, 10:54 AM
  4. whats wrong with this? no errors but wrong result
    By InvariantLoop in forum C Programming
    Replies: 6
    Last Post: 01-28-2005, 12:48 AM
  5. Replies: 9
    Last Post: 07-15-2004, 03:30 PM