function & pointer

This is a discussion on function & pointer within the C Programming forums, part of the General Programming Boards category; Hi I've got this type of problem.. In the main i got this: Code: int main(void){ int *myint; myint = ...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    32

    function & pointer

    Hi

    I've got this type of problem.. In the main i got this:

    Code:
    int main(void){
      int *myint;
    
      myint = (int *)60;
      myfunc(myint);
      printf("RETURNED%d\n",myint);
    the function is then

    Code:
    myfunc(int *int){
       int mynewintvalue; 
    }
    I need to show the value of myint which was set in the given function in the main.... I'm finding some problems doing it as the way im doing it works for the function but in main it returns the original value... :s

    Thanks beforehand

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Code:
    myint = (int *)60;
    Is pointless.

    Code:
    myfunc(int *int){
    Note that int is a C-Keyword and cannot be used as a variable name.

    Something like?

    Code:
    #include <stdio.h>
    
    void myfunc(int *);
    
    int main(void)
    {
    	int myint = 10;
    	printf("Before: &#37;d\n", myint);
    	myfunc(&myint);
    	printf("After: %d\n", myint);
    	return 0;
    }
    
    void myfunc(int * i)
    {
    	*i = 60;
    	return;
    }

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    32
    ok sorted it out... thx!

  4. #4
    Lean Mean Coding Machine KONI's Avatar
    Join Date
    Mar 2007
    Location
    Luxembourg, Europe
    Posts
    444
    It might be good to know why your initial try, using a pointer, didn't succeed. As you might know, a pointer is a memory location that, instead of containing the actual value, contains a memory address and it is that memory address that contains the value:

    Example:

    suppose this is initialized at address 0000, its value is NULL:
    int *myPointer = NULL;

    suppose this is initialized at address 0004, it's value is 10:
    int myValue = 10;

    the address 0000 now contains 0004:
    myPointer = &myValue;

    this reads the value of the address 0000, which is 0004 and then reads the value there, which is 10:
    printf("&#37;d", *myPointer);


    If you want to start with a pointer, you need the memory location that holds the real value. If you only have a pointer, you can use malloc() to create a new memory space and the address to that memory space is saved in your pointer. Then you can use that newly created memory to save the value into:

    Code:
    int *myPointer;
    myPointer = malloc(sizeof(int));
    *myPointer = 10;
    Better code would check the return value of malloc() to see if malloc() was able to allocate new space and sizeof(int) can be written sizeof(*myPointer) to be more general.

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. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 12:03 PM
  3. Function Pointer help
    By Skydt in forum C Programming
    Replies: 5
    Last Post: 12-02-2005, 08:13 AM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM

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