Getting pointer to work with function

This is a discussion on Getting pointer to work with function within the C Programming forums, part of the General Programming Boards category; Why will this code not work? #include <stdio.h> #include <stdlib.h> int newvalue(t); int main(int argc, char *argv[]) { int x ...

  1. #1
    Registered User
    Join Date
    Nov 2003
    Posts
    28

    Question Getting pointer to work with function

    Why will this code not work?

    #include <stdio.h>
    #include <stdlib.h>

    int newvalue(t);

    int main(int argc, char *argv[])
    {
    int x = 3;
    newvalue(&x);
    printf("%i\n",x);
    system("pause");
    }
    int newvalue(t)
    {
    int *t;
    *t = 5;
    return 0;
    }

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,159
    Code:
    int newvalue(t);
    
    int newvalue(t)
    {
    int *t;
    *t = 5;
    return 0;
    }
    Nice try, but no.

    Function prototypes and declarations are designed to let the compiler know what the function expects. Look up function prototypes or look through any of the hundreds of examples on this site that create a function to see the correct way of going about it.
    If you understand what you're doing, you're not learning anything.

  3. #3
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    Because your code does several things that don't make sense. Perhaps you could tell us what you're trying to do and we could show you a better example. You could then look at what's different and see where you went wrong.

    Also, use code tags. Place [/code] and [code] and the end and beginning of your code, respectively (I did it in backwards order so that the code tags wouldn't format my explanation of how to use code tags. They make your code a lot easier to read.

  4. #4
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    Code:
    int main(int argc, char *argv[])
    {
        int x = 3;
        newvalue(&x);
        printf("%i\n",x);
        system("pause");
    }
    
    int newvalue(t)
    {
        int *t;
        *t = 5;
        return 0;
    }
    Pretty cool, eh?

  5. #5
    Registered User
    Join Date
    Nov 2003
    Posts
    28
    ok, what i am trying to do is pass the address of a variable to a function, then change the variable within the fuction by using a pointer to the address of the variabel

  6. #6
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,643
    see if this makes things clearer:

    Code:
    int sum(int a, int b); // prototype
    
    int sum(int a, int b) { // implementation
     int result = a + b;
     return result;
     }
    
    int main(void) {
     printf("%d\n", sum(512, 512));
     }
    
    int sum(int a, int b, int * result) // prototype
    
    int sum(int a, int b, int * result) { // implementation
     *result = a + b;
     return *result;
     }
    
    int main(void) {
     int result;
     sum(512, 512, &result);
     printf("%d\n", result);
     }
    Code:
    if( numeric_limits< byte >::digits != bits_per_byte )
        error( "program requires bits_per_byte-bit bytes" );
    24bbs.cpp

  7. #7
    ---
    Join Date
    May 2004
    Posts
    1,379
    just a few simple fixes
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int newvalue(int *);
    
    int main(int argc, char *argv[])
    {
            int x = 3;
            newvalue(&x);
            printf("%i\n",x);
            system("pause");
            return 0;
    }
    
    int newvalue(int *t)
    {
            *t = 5;
            return 0;
    }

  8. #8
    Registered User
    Join Date
    Nov 2003
    Posts
    28
    thanks sand_man, I am trying to learn to apply pointers without using the books examples, i see how I was thinking wrong now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  4. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  5. function pointer
    By durban in forum C++ Programming
    Replies: 3
    Last Post: 10-08-2005, 04:06 AM

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