Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > void pointers and scanf/printf

Reply
Thread Tools

void pointers and scanf/printf

 
 
pereges
Guest
Posts: n/a
 
      07-20-2008
have i written this program correctly ?

i'm a bit suspicious about the statement :scanf("%d", ptr->data)



#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);
}
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
What is the difference between void proba(); and void proba(void); ??? PencoOdStip@gmail.com C++ 1 05-23-2007 07:12 PM
what is the difference, void func(void) and void fucn() noblesantosh@yahoo.com C Programming 5 07-22-2005 04:38 PM
void pointers & void function pointers Peter Goddard C Programming 3 05-16-2005 09:44 PM
"void Method()" vs "void Method(void)" Ollej Reemt C++ 7 04-22-2005 03:47 AM
`void **' revisited: void *pop(void **root) Stig Brautaset C Programming 15 10-28-2003 09:03 AM



Advertisments