Little Confused

M

manochavishal

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(binaryptr , int);

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

}
input = atoi(argv[1]);

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

return 0;


}

void AddNum(binaryptr list, int num)
{
binaryptr temp,NewNode;
printf("IN Add Num\n");
if(list == NULL)
{
NewNode = malloc(sizeof(Binary));
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(binaryptr));
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
 
M

manochavishal

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
 
C

Chris Dollin

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

shiv

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 )
 
V

Vladimir S. Oka

shiv said:
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>
 
S

santosh

shiv said:
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.org/wiki/USENET>
<http://en.wikipedia.org/wiki/Netiquette>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Little Confused 0
a simple struct code failing 12
linked list 4
linked list implementation 8
Double linked list 9
C pipe 1
Variable-sized lines of text in linked list 47
Infinite loop problem 1

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,266
Latest member
DavidaAlla

Latest Threads

Top