pointer

This is a discussion on pointer within the C Programming forums, part of the General Programming Boards category; hello guys, I have this code: Code: #include <stdlib.h> #include <stdio.h> int *integer(int *a, int *p); int main(){ int k ...

  1. #1
    Registered User
    Join Date
    Jun 2010
    Posts
    2

    pointer

    hello guys, I have this code:

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    int *integer(int *a, int *p);
    
    int main(){
    	int k = 5;
    	int *p = &k;
    	int n = 10;
    	int m = 30;
    	p = (&m, &n);
    	printf("p is: %d, k is : %d \n", *p, k);
    	
    }
    
    int *integer(int *a, int *p){
    	if(*a  > *p ){
    		return a;
    	}
    	else {
    		return p;
    	}
    
    }
    My question is: isn't that supposed to print out 30 ? (because 30 > 10) , but when I run it , it prints out 10.

    Thanks guys

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,820
    Look at this line:
    Code:
    p = (&m, &n);
    You forgot to call the function, i.e., to write:
    Code:
    p = integer(&m, &n);
    As such, what happened is that &m was evaluated, then &n was evaluated, and the result of (&m, &n) was &n, hence *p is 10, since n is 10.
    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
    Registered User
    Join Date
    Jun 2009
    Posts
    466
    Code:
    	p = (&m, &n);
    Is that supposed to be a function call? If it is, you forgot to name the function you are calling.

    EDIT: too slow

  4. #4
    Registered User
    Join Date
    Jun 2010
    Posts
    2
    ya that is supposed to be a function call . wow, you guys are too fast in responding ..

    Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointer to a function pointer
    By @nthony in forum C Programming
    Replies: 3
    Last Post: 05-30-2010, 05:13 PM
  2. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  3. Quick Pointer Question
    By gwarf420 in forum C Programming
    Replies: 15
    Last Post: 06-01-2008, 03:47 PM
  4. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM

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