BigBug
BigBug

Reputation: 6290

C programming why is junk printing before the value?

I am making a linked list of objects. For some reason when i go to print the value of a value within the object (a char array) i'm getting junk printing before the value. Why is this happening and how do i get rid of it? Here is a part of my code:

    int num = 0;
    int input = 1;
    int retval = 0;
    struct PDB2 *llist;
    char avalue[100] = ""; 
    llist = (struct PDB2 *)malloc(sizeof(struct PDB2));
    llist->data1[100] = NULL;
    llist->next = NULL;

     while(input != 0) {
  printf("\n-- Menu Selection --\n");
  printf("0) Quit\n");
  printf("1) Insert\n");
  printf("2) Delete\n");
  printf("3) Search\n");
  printf("4) Display\n");
  scanf("%d", &input);

  switch(input) {
   case 0: 
   default:
    printf("Goodbye ...\n");
    input = 0;
    break;
   case 1:
    printf("Your choice: `Insertion'\n");
    printf("Enter the value which should be inserted: ");

    scanf("%s", &avalue);
    append_node(llist, avalue);
    break;
   case 2:
    printf("Your choice: `Deletion'\n");
    printf("Enter the value which should be deleted: ");
    scanf("%s", &avalue);
    delete_node(llist, avalue);
    break;
   case 3:
    printf("Your choice: `Search'\n");
    printf("Enter the value you want to find: ");
    scanf("%s", &avalue);
    if((retval = search_value(llist, avalue)) == -1)
     printf("Value `%s' not found\n", avalue);
    else
     printf("Value `%s' located at position `%d'\n", avalue, retval);
    break;
   case 4:
    printf("You choice: `Display'\n");
    display_list(llist);
    break;
   } /* switch */
  } /* while */

 free(llist);
 return(0);
}


    void append_node(struct PDB2 *llist, char message[])
{
    int x = 0; 
     while(llist->next != NULL)
     {
         llist = llist->next;
     }

     llist->next = (struct PDB2 *)malloc(sizeof(struct PDB2));

     for(x = 0; x < 100; x++)
     {
        llist->next->data1[x] = message[x]; 
     }
     llist->next->next = NULL;

}
void display_list(struct PDB2 *llist)
{
    while(llist->data1 == NULL)
    {
        llist = llist->next; 
    }
    while(llist->next != NULL) 
    {
      printf("%s ", llist->data1);
      llist = llist->next;
    }
     printf("%s", llist->data1);
}

Upvotes: 0

Views: 1120

Answers (3)

Brian L
Brian L

Reputation: 3251

The important part of your code (based on your description of the problem) can be boiled down to this:

struct PDB2 {
    char data[100];
    ...other fields...
};

struct PDB2 * llist = (struct PDB2 *)(malloc(sizeof(struct PDB2)));

llist->data[100] = NULL; /* This is wrong */

printf("%s", llist->data); /* This prints rubbish */

The marked line is wrong because:

  1. NULL is meant to be used as a pointer value, not as a character. You probably mean to use '\0'.
  2. The point seems to be to clear the string, so the correct method is

.

llist->data[0] = '\0';

Also, the call to scanf seems that it should be

char avalue[100];

scanf("%s", avalue);

since avalue is already of type char *. With the extra & you may be writing to an invalid location. However,

int input;

scanf("%d", &input);

is correct for integer input.

Upvotes: 0

abelenky
abelenky

Reputation: 64672

These two lines, together, are wrong:

char avalue[100] = ""; 
[...]
scanf("%s", &avalue);

The proper way to scanf a string is:

scanf("%s", avalue);  // No Address-Of operator.

Upvotes: 1

Lambda Fairy
Lambda Fairy

Reputation: 14676

The char array in your initial node is uninitialized. Change

llist->data1[100] = NULL;

to

llist->data1[0] = '\0';

to stop it from being printed.

Upvotes: 1

Related Questions