473,385 Members | 1,379 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

void pointers and scanf/printf

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);
}
Jul 20 '08 #1
15 6980
On Jul 20, 5:53 pm, pereges <Brol...@gmail.comwrote:
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
Jul 20 '08 #2
On Jul 21, 2:23 am, voidpointer <diegorocha1...@gmail.comwrote:
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.
Jul 20 '08 #3
On Jul 20, 6:28 pm, pereges <Brol...@gmail.comwrote:
On Jul 21, 2:23 am, voidpointer <diegorocha1...@gmail.comwrote:
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
Jul 20 '08 #4
On Jul 20, 6:57 pm, voidpointer <diegorocha1...@gmail.comwrote:
On Jul 20, 6:28 pm, pereges <Brol...@gmail.comwrote:
On Jul 21, 2:23 am, voidpointer <diegorocha1...@gmail.comwrote:
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
Jul 20 '08 #5
On Jul 21, 3:12 am, voidpointer <diegorocha1...@gmail.comwrote:
only a correction, use scanf("%d", (int*)&ptr->data), sorry
but ptr->data is already address then why &ptr->data ?
Jul 20 '08 #6
voidpointer <di************@gmail.comwrites:
On Jul 20, 6:28 pm, pereges <Brol...@gmail.comwrote:
>On Jul 21, 2:23 am, voidpointer <diegorocha1...@gmail.comwrote:
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.
Jul 20 '08 #7
pereges <Br*****@gmail.comwrites:
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.
Jul 20 '08 #8
On Jul 20, 7:15 pm, pereges <Brol...@gmail.comwrote:
On Jul 21, 3:12 am, voidpointer <diegorocha1...@gmail.comwrote:
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.
Jul 20 '08 #9
pereges <Br*****@gmail.comwrites:
On Jul 21, 3:12 am, voidpointer <diegorocha1...@gmail.comwrote:
>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.
Jul 20 '08 #10
On Sun, 20 Jul 2008 15:51:13 -0700, neobakuer wrote:
On Jul 20, 7:15 pm, pereges <Brol...@gmail.comwrote:
>On Jul 21, 3:12 am, voidpointer <diegorocha1...@gmail.comwrote:
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.
(int*)&ptr->data avoids the warning message and is incorrect in this case.
(int*)ptr->data avoids the warning message and is correct. This is not
directed at you specifically, but please try to understand warnings before
modifying code to work around them.
Jul 20 '08 #11
On Sun, 20 Jul 2008 23:58:16 +0100, Ben Bacarisse wrote:
pereges <Br*****@gmail.comwrites:
>On Jul 21, 3:12 am, voidpointer <diegorocha1...@gmail.comwrote:
>>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.
To quote the original message:

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->data has been properly initialised, so

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

is correct.
Jul 20 '08 #12
Harald van Dijk <tr*****@gmail.comwrites:
On Sun, 20 Jul 2008 23:58:16 +0100, Ben Bacarisse wrote:
>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.

To quote the original message:

ptr->data = malloc(sizeof(int));
<snip>
ptr->data has been properly initialised, so

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

is correct.
Blimey! Must go to sleep. Totally missed that!

--
Ben.
Jul 20 '08 #13
I had delete the previous code and posted a new code. I don't know how
others can still see the previous code but it happens always with
google and its quite irritating.

Anyway, thanks for the help Harald and Ben.
Jul 20 '08 #14
pereges said:
I had delete the previous code and posted a new code. I don't know how
others can still see the previous code but it happens always with
google and its quite irritating.
Google has nothing to do with it. Usenet is not Google, any more than the
World Wide Web is Internet Explorer, email is Outlook Express, or
spreadsheets are Lotus 1-2-3.

When you post a Usenet article, it is sent to a great many servers all over
the world. It used to be pretty easy to cancel messages until that service
got abused. Now, few servers honour cancellation requests - it's a shame,
but it's easy to understand why. Even if Google offers a cancellation
option, the rest of the world is under no obligation to follow suit.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Jul 20 '08 #15
Richard Heathfield <rj*@see.sig.invalidwrites:
pereges said:
>I had delete the previous code and posted a new code. I don't know how
others can still see the previous code but it happens always with
google and its quite irritating.

Google has nothing to do with it. Usenet is not Google, any more than the
World Wide Web is Internet Explorer, email is Outlook Express, or
spreadsheets are Lotus 1-2-3.

When you post a Usenet article, it is sent to a great many servers all over
the world. It used to be pretty easy to cancel messages until that service
got abused. Now, few servers honour cancellation requests - it's a shame,
but it's easy to understand why. Even if Google offers a cancellation
option, the rest of the world is under no obligation to follow suit.
And the lesson is, if you want to correct something you've posted,
post the correction *as a followup* and clearly indicate in the text
of the followup that it's a correction. It won't hurt to try to
cancel the original article, but it's not likely to do much good.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jul 21 '08 #16

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Vivek | last post by:
Hi, I am trying to get as much information on void pointers. How do we take help of void pointers in writing generic functions. I have seen this kind of code for many years, but now I have got...
25
by: Podrzut_z_Laweczki | last post by:
Hello, I have question (or 2 :)). Is that true that for a large data using scanf/printf instead of cin/cout makes that the program runs faster? And if it is, is it legal to mix scanf/printf with...
3
by: sam | last post by:
Hello whats the use of void pointers? and when they are useful (on which conditions?) Please give me example code with some explanation. Thanks in advance.
5
by: Rahul | last post by:
Hi Everyone, There was a discussion on the need of void * in C and C++. In C, it is a generic pointer which can be typecasted to and from that of other types. And it is developer's head ache...
18
by: bcpkh | last post by:
Hello All Hope someone can help me, please note that at first this might look as if it is posted to the wrong group but if you ignore the specifics I think it is general pointer referencing...
160
by: raphfrk | last post by:
Is this valid? int a; void *b; b = (void *)a; // b points to a b += 5*sizeof(*a); // b points to a a = 100;
3
by: sritejv | last post by:
Hello everyone, I am having a problem with typecasting void pointers.I have read the pointer basics but still cant understand why the following test code doesnt work. void *xyz; struct abcd...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.