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 ?

it is giving me correct output but i am little suspicious in the
following two statements -

scanf("%d", &ptr->data) and printf("%d\n", ptr->data).

/********** LINK LIST **********/

#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);
}
 
Reply With Quote
 
 
 
 
voidpointer
Guest
Posts: n/a
 
      07-20-2008
On Jul 20, 5:53 pm, pereges <(E-Mail Removed)> wrote:
> have i written this program correctly ?
>
> it is giving me correct output but i am little suspicious in the
> following two statements -
>
> scanf("%d", &ptr->data) and printf("%d\n", ptr->data).
>
> /********** LINK LIST **********/
>
> #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);
>
> }


your program don't work, have a segfault, compile with -Wall (if
you're using gcc) and try solve the errors, any doubt post here
 
Reply With Quote
 
 
 
 
pereges
Guest
Posts: n/a
 
      07-20-2008
On Jul 21, 2:23 am, voidpointer <(E-Mail Removed)> wrote:

> your program don't work, have a segfault, compile with -Wall (if
> you're using gcc) and try solve the errors, any doubt post here


Hello, Can you please check the first post again ? I have changed the
code. I got no warnings from my dmc/pellesC compilers and the outpu is
correct.
 
Reply With Quote
 
voidpointer
Guest
Posts: n/a
 
      07-20-2008
On Jul 20, 6:28 pm, pereges <(E-Mail Removed)> wrote:
> On Jul 21, 2:23 am, voidpointer <(E-Mail Removed)> wrote:
>
> > your program don't work, have a segfault, compile with -Wall (if
> > you're using gcc) and try solve the errors, any doubt post here

>
> Hello, Can you please check the first post again ? I have changed the
> code. I got no warnings from my dmc/pellesC compilers and the outpu is
> correct.


Hello, now work but have one warning message,

In function 'add_to_link_list':
test.c:30: warning: format '%d' expects type 'int *', but argument 2
has type 'void *'

you can solve this with a cast in your scanf, like this scanf("%d",
(int*)ptr->data)
and you have a lot of memory leak in your code, you doesn't free()d
the allocated space by malloc,
other thing, if you expect work with int, so use int, because void*
can be hard to work,
and need a lot of cast in your code
 
Reply With Quote
 
voidpointer
Guest
Posts: n/a
 
      07-20-2008
On Jul 20, 6:57 pm, voidpointer <(E-Mail Removed)> wrote:
> On Jul 20, 6:28 pm, pereges <(E-Mail Removed)> wrote:
>
> > On Jul 21, 2:23 am, voidpointer <(E-Mail Removed)> wrote:

>
> > > your program don't work, have a segfault, compile with -Wall (if
> > > you're using gcc) and try solve the errors, any doubt post here

>
> > Hello, Can you please check the first post again ? I have changed the
> > code. I got no warnings from my dmc/pellesC compilers and the outpu is
> > correct.

>
> Hello, now work but have one warning message,
>
> In function 'add_to_link_list':
> test.c:30: warning: format '%d' expects type 'int *', but argument 2
> has type 'void *'
>
> you can solve this with a cast in your scanf, like this scanf("%d",
> (int*)ptr->data)
> and you have a lot of memory leak in your code, you doesn't free()d
> the allocated space by malloc,
> other thing, if you expect work with int, so use int, because void*
> can be hard to work,
> and need a lot of cast in your code


only a correction, use scanf("%d", (int*)&ptr->data), sorry
 
Reply With Quote
 
pereges
Guest
Posts: n/a
 
      07-20-2008
On Jul 21, 3:12 am, voidpointer <(E-Mail Removed)> wrote:

> only a correction, use scanf("%d", (int*)&ptr->data), sorry


but ptr->data is already address then why &ptr->data ?
 
Reply With Quote
 
Ben Bacarisse
Guest
Posts: n/a
 
      07-20-2008
voidpointer <(E-Mail Removed)> writes:

> On Jul 20, 6:28 pm, pereges <(E-Mail Removed)> wrote:
>> On Jul 21, 2:23 am, voidpointer <(E-Mail Removed)> wrote:
>>
>> > your program don't work, have a segfault, compile with -Wall (if
>> > you're using gcc) and try solve the errors, any doubt post here

>>
>> Hello, Can you please check the first post again ? I have changed the
>> code. I got no warnings from my dmc/pellesC compilers and the outpu is
>> correct.

>
> Hello, now work but have one warning message,
>
> In function 'add_to_link_list':
> test.c:30: warning: format '%d' expects type 'int *', but argument 2
> has type 'void *'


Check the code, your compiler or the message you really get. The
argument is of type void **.

> you can solve this with a cast in your scanf, like this scanf("%d",
> (int*)ptr->data)


This won't fix the problem.

--
Ben.
 
Reply With Quote
 
Ben Bacarisse
Guest
Posts: n/a
 
      07-20-2008
pereges <(E-Mail Removed)> writes:

> have i written this program correctly ?
>
> it is giving me correct output but i am little suspicious in the
> following two statements -
>
> scanf("%d", &ptr->data) and printf("%d\n", ptr->data).


Context:
> typedef struct node_s
> {
> void *data;
> struct node_s *next;
>
> }node;


You are right to be. You can't use %d to scanf into a void * and the
reverse is also wrong.

If you want a linked list of ints, then 'data' should be an int. If
you want something more generic, you could use union. More generalt
still comes from keeping the data field as void *, but allocating
storage for it to point to whatever you need to store.

In the Bad Old Days, it was a common trick to smuggle small data types
into a generic list by putting them into the void * (actually I've not
seen it done since void * arrived, but the same applies). It is not a
good idea.

--
Ben.
 
Reply With Quote
 
neobakuer
Guest
Posts: n/a
 
      07-20-2008
On Jul 20, 7:15 pm, pereges <(E-Mail Removed)> wrote:
> On Jul 21, 3:12 am, voidpointer <(E-Mail Removed)> wrote:
>
> > only a correction, use scanf("%d", (int*)&ptr->data), sorry

>
> but ptr->data is already address then why &ptr->data ?


this is only to avoid the warning message.

But as said voidpointer and Ben, if you want work with int, use int.
 
Reply With Quote
 
Ben Bacarisse
Guest
Posts: n/a
 
      07-20-2008
pereges <(E-Mail Removed)> writes:

> On Jul 21, 3:12 am, voidpointer <(E-Mail Removed)> wrote:
>
>> only a correction, use scanf("%d", (int*)&ptr->data), sorry

>
> but ptr->data is already address then why &ptr->data ?


You some problems if you are asking this. I thought you'd written

scanf("%d", &ptr->data)

because you were knowingly breaking the type rules to put an int into
value into a void * object.

Both

scanf("%d", ptr->data)

and the type-correct version:

scanf("%d", (int *)ptr->data)

are wrong because they try to use an indeterminate pointer. The data
field has not been set to point anywhere. If you want data to point
to an int and then to read into it you must write:

ptr->data = malloc(sizeof(int));
if (ptr->data && scanf("%d", (int *)ptr->data) == 1) ...

Both what you had

scanf("%d", &ptr->data)

and the slightly better

scanf("%d", (int *)&ptr->data)

are wrong because they try to put an into a void * object. It often
works (as you have found out) but it is much better to do this:

struct node {
union {
int i;
void *vp;
} data;
struct node *next;
};

if you want to put ints into the actual nodes themselves. You then

scanf("%d", &ptr->data.i)

in a type-safe manner.

--
Ben.
 
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