Like Tree1Likes
  • 1 Post By Who

printf an int that was defined in another function?

This is a discussion on printf an int that was defined in another function? within the C Programming forums, part of the General Programming Boards category; Hi all, I'm making my way through most of this assignment that I have, but now it seems like I've ...

  1. #1
    Who
    Who is offline
    Registered User
    Join Date
    Jan 2013
    Posts
    21

    Cool printf an int that was defined in another function?

    Hi all,
    I'm making my way through most of this assignment that I have, but now it seems like I've run into a bit of a roadblock. The issue that I'm having is not being able to printf a series of ints that I thought I had previously defined in another function. I don't want to clog up this post with the entire code, so I'll just post one function that defined an int to give an example. I will upload the whole thing upon request however.
    SOO here's what I mean:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    //Prototypes
    int AGrade1(int* grade1);
    int AGrade2(int* grade2);
    int AGrade3(int* grade3);
    int AGrade4(int* grade4);
    int AGrade5(int* grade5);
    int AGrade6(int* grade6);
    int AGrade7(int* grade7);
    int AGrade8(int* grade8);
    int MGrade(int* midterm);
    int FGrade(int* final);
    void printAGrade(int grade1,int grade2,int grade3,int grade4,int grade5,int grade6,int grade7,int grade8);
    int main()
    {
        //Name
        printf("Horton hears a WHO?\n");
        printf("Lab Exercise #12\n");
        printf("Code::Blocks\n");
        printf("\n");
        //Declarations
        int grade1, grade2, grade3, grade4, grade5, grade6, grade7, grade8, midterm, final;
        AGrade1(&grade1);
        AGrade2(&grade2);
        AGrade3(&grade3);
        AGrade4(&grade4);
        AGrade5(&grade5);
        AGrade6(&grade6);
        AGrade7(&grade7);
        AGrade8(&grade8);
        MGrade(&midterm);
        FGrade(&final);
        printAGrade(grade1, grade2, grade3, grade4, grade5, grade6, grade7, grade8);
        return 0;
    }
    int AGrade1(int* grade1)
    {
        //Statements
        printf("Enter 8 assignment grades one at a time: \n");
        printf("-> ");
        scanf("%d", &grade1);
        if (grade1 >= 0 && grade1 <= 20)
        {
            printf("Thanks\n");
            return;
        }
        else
        {
            printf("Invalid points: %d. Maximum allowed is 20\nTry again. ", grade1);
            printf("Enter points -> ");
            scanf("%d", &grade1);
        }
        if (grade1 >= 0 && grade1 <= 20)
        {
            printf("That's better\n");
        }
        else
        {
            printf("Invalid points: %d. Maximum allowed is 20\nExiting program\n", grade1);
            exit (0);
        }
    }
    
    //BLAH BLAH BLAH.. ALOT MORE FUNCTIONS UNTIL..
    
    
    void printAGrade(int grade1,int grade2,int grade3,int grade4,int grade5,int grade6,int grade7,int grade8)
    {
        printf("Assignment Grades:      %d      %d      %d      %d      %d      %d      %d      %d", grade1, grade2, grade3, grade4, grade5, grade6, grade7, grade8);
    }
    I've tried many many things, but I just cant figure it out. :/ Any help greatly appreciated! Just for reference, this is what it's supposed to look like.


    Assignment Grades: 18 12 17 15 20 13
    20 18

  2. #2
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    You pass the variable grade1 to the function Agrade1 as a pointer, but in the function you do not treat it as a pointer.
    Code:
    while(!asleep) {
       sheep++;
    }

  3. #3
    Who
    Who is offline
    Registered User
    Join Date
    Jan 2013
    Posts
    21
    Could you please elaborate on that? I know what you mean, but I'm not sure what I need to do to treat it as a pointer.

    Thanks for your time
    Last edited by Who; 02-26-2013 at 08:49 PM.

  4. #4
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    The fix is simple: in the function Agrade1, wherevery you have &grade1 you need to remove the &, and wherever you have just grade1 you need to add a *:

    grade1 -> *grade1
    &grade1 -> grade1

    Do you understand the reason for these changes?
    Code:
    while(!asleep) {
       sheep++;
    }

  5. #5
    Who
    Who is offline
    Registered User
    Join Date
    Jan 2013
    Posts
    21
    DOH. Absolutely. Sorry for asking you to explain an obvious answer. I should've looked more closely. Thanks for your help!
    TheBigH likes this.

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,546
    Code:
    $ gcc -c foo.c
    foo.c: In function ‘AGrade1’:
    foo.c:42:5: warning: format ‘%d’ expects argument of type ‘int *’, but argument 2 has type ‘int **’ [-Wformat]
    foo.c:43:31: warning: comparison between pointer and integer [enabled by default]
    foo.c:50:9: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘int *’ [-Wformat]
    foo.c:52:9: warning: format ‘%d’ expects argument of type ‘int *’, but argument 2 has type ‘int **’ [-Wformat]
    foo.c:54:31: warning: comparison between pointer and integer [enabled by default]
    foo.c:60:9: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘int *’ [-Wformat]
    Some compilers will tell you when you make a mess of passing parameters to printf/scanf.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    The code is pretty ugly in a few other ways:

    1. grade1... grade8. Why not grade[8]? This is basically what arrays are meant to do.

    2. I think you're entirely missing the point of functions when you made 8 of them to do (presumably) exactly the same thing. As a general rule, if you frequently find yourself copying and pasting, you're probably doing something wrong. This entire problem can be solved in roughly 30 lines of code using arrays, loops, and better designed functions.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  8. #8
    Registered User ledow's Avatar
    Join Date
    Dec 2011
    Posts
    435
    Agreed. As soon as you see this:

    Code:
    int grade1, grade2, grade3, grade4, grade5, grade6, grade7, grade8, midterm, final;
        AGrade1(&grade1);
        AGrade2(&grade2);
        AGrade3(&grade3);
        AGrade4(&grade4);
        AGrade5(&grade5);
        AGrade6(&grade6);
        AGrade7(&grade7);
        AGrade8(&grade8);
        MGrade(&midterm);
        FGrade(&final);
        printAGrade(grade1, grade2, grade3, grade4, grade5, grade6, grade7, grade8);
    you know that someone has misunderstood how things work.

    Yes, a program is a set of instructions laid out in order for a computer to rigorously follow to the letter, one after the other.

    But the whole POINT of writing the program in the first place is to get the computer to do the hard work for you. Thus if you have 8 variables, 8 functions, and a function to print 8 variables, then it suggests you should have done things in blocks of 8 in the first place, rather than write everything out 8 times with slightly different (incremental) numbers each time.

    This is like building a car that has a stick that you have to push into 8 positions to move the wheel 1/8th of the way round. If you push the stick too much or at the wrong time or into the wrong position, the car goes nowhere. If you make a simple error, the car judders to a grinding halt. All the time you are working with the car, everything gets more complex. If you want to go faster or do more, you have to make a bigger stick or do more work yourself to make it do so (and increase the chances of error all the time). The computer is a labour-saving device like the car. It's perfectly capable of counting for you, doing things one after the other in a loop, and saving you having to specify eight similar functions to do eight similar things in order. And every time you don't use it, you look like the idiot with the car that needs an 8-position stick to be constantly adjusted every time the wheels move an inch.

    - Compiler warnings are like "Bridge Out Ahead" warnings. DON'T just ignore them.
    - A compiler error is something SO stupid that the compiler genuinely can't carry on with its job. A compiler warning is the compiler saying "Well, that's bloody stupid but if you WANT to ignore me..." and carrying on.
    - The best debugging tool in the world is a bunch of printf()'s for everything important around the bits you think might be wrong.

  9. #9
    Registered User
    Join Date
    Feb 2013
    Posts
    22
    You could take the declarations of Grade1,Grade2,etc. out of the main function and put them outside the declarations of all the functions. This will make it global and the program should work.

    Hope I'm right!!

  10. #10
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Making all variables global will just make it worse.
    Global Variables Are Bad

    Bye, Andreas

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. User-defined function
    By ulti-killer in forum C Programming
    Replies: 5
    Last Post: 06-10-2012, 05:27 AM
  2. Replies: 35
    Last Post: 12-01-2011, 07:31 PM
  3. How to override standard printf defined in gcc library
    By RahulJain83 in forum C Programming
    Replies: 7
    Last Post: 10-27-2009, 09:23 PM
  4. Error: _ defined as a function returning a function?
    By Jardon in forum C Programming
    Replies: 15
    Last Post: 07-29-2009, 11:53 AM
  5. Replies: 14
    Last Post: 03-02-2008, 12:27 PM

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