Structs - garbage printing

This is a discussion on Structs - garbage printing within the C Programming forums, part of the General Programming Boards category; Hi, Im having a problem with having the information within a struct pritning properly. The function returns a pointer to ...

  1. #1
    Microsoft. Who? MethodMan's Avatar
    Join Date
    Mar 2002
    Posts
    1,198

    Structs - garbage printing

    Hi,

    Im having a problem with having the information within a struct pritning properly.

    The function returns a pointer to a record.

    recordptr = search(&abc);

    then I do the following:

    strcpy(temp, record_ptr->date);
    printf("%s", temp);

    I was trying to print out a portion of the sturct as above, but all I get is junk. Is this the correct approach, or does the problem lie somewhere else?

    Thanks
    -MethodMan-

    Your Move:Life is a game, Play it; Life is a challenge, Meet it; Life is an opportunity, capture it.

    Homepage: http://www.freewebs.com/andy_moog/home.html

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Without seeing the actual code its very hard to say where the problem is. However if the code you gave is an example of your coding then I see a major problem.

    recordptr = search(&abc);
    strcpy(temp, record_ptr->date);

  3. #3
    Microsoft. Who? MethodMan's Avatar
    Join Date
    Mar 2002
    Posts
    1,198
    Yeah, sorry, that was a typo.

    The function is fairly large, any specific code that would help identify the problem?

    Thanks
    -MethodMan-

    Your Move:Life is a game, Play it; Life is a challenge, Meet it; Life is an opportunity, capture it.

    Homepage: http://www.freewebs.com/andy_moog/home.html

  4. #4
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Well like all problems try to export the parts that deal with that struct and make some test programs to help isolate the problem

  5. #5
    Microsoft. Who? MethodMan's Avatar
    Join Date
    Mar 2002
    Posts
    1,198
    Within the search function, I put the struct into a file, with the code below, and it saved it the file correctly.

    fputs(record_ptr->date, fp);

    I then return the struct containing the info as below.

    return ((record *)record_ptr);

    I then need to print the information contained within the struct, but I am then getting the garbage.
    -MethodMan-

    Your Move:Life is a game, Play it; Life is a challenge, Meet it; Life is an opportunity, capture it.

    Homepage: http://www.freewebs.com/andy_moog/home.html

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Are you trying to return a local variable?
    Code:
    struct foo *function( void )
    {
        struct foo bar;
    
        return &bar;
    }
    Because if you are, and it isn't something you've just allocated, then it's gone when the function ends.

    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Microsoft. Who? MethodMan's Avatar
    Join Date
    Mar 2002
    Posts
    1,198
    Within the function I declare the pointer to the struct, and use malloc for every item within the struct as below.

    static record *record_ptr = NULL;
    record_ptr = (record *)malloc(sizeof(record));

    And then I return it with the code I showed previously.

    Thanks
    -MethodMan-

    Your Move:Life is a game, Play it; Life is a challenge, Meet it; Life is an opportunity, capture it.

    Homepage: http://www.freewebs.com/andy_moog/home.html

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Well for starters, you don't need to keep typcasting everything. If your function is set up to return a specific item, there's no need to type cast it to that. Example:
    Code:
    int myfunction( void )
    {
        int x = 10;
    
        return x; /* Right. */
    
        return (int)x; /* No need to typecast. */
    }
    Also, you shouldn't typecast malloc, as again, there is no need to do so. If you're getting warnings about it, it's because you're not compiling as C and are instead compiling as C++. (I believe it's covered in the FAQ.)

    Anyway... Run this test code, compare it to your own, and find your problem, since you haven't really given us anything to go on:
    Code:
    #include <stdio.h>
    
    struct quickhack {
        int x;
    };
    
    struct quickhack *function( int y );
    
    int main( void )
    {
        struct quickhack *q = NULL;
    
        q = function( 5 );
    
        if( q )
        {
            printf("q->x = %d\n", q->x );
            free( q );
        }
        return 0;
    }
    
    struct quickhack *function( int y )
    {
        struct quickhack *p = NULL;
        p = malloc( sizeof( *p ) );
    
        if( p )
        {
            p->x = y;
        }
        return p;
    }
    Is that what you're trying to do?

    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    Microsoft. Who? MethodMan's Avatar
    Join Date
    Mar 2002
    Posts
    1,198
    Maybe I should have been a little clearer. I am returning the entire struct. I was just showing bits of code on how I was used malloc for a particular item within the struct.

    Within my search function, I use fputs, as shown previously, and everything is put into the file correctly.

    The problem comes when I try to print it after the search is finished.

    record_ptr = search(&abc);

    //I would print all items in the struct, this is just printing one.
    strcpy(temp, record_ptr->date);
    -MethodMan-

    Your Move:Life is a game, Play it; Life is a challenge, Meet it; Life is an opportunity, capture it.

    Homepage: http://www.freewebs.com/andy_moog/home.html

  10. #10
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Look at the following:
    I am returning the entire struct.
    record_ptr = search(&abc);
    strcpy(temp, record_ptr->date);
    So are you returning the entire struct or a pointer to the struct?

  11. #11
    Microsoft. Who? MethodMan's Avatar
    Join Date
    Mar 2002
    Posts
    1,198
    A pointer to a struct.
    -MethodMan-

    Your Move:Life is a game, Play it; Life is a challenge, Meet it; Life is an opportunity, capture it.

    Homepage: http://www.freewebs.com/andy_moog/home.html

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by MethodMan
    The problem comes when I try to print it after the search is finished.

    record_ptr = search(&abc);

    //I would print all items in the struct, this is just printing one.
    strcpy(temp, record_ptr->date);
    Of course it only prints one. "date" is the only thing you're copying. What is "date"? I'll assume it's a string containing a date. As such, all you're doing is copying one string to another. That's all. Nothing else.

    Look again at my example. That is "returning an entire struct". If you're not doing it like that, you're probably doing it wrong. Post:
    1) Your structure definition.
    2) Your printing lines, and the few lines preceeding it.

    Better yet, take my example, replace quickhack with your structure name and definition, and modify the printf line(s) to print each member of the structure one at a time.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Printing an array of structs?
    By blernblan in forum C Programming
    Replies: 4
    Last Post: 04-28-2009, 03:04 PM
  2. C# Printing Problem
    By silverlight001 in forum C# Programming
    Replies: 0
    Last Post: 03-23-2009, 01:13 AM
  3. Replies: 3
    Last Post: 04-19-2004, 06:42 PM
  4. Segmentation Fault printing an array of structs
    By ccoder01 in forum C Programming
    Replies: 1
    Last Post: 04-17-2004, 07:03 AM
  5. Searching structs...
    By Sebastiani in forum C Programming
    Replies: 1
    Last Post: 08-25-2001, 12:38 PM

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