Thread: have i written this program correctly ?

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    87

    have i written this program correctly ?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    typedef struct node_s
    {
            void *data;
            struct node_s *next;
    
    }node;
    
    
    int add_to_link_list(node **head)
    {
            node *ptr;
            int temp;
    
            ptr = malloc(sizeof(node));
    
            if (ptr == NULL)
            {
                    fprintf(stderr, "Memory allocation failed\n");
                    return (1);
            }
    
            printf("Enter data\n");
            if (scanf("%d", &temp) != 1)
            {
                    fprintf(stderr, "Error while entering data\n");
                    return (1);
            }
    
            ptr->data = malloc(sizeof(int));
            if (ptr->data == NULL)
            {
                    fprintf(stderr, "Memory allocation failed\n");
                    return (1);
            }
    
            (int) ptr->data = temp;
            ptr->next = *head;
            *head = ptr;
    
            return (0);
    }
    
    int main(void)
    {
            node *head = NULL;
            node *ptr;
            int n, i;
    
            printf("How many numbers\n");
            if (scanf("%d", &n) != 1)
            {
                    fprintf(stderr, "Error while enterning list size\n");
                    return (EXIT_FAILURE);
            }
    
            for (i = 0; i < n; i++)
            {
                    if (add_to_link_list(&head))
                    {
                            fprintf(stderr, "add_to_link_list failed\n");
                            return (EXIT_FAILURE);
                    }
            }
    
            ptr = head;
    
            while (ptr != NULL)
            {
                    printf("%d\n", ptr->data);
                    ptr = ptr->next;
            }
    
            return (EXIT_SUCCESS);
    }
    btw i didn't understand how the statement

    printf("%d\n", ptr->data) was acceptable. The compiler did not warn me to do a typecast or anything. Can I get away witht his while using scanf as well ? scanf("%d", &ptr->data) ?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    This is what your compiler should have told you:
    Code:
    temp.c: In function `add_to_link_list':
    temp.c:39: warning: use of cast expressions as lvalues is deprecated
    temp.c: In function `main':
    temp.c:72: warning: int format, pointer arg (arg 2)

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    87
    Quote Originally Posted by tabstop View Post
    This is what your compiler should have told you:
    Code:
    temp.c: In function `add_to_link_list':
    temp.c:39: warning: use of cast expressions as lvalues is deprecated
    temp.c: In function `main':
    temp.c:72: warning: int format, pointer arg (arg 2)

    I changed my program to the following and its working perfectly well :

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    typedef struct node_s
    {
            void *data;
            struct node_s *next;
    
    }node;
    
    
    int add_to_link_list(node **head)
    {
            node *ptr;
            int temp;
    
            ptr = malloc(sizeof(node));
    
            if (ptr == NULL)
            {
                    fprintf(stderr, "Memory allocation failed\n");
                    return (1);
            }
    
            ptr->data = malloc(sizeof(int));
            if (ptr->data == NULL)
            {
                    fprintf(stderr, "Memory allocation failed\n");
                    return (1);
            }
    
            printf("Enter data\n");
            if (scanf("%d", &ptr->data) != 1)
            {
                    fprintf(stderr, "Error while entering data\n");
                    return (1);
            }
            
            ptr->next = *head;
            *head = ptr;
    
            return (0);
    }
    
    int main(void)
    {
            node *head = NULL;
            node *ptr;
            int n, i;
    
            printf("How many numbers\n");
            if (scanf("%d", &n) != 1)
            {
                    fprintf(stderr, "Error while enterning list size\n");
                    return (EXIT_FAILURE);
            }
    
            for (i = 0; i < n; i++)
            {
                    if (add_to_link_list(&head))
                    {
                            fprintf(stderr, "add_to_link_list failed\n");
                            return (EXIT_FAILURE);
                    }
            }
    
            ptr = head;
    
            while (ptr != NULL)
            {
                    printf("%d\n", ptr->data);
                    ptr = ptr->next;
            }
    
            return (EXIT_SUCCESS);
    }

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Define "perfectly well". Your print statements should be printing, not garbage necessarily, but not actual values.

  5. #5
    Registered User
    Join Date
    Apr 2008
    Posts
    87
    Quote Originally Posted by tabstop View Post
    Define "perfectly well". Your print statements should be printing, not garbage necessarily, but not actual values.
    They print actual values correctly.

    I'm using digital mars compiler and used the -a option(strict ansi c/c++) and got no warnings or errors.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Somehow, you're still suppressing warnings (i.e., you have a valid program, but it doesn't do what you think it does) -- and here you have two warnings that cancel out:
    Code:
    temp.c: In function `add_to_link_list':
    temp.c:33: warning: int format, pointer arg (arg 2)
    temp.c:15: warning: unused variable `temp'
    temp.c: In function `main':
    temp.c:71: warning: int format, pointer arg (arg 2)
    So your pointer isn't valid, you're storing the value of the integer as the value of the pointer.

  7. #7
    Registered User
    Join Date
    Apr 2008
    Posts
    87
    yes, i realised that error.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    typedef struct node_s
    {
            void *data;
            struct node_s *next;
    
    }node;
    
    
    int add_to_link_list(node **head)
    {
            node *ptr;
        
            ptr = malloc(sizeof(node));
            if (ptr == NULL)
            {
                    fprintf(stderr, "Memory allocation failed\n");
                    return (1);
            }
    
            ptr->data = malloc(sizeof(int));
            if (ptr->data == NULL)
            {
                    fprintf(stderr, "Memory allocation failed\n");
                    return (1);
            }
    
            printf("Enter data\n");
            if (scanf("%d", ptr->data) != 1)
            {
                    fprintf(stderr, "Error while entering data\n");
                    return (1);
            }
            
            ptr->next = *head;
            *head = ptr;
    
            return (0);
    }
    
    int main(void)
    {
            node *head = NULL;
            node *ptr;
            int n, i;
            int *temp;
            
            printf("How many numbers\n");
            if (scanf("%d", &n) != 1)
            {
                    fprintf(stderr, "Error while enterning list size\n");
                    return (EXIT_FAILURE);
            }
    
            for (i = 0; i < n; i++)
            {
                    if (add_to_link_list(&head))
                    {
                            fprintf(stderr, "add_to_link_list failed\n");
                            return (EXIT_FAILURE);
                    }
            }
    
            ptr = head;
          
            while (ptr != NULL)
            {
                    temp = (int *)ptr->data;
                    printf("%d\n",*temp);
                    ptr = ptr->next;
            }
    
            return (EXIT_SUCCESS);
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program Plan
    By Programmer_P in forum C++ Programming
    Replies: 0
    Last Post: 05-11-2009, 01:42 AM
  2. Client-server system with input from separate program
    By robot-ic in forum Networking/Device Communication
    Replies: 3
    Last Post: 01-16-2009, 03:30 PM
  3. Replies: 10
    Last Post: 09-07-2008, 11:38 PM
  4. insufficient memory for tsr
    By manmohan in forum C Programming
    Replies: 8
    Last Post: 01-02-2004, 09:48 AM
  5. Date program starts DOS's date
    By jrahhali in forum C++ Programming
    Replies: 1
    Last Post: 11-24-2003, 05:23 PM