Thread: Pointers and Function calls

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    6

    Pointers and Function calls

    Hi,

    I'm learning about pointers from a c programming book, and I tried to do some of the exercise problems.

    In the book, every 'program' is written as one function with a main. But I'm trying to combine multiple 'programs' as functions, then call them in main. Trouble is, a lot of these functions take pointers as args, so I'm not sure how to call them in main with actual values.

    I put these functions inside of main [to try to get around the problem] but it cannot compile. I'm getting 'need ; before {' errors in main, but ofc I checked and don't need any ;'s before {'s.

    My code:
    Code:
    int division(int numerator, int denominator, int *dividend, int *remainder) {
    
      printf("address stored in dividend: %u\n",dividend);
      printf("address stored in remainder: %u\n",remainder);
      if (denominator == 0)
        return(0);
      *dividend=numerator/denominator;
      *remainder=numerator%denominator;
      return(1);
    
    }
    
    main() {
    
      functioncall1();
    
      functioncall2();
    
      functioncall3();
    
      functioncall4() {
        int x, y, d, r;
        x=9;
        y=2;
        printf("address of d: %u\n",&d);
        printf("address of r: %u\n",&r);
        division(x, y, &d, &r);
        printf("%d/%d = %d with %d remainder\n",x,y,d,r);
        printf("x=%d\n",x);
        } 
    
      functioncall5() {
    
        double array[10];
        double *d_ptr;
        double value;
        int i,offset;
        for (i=0; i<10; i++)
          array[i]=(double)i+10.0; /* access memory using array index and fill cells with #s*/
        d_ptr=&(array[0]); /* access memory location of first array cell using pointer*/
        while (1) {
            printf("Address(hex)\tAddress(base10)\tValue\n");
            for (i=0; i<10; i++)
              printf("%p\t%u\t%lf\n",&(array[i]),&(array[i]),array[i]);
            printf("Enter offset value (0 0 to quit): ");
            scanf("%d %lf",&offset,&value);
            if (offset == 0 && value == 0.0)
              break; /* break out of loop */
            if (offset < 0 || offset > 9) {
                printf("Offset out of bounds\n");
                continue; /* go back to start of loop */
              }
            array[offset]=value; /* using array syntax */
          }
        return (0);
        }
    }
    Last edited by Michele Degges; 10-09-2011 at 02:47 PM.

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Please don't be offended... but if you can't figure this out... you need to do some serious reviewing, cause it clearly didn't sink in the first time!

    Some of the problems... you cannot nest functions inside main in C... it will hate you forever. You really need to give your functions descriptive names "GetName" or "ReadDiskFile".... and there's more.


    Also as a little hint... No, you don't always get to just pile stuff in together... code in C is not always "interchangeable" and often you cause more problems than you solve. Best bet, if you want to combine some aspects of one exercise with those of another... here's a project for you... scratch write the thing. I guarantee you'll learn a lot more than you will by copying blocks of code from here to there... (We call that "scoop and poop" coding, btw)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 19
    Last Post: 09-08-2011, 07:56 AM
  2. function calls and pointers
    By davidjg in forum C Programming
    Replies: 14
    Last Post: 01-30-2011, 11:40 PM
  3. why does c++ do function calls like this?
    By sndhearn in forum C++ Programming
    Replies: 5
    Last Post: 07-20-2005, 03:17 AM
  4. function calls
    By Draco in forum C Programming
    Replies: 3
    Last Post: 11-09-2003, 04:28 PM
  5. I need help with passing pointers in function calls
    By vien_mti in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 10:00 AM