473,785 Members | 2,641 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Little Confused

Hi ,
Ihave this code to show binary of an integer.

#include<stdio. h>
#include<stdlib .h>

typedef struct binary* binaryptr;
typedef struct binary
{
int data;
binaryptr next;

}Binary;

void AddNum(binarypt r , int);

int main(int argc, char **argv)
{
int input,num;
binaryptr list;
list = NULL;
if(argc!=2)
{
printf("Argumen ts not enough\n");
exit(EXIT_FAILU RE);

}
input = atoi(argv[1]);

while(input!=0)
{
num= input%2;
AddNum(list,num );
input /= 2;
}
while(list!=NUL L)
{
printf("%d\n",l ist->data);
list = list->next;
}

return 0;
}

void AddNum(binarypt r list, int num)
{
binaryptr temp,NewNode;
printf("IN Add Num\n");
if(list == NULL)
{
NewNode = malloc(sizeof(B inary));
printf("IN List Null\n");
NewNode->data = num;
NewNode->next = NULL;

list = NewNode;

}
else
{
temp = list;
while(temp->next!=NULL)
{
temp = temp->next;
}
NewNode = malloc(sizeof(b inaryptr));
NewNode->data = num;
NewNode->next = NULL;
temp->next = NewNode;

}

}

Why would i will not get the List through this code.
Each time i am entering if(list == NULL){
} Loop.

Is the reason is that i am allocating memory in the fucntion and when i
will return from that function that object may cease to exist.
I am passing a pointer to structure. So if i allocate some memory to it
in some function it should not sho itself as NULL.

Also if i pass pointer to a pointer to structure it works fine.

Why???

Thanx in advance
Vishal

Mar 22 '06 #1
6 1701
I am also adding the Output.
It should enter in In List Null for the second time.

$ ./a.exe 12
IN Add Num
IN List Null
IN Add Num
IN List Null
IN Add Num
IN List Null
IN Add Num
IN List Null

Mar 22 '06 #2
ma***********@g mail.com wrote:
AddNum(list,num );


`list` starts off null, and you never change it.

[You change a completely different variable called `list` in
the `AddNum` function, but that doesn't change this `list`
at all. We [1] say "C does't have (pass|call) by reference".]

You have too much duplication in the code of `AddNum` - it
doesn't need to be that complicated.

Here's a trick to consider: build the list up backwards (this
is dead easy) and then reverse it when you're done (this is also
dead easy).

If all you want to do is see the binary expansion of an int, all
this listing and mallocating is irrelevant. Use an array and
make it big enough [2].

[1] Well, /some/ of us do.

[2] You can work out how big "enough" is using sizeof() and
CHAR_BIT, but for a quick hack a value of 128 should be
fine. Probably 32 will be enough. Probably.

--
Chris "x.f(y) == f(x, y) == (x, y).f" Dollin
The shortcuts are all full of people using them.
Mar 22 '06 #3
"ma***********@ gmail.com" <ma***********@ gmail.com> wrote:
void AddNum(binarypt r list, int num)
{ if(list == NULL)
{ list = NewNode;

} Why would i will not get the List through this code.
Each time i am entering if(list == NULL){
} Loop.


<http://c-faq.com/ptrs/passptrinit.htm l>

HTH; HAND.

Richard
Mar 22 '06 #4
well i give u a simple example..
if u understand the difference between "pass by value" and "pass by
reference "u will be able to solve this urself..
if u want to pass the list and have that pointer contain some value, u
have to pass by reference (for that pointer ) not by Value (for that
pointer )

Mar 22 '06 #5

shiv wrote:
well i give u a simple example..
Who is this "u" that so many people are talking about?
if u understand the difference between "pass by value" and "pass by
reference "u will be able to solve this urself..
In which case, "u" does not need your advice.
if u want to pass the list and have that pointer contain some value, u
have to pass by reference (for that pointer ) not by Value (for that
pointer )


Your post is more than a "Little Confused". I doubt OP will consider it
helpful.

If you plan on contributing here, please read, and apply advice found
here:

<http://cfaj.freeshell. org/google/>
<http://clc-wiki.net/wiki/Introduction_to _comp.lang.c>

--
BR, Vladimir

Mar 22 '06 #6
shiv wrote:
well i give u a simple example..
if u understand the difference between "pass by value" and "pass by
reference "u will be able to solve this urself..
if u want to pass the list and have that pointer contain some value, u
have to pass by reference (for that pointer ) not by Value (for that
pointer )


Please include context, by means of quoting, in your replies. Without
that, readers who do not use Google Groups may no be able to figure out
to whom you're replying.

In this post, I'm guessing you're addressing the OP while posting your
message as a reply to Richard Bos's post. This is awful and
counter-intuitive to others. Please be *sure* to read and follow the
advice given at the following URLs before posting again to this group.

<http://cfaj.freeshell. org/google/>
<http://clc-wiki.net/wiki/Introduction_to _comp.lang.c>
<http://en.wikipedia.or g/wiki/USENET>
<http://en.wikipedia.or g/wiki/Netiquette>

Mar 22 '06 #7

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

Similar topics

23
63698
by: stewart.midwinter | last post by:
No doubt I've overlooked something obvious, but here goes: Let's say I assign a value to a var, e.g.: myPlace = 'right here' myTime = 'right now' Now let's say I want to print out the two vars, along with their names. I could easily do this: print "myPlace = %s, myTime = %s" % (myPlace, myTime)
2
1908
by: Charlie T | last post by:
Hello, I am a little confused... please help: ----------------------------------- <DIV id="01"> <DIV id="aNum"></DIV> <DIV id="bNum"></DIV> <DIV id="cNum"></DIV> </DIV>
1
1498
by: Jeff Uchtman | last post by:
I have read 10000 opinions on returning random records and I am more confused now that before I started. I have a small access db that will contain 20 or so rows with 3 or 4 columns. I like to return a set of 6 distinct rows and display the columns in html. I have seen response.write and loops but can't I throw the 6 random rows in a array and pull the data as I need it in the page? Examples? More confused than ever Jeff
3
9934
by: Laszlo Szijarto | last post by:
In using the BitArray class to parse a single byte, I noticed that the bits show up in Little Endian order. Aren't bits always arranged in Big Endian order? I'm a bit confused and would appreciate any assistance. Thank you, Laszlo
2
2061
by: Daniel | last post by:
I'm new to .Net and all of its abilities so I hope this makes sense. Basically I'm confused on when is the appropriate time to use web forms controls vs. regular HTML. For example in ASP (non-.Net) if I wanted to fill a list it may look something like this: -------START CODE <%
14
1583
by: CMM | last post by:
Do the developers of Visual 2005 actuall use it??? There's lots of great things in VS2005 (mostly related to the outstanding work done on the CLR)... but in general the LITTLE THINGS totally drag it down.. especially the slightly-improved-but-not-all-that-much IDE. I waited for 3 years for this? Who's in charge of this mess? 1) Editing a web form in the designer... I think I'm totally misunderstanding the usage of CSS stylesheets and...
16
1779
by: mdh | last post by:
Can someone look at this little program and tell me why the last line is not compiling. #include <stdio.h> int main (){ int i, j, k; i=0;
1
1274
by: shapper | last post by:
Hello, I need to create a data object to hold a number of records with 3 columns. A datatable would do. My problem is this: 1. I will use this as a GridView datasource. 2. I will need to postback so I can Insert, Delete, Update records
4
2015
by: mattmao | last post by:
I am moving onto the tough part in learning C:( First, about the declaration of a user defined structure: I found this syntax in my lecture notes: struct userinfo { char username; int age;
0
9645
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10325
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10091
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9950
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8972
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5381
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3646
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2879
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.