Thread: pointer int math wrong

  1. #1
    Registered User
    Join Date
    Jul 2002
    Posts
    913

    pointer int math wrong

    ive been having alot of fishy problems lately

    if i run this it adds the numbers wrong. i tried adding a * to it but it didnt help any.

    Code:
    int main() {
    	int x = 1;
    	
    	test(x);
    	
    	return 0;
    }
    
    int test(int *x) {
    	printf("X = %i\n", x);
    	++x;
    	printf("X Now = %i", x);
    	
    	return 0;
    }
    i get back
    X = 1
    X Now = 5
    i want
    X Now = 2

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    You have a few problems with your code. Here is what it should look like.

    Code:
    #include <stdio.h>
    
    int test( int *x );
    
    int main() {
    	int x = 1;
    	
    	test(&x);
    	
    	return 0;
    }
    
    int test(int *x) {
    	printf("X = %i\n", *x);
    	++(*x);
    	printf("X Now = %i", *x);
    	
    	return 0;
    }
    When you pass a regular variable to a function as a pointer you need to pass the address of it. Also you have to dereference the pointer to get the value located there. Hope this helps.

    EDIT: I don't know how it even compiled for you. There are several errors that should occur. If you just print out x in the test function without dereferencing you will get the address not the value.

  3. #3
    Registered User
    Join Date
    Jul 2002
    Posts
    913
    i didnt get why it compiled either, it didnt seem right. but how can 1 be the adress? i thought they took a completly diff form(i was thinking hex) and wouldnt my kernel take up alot of the begining?

    thanks

  4. #4
    Registered User
    Join Date
    Jul 2002
    Posts
    913
    i just compiled your code, seg fault.

    now im really confused.

  5. #5
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    What compiler are you using??

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by mart_man00
    i just compiled your code, seg fault.

    now im really confused.
    Did you cut/paste the code, or re-type it?
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  7. #7
    Registered User
    Join Date
    Jul 2002
    Posts
    913
    i copy and pasted it into vi, but instead of typing :q i had caps on and did :Q. sorry

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. Working with random like dice
    By SebastionV3 in forum C++ Programming
    Replies: 10
    Last Post: 05-26-2006, 09:16 PM
  3. Converted from Dev-C++ 4 to Dev-C++ 5
    By Wraithan in forum C++ Programming
    Replies: 8
    Last Post: 12-03-2005, 07:45 AM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 05:26 PM