printf output makes no sense. Is it a Pointer problem?

This is a discussion on printf output makes no sense. Is it a Pointer problem? within the C Programming forums, part of the General Programming Boards category; Firstly a quick hello and thanks to the people here for such a helpful resource. I am very new to ...

  1. #1
    Registered User officedog's Avatar
    Join Date
    Oct 2008
    Posts
    77

    printf output makes no sense. Is it a Pointer problem?

    Firstly a quick hello and thanks to the people here for such a helpful resource.

    I am very new to C and have run into a problem with program I'm experimenting with. My guess is it is a very simple issue, but I just cannot figure it out.

    The gist of the program is to create a very simple stats program which has a struct containing the raw data and variables to hold basic descriptive stats (mean, SD, N). And a series of functions to initialise the data, modify and print.

    I'm just learning about pointers and thought I'd try implementing this using pointers as return values.

    In the code below, I wanted to test whether my "initialse struct" function was working so added some printf statements into the main function to check. It seems to print the "n" value from the struct, but all other values were completely bizarre. Rather than copy the whole thing, I reduced the programme down to the code below. It still replicates the problem (the mean value is plain wrong).

    I have tried various work arounds. Somewhat bizarrely (i.e. I have no idea why it works) if I insert a printf statement into the body of the initialisation function, it seems to print OK.

    Here is the out put I got from running that code. Am working using XCode (standard tool)

    New dataset, n = 101
    New dataset, mean = -12978830180352.000000
    The Debugger has exited with status 0.
    Any help would be really appreciated....

    Dave


    Code:
    #include <stdio.h>
    
    struct dataSet
    {
        struct dataSet * pdataSet;
        int n;
        float mean;
        float sd;
        int data[100];
    } ;
    
    struct dataSet * initDataSet(int n);
    
    
    int main () 
    {
        int n = 5;
        struct dataSet * pNewDataSet =  initDataSet(n);
        
        printf("\nNew dataset, n = %d", (*pNewDataSet).n);
        printf("\nNew dataset, mean = %f", (*pNewDataSet).mean);
        
        return 0;
    }
    
    
    struct dataSet * initDataSet(int n)
    {
        struct dataSet dataSet1;
        
        dataSet1.pdataSet = &dataSet1;  //own address    
        dataSet1.n = 101;
        dataSet1.mean = 8;
        
        return dataSet1.pdataSet;  // return a pointer 
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    dataSet1 only lives as long as the function initDataSet is still running; once it stops, the dataSet1 variable dies with it. However, you're still holding a pointer to it, which now points into the deepest abyss. It is luck (good or bad, you decide) that the n printed correctly.

    If you need a piece of memory to still be around after a function exits, you would need to acquire that piece of memory with malloc.

  3. #3
    Registered User officedog's Avatar
    Join Date
    Oct 2008
    Posts
    77
    That is a very helpful reply. Thanks

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,185
    Quote Originally Posted by tabstop View Post
    If you need a piece of memory to still be around after a function exits, you would need to acquire that piece of memory with malloc.
    It should be noted that anything acquired with malloc must be freed with free. If you do not, it will stay there for the rest of the program and eat up senseless memory.
    Therefore, do not forget to free what you malloc!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

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. Advice requested, Code makes sense to me, not compiler
    By andrew.bolster in forum C Programming
    Replies: 53
    Last Post: 01-06-2008, 12:44 PM
  3. error in program????
    By SpEkTrE in forum C Programming
    Replies: 5
    Last Post: 11-24-2003, 05:16 PM
  4. Drawing tables in C
    By stanoman in forum C Programming
    Replies: 5
    Last Post: 10-09-2003, 10:14 AM
  5. problem with output
    By Garfield in forum C Programming
    Replies: 2
    Last Post: 11-18-2001, 07:34 PM

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