Lamberto Basti
Lamberto Basti

Reputation: 476

The printf functions print something that shouldn't, why?

As you can see from the images attached below, adding the second element from the top of the list create a strange behaviour for the printf function.

the function that add a list node is:

void add_QUEUEnode_top(Item a)
{
    if (head==NULL)
    {
        QUEUEinit(a);
    }
    else
    {
        QUEUEput_top(a);
    }
    return;
}

void QUEUEinit(Item a)
{
    head=(link)malloc(sizeof(link*));
    head->next=NULL;
    head->item=a;
    tail=head;
    printf("Coda iniziallizata...\n\n");

}

void QUEUEput_top(Item a)
{
    link tmp;
    tmp=(link)malloc(sizeof(link*));
    tmp->item=a;
    tmp->next=head;
    head=tmp;
    return;
}

Here the functions that handle the items:

Item fill_item()
{
    Item a;
    int i;
    for(i=0; i<DIM-1; i++)
    {
        a.stringa[i]=rand();
    }
    a.stringa[DIM-1]='\0';
    a.numero=rand();
    printf("\nOggetto generato: \n");
    print_item(a);
    return a;
}

void print_item(Item a)
{
    printf("\nStringa elemento: ");
    printf("%s", a.stringa);
    printf("\nNumero elemento:  %d\n", a.numero);
}

Here's the link to the codeblock project i'm editing. The function print_item(Item a) that call the "bugged" printf is in the item.c module, while the functions that generate a list item are inside the list.c module.

Any idea of what can cause this problem?

entering the first element of the stack

and that is what happen at the second element

PS: i'm sorry for the captures located in italian

EDIT: definition of the items:

typedef struct
{
    char stringa[DIM];
    int numero;
} Item;

Definition of the link pointer:

typedef struct QUEUEnode *link;

definition of the link structure:

struct QUEUEnode
    {
        Item item;
        link next;
    };

Upvotes: 1

Views: 209

Answers (2)

Carl Norum
Carl Norum

Reputation: 225142

You have several bugs - I'll try to go through and find more of them for you, but the specific problem you're asking about is related to this code:

for(i=0; i<DIM-1; i++)
{
    a.stringa[i]=rand();
}
a.stringa[DIM-1]='\0';

You're just putting a random number in each character - many of them may not be interesting or even printable characters in your character set. That's why you get the crazy output you're seeing. If you want to put random printable characters into the string, do so in a more character-set-aware way.

Some more problems:

  1. These allocation lines are wrong:

    head=(link)malloc(sizeof(link*));
    tmp=(link)malloc(sizeof(link*));
    

    They should be:

    head = malloc(sizeof(struct QUEUEnode));
    tmp = malloc(sizeof(struct QUEUEnode));
    

    That is, you should allocate enough size for an entire struct QUEUEnode, not just a pointer to one. (Or a pointer to a pointer to one, which is what you had). Hiding the pointer type inside a typedef like you do with typedef struct QUEUEnode *link; is one of those controversial style choices - I personally prefer not to, since it confuses issues like this one pretty fast.

  2. You're passing around a lot of structures by value in this program. That's fine and valid C, it's just a bit non-idiomatic. Normally people pass around pointers instead. If your structure grows to any appreciable size, performance can start to suffer badly as a result of all of the implicit memory copying going on.

Upvotes: 4

David Heffernan
David Heffernan

Reputation: 613451

You are allocating random text to stringa and then setting numero to a random number. When you print those things you get, well, random output. Because you have not restricted the randomisation to printable characters weird things happen. You are printing control characters. Your second screenshot would indicate that you have printed a carriage return character. I'm sure you don't want to do that!

The program is doing exactly what you asked it to do. Since you did not say what set of characters you wanted to assign to stringa from, I really could not say what your program should be. You could make it choose values in the range A to Z with this change:

a.stringa[i] = 'A' + rand() % 26;

I'll bet that there are other problems with your code, but since the main focus of the question concerned printing, I'll not look any deeper.

Upvotes: 2

Related Questions