Thread: non initialized warnings??

  1. #1
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751

    non initialized warnings??

    I'm not sure if I should post this on the DevC++ forum on sourceforege but whatever, maybe it'll help someone.

    I recently had the following code to compute an the average in an array's rows
    PHP Code:
    double RowAvg(const double arr[][CLM], int row){
        
    /* computes average of each set of numbers. using 1 demensional array method */
        
    int i;
        
    double avg;
        for(
    0i<CLMi++)
            
    avg+= arr[row][i]; /* this gives the given row and adds all elements in that row */
         
        
    avg avg CLM;   
        return 
    avg

    for the life of me I couldn't figure out why the ouput would be ouptut like this
    PHP Code:
    Enter row 0 element 0 122
    Enter row 0 element 1 
    233.3
    Enter row 0 element 2 
    33.5
    Enter row 1 element 0 
    233
    Enter row 1 element 1 
    93
    Enter row 1 element 2 
    233
    This 
    array contains:

    Row 0:  122.000 233.300 33.500
    Row 1
    :  233.000 93.000 233.000
    The avg of row 1 is 264764614254408340000000000000000000000000000000000000000000
    00000000000000000000000000000000000000000000000000000000000000000000000000000000
    00000000000000000000000000000000000000000000000000000000000000000000000000000000
    0000000000000000000000000000000000000000000000000.000
    The avg of row 2 is 882548714181361150000000000000000000000000000000000000000000
    00000000000000000000000000000000000000000000000000000000000000000000000000000000
    00000000000000000000000000000000000000000000000000000000000000000000000000000000
    000000000000000000000000000000000000000000000000.000
    The avg of row 3 is 294182904727120370000000000000000000000000000000000000000000
    00000000000000000000000000000000000000000000000000000000000000000000000000000000
    00000000000000000000000000000000000000000000000000000000000000000000000000000000
    000000000000000000000000000000000000000000000000.000
    The avg of the total 
    array is 315.933 
    when i finally tracked the problem down, it was so simple I wanted to smack my self. Most of you know but I had for gotten to initialize avg to 0. THAT'S IT.

    I Understand C's philosophy is not to get in the way of the programmer, but wow. in another language if i had done lets say[
    PHP Code:
    #language unknown :D 
    >>> = [1,2,3,4,5,6,7]
    >>> for 
    num in l:
        
    avg+=num
        avg2 
    avg len(l

    I'd get this output
    PHP Code:
    Traceback (most recent call last):
      
    File "<pyshell#4>"line 2in -toplevel-
        
    avg+=num
    NameError
    name 'avg' is not defined 
    really simple??? I guess my beef is: is it too much to ask for C to help me out once in awhile. i mean I know it has it's idiosyncasies but sheesh!!!..

    that's my two cents, my ten cents is free.
    Warning: Opinions subject to change without notice

    The C Library Reference Guide
    Understand the fundamentals
    Then have some more fun

  2. #2
    ---
    Join Date
    May 2004
    Posts
    1,379
    In C you have the power to do what you want
    C thought you didnt want avg initialised.

    what compiler are you using? maybe up the warning levels

  3. #3
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751
    Quote Originally Posted by sand_man
    In C you have the power to do what you want
    C thought you didnt want avg initialised.

    what compiler are you using? maybe up the warning levels

    I'm using dev C++ 4.9.8.10

    I can accept the responsibilty. I just figue a little help would be good now and then
    Warning: Opinions subject to change without notice

    The C Library Reference Guide
    Understand the fundamentals
    Then have some more fun

  4. #4
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    In Dev-C++:
    Tools->Compiler Options
    Add the following to the "Add the following commands when calling compiler" field:
    Code:
    -Wuninitialized -O
    Then you will get:
    Code:
    C:/.../Untitled1.cpp: In 
       function `double RowAvg(const double (*)[10], int)':
    C:/.../Untitled1.cpp:6: warning: `
       double avg' might be used uninitialized in this function
    For a discussion of this and other GCC warning options see the GCC warning options page.

    The general options page may also be helpful.

  5. #5
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751
    niceeeee....thanks man.

    you'd think they would just have these warnings installed but i suppose they just go along with the C and unix philosopy. Don't get in the programmers way.

    Still, its nice to know. Thanks again guys.
    Maybe that'll be one of my projects when i'm good enough. ARealDecent Compiler. or call it BabyC or something.
    Last edited by caroundw5h; 07-26-2004 at 10:36 PM.
    Warning: Opinions subject to change without notice

    The C Library Reference Guide
    Understand the fundamentals
    Then have some more fun

  6. #6
    Quote Originally Posted by caroundw5h
    you'd think they would just have these warnings installed but i suppose they just go along with the C and unix philosopy. Don't get in the programmers way.

    Maybe that'll be one of my projects when i'm good enough. ARealDecent Compiler. or call it BabyC or something.
    Dev-C++ is good. You just need to learn how to configure it.

    project / options / compiler / Command line ...

    I suggest

    -W -Wall -O2

    as a minimum.
    Emmanuel Delahaye

    "C is a sharp tool"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Warnings when using vector of vector
    By Boksha in forum C++ Programming
    Replies: 5
    Last Post: 03-29-2008, 01:54 PM
  2. Compilers and warnings
    By rogster001 in forum C Programming
    Replies: 6
    Last Post: 03-26-2008, 05:16 AM
  3. Replies: 9
    Last Post: 03-14-2008, 09:55 AM
  4. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM