I believe Labdabeta answered the first question well. As for your second question you can technically do this...

const *p = (const *)0x05;  // Constant pointer address
char *a = (char *)p;       // Assign this address to a character array
printf("%p", a);           // View the address of the character array

This will print out "0x5".

It is not advisable to work with pointers this way unless you are certain of the memory mapping. While programming in C you essentially give up control of the memory mapping and let C and the Operating System deal with that for you.

Of course if you are certain of the memory mapping (such as with embeded devices or OS programming) by all means create constants for these "POKE"-style addresses. This is usually done as a series of #define lines in headers.

Rahul47 commented: +1 for answering the second part. +4

I just tested the code and it agrees with Ancient Dragon's explanation. Sorry about that.

Integer has nothing to do with pointer size. The pointer is the size of the memory address as dictated by the Operating System based on the CPU's capabilities. A 32 bit CPU will have a 4 byte address whereas a 64 bit CPU will have an 8 byte address. Some Operating Systems will have an emulation layer that would change this address size.

Be careful assuming the size of the pointer as you will force your software to be locked into specific system specifications. Use this line of code when you need to figure that sort of thing out:

int PointerSizeOnThisSystem = sizeof(char *);

Its funny to think that c=(--a)+(--a); is not any more efficient than --a;--a;c=a+a; in terms of how it compiles. Its just that the first version is ambiguous in design as (--a) acts as a function with a side effect rather than as a variable in an algebraic formula.

Ever since I learned about the ++ operator I felt it was like cheating and could confuse people on this very thing. It would make C much more logical if you avoided ++ altogether in place of inline function calls achieving the same thing.

I would classify your question a platform specific issue since again the definition of SOCKET is not your responsability. As the programmer of the PIC32 and user of your compiler you need to have certain expectations of compliance with the standard libraries as perscribed by the C standard. If there are bugs with its functionality thats the platform's fault, not yours.

Unless of course you are not including the correct libraries as defined in your platform documentation. You could always try this and see what happens:

#include <socket.h>

This is illegal unless you are working on an embedded system or a very low tech system that only works with 1 byte addresses:

typedef BYTE SOCKET;

And unless you are reinventing the wheel you should not be actually defining a typedef for SOCKET. Its defined in your socket library headers. In Windows its winsock2.h and linux its sys/socket.h. Find out which is right for your system. You are not responsible for defining this structure unless you are correcting a conflicting definition which a situation you want to avoid as much as you can for scalability sake.

Long story short just include the correct header. The compiler error you mentioned implies that you are not.

If you have any specific questions from the book, you should ask them here. We would be happy to help you. It is not the nature of DaniWeb to give out answers, especially instructor's manuals.

[QUOTE=thines01;1701290]Maybe these are just code snippets. :|[/QUOTE]

There is a right way to post code snippets. This is the wrong way, primarily because he doesn't describe the application of the code he is posting.

It would be nice if a moderator can provide a link on the rules for publishing code snippets so I can point out the specific violations. (I could not locate them myself)

There is no excuse not to say anything in a help forum. We are not robots: we don't communicate in code. If you are not comfortable with the English language, its okay. We will not criticise your speech. But you must explain yourself, otherwise you are just spamming the site.

C is not FORTRAN: It is not "made for mathematics". C is a system programming language, and as such, you must understand that it can't magically figure out what relationship your functions [B]f()[/B] are to each other by context. To make this work in C, as [B]WaltP[/B] explained, you must construct a series of steps that can be worked out by a human, then map that logic to a series of steps in the C language that emulates that procedure.

Also worth noting, your code produces no output and does no work because the functions that you wrote is never used by the [B]main[/B] function.

WaltP commented: You also can't make it work in ForTran as you are implying. forTran has the same limitations a C in this context. +17

[QUOTE=greatparthi;1657644]HI, N1GHTS, I am not getting[/QUOTE]

... and that's okay too. Welcome to Daniweb people. When posting a new thread on this forum, it helps to say something. With nothing to say, a thread can easily become derailed, such as this one.

[QUOTE=Lordvivi;1656604]What is the meaning of this?[/QUOTE]

It's obvious that [B]neeraj goswami[/B] did not want you to know what this is about, otherwise he would have said something by now. So use this rare opportunity to use your imagination!

I have attached a visual aid of the concept of imagination which may help your interpretation of this thread.

Here you go!

include <stdio.h>

int main(int argc, char** argv) {
printf("Hello World\n");
return 0;

Ok, thats a different question. When you compile your code in C, it gets converted to machine code (assembly). That code is not a direct representation of C.

When you type this:
int a;
That is not compiled. That's because its only useful for the compiler. The computer itself does not care if "a" is an integer, only the compiler cares.

This code does get compiled:
int a = 10;
It is putting the value of 10 to some memory space, probably a register.

Code in C generally executes one line at a time, and if you use a "switch", the program will jump to wherever you are "switching" to.

switch (2) { <--- Starts Here
int a = 10; <-- ignored
case 1: <-- ignored
break; <-- ignored
case 2: <---- Jumps to here
break; <---- Exits the switch
case 3: <-- ignored
break; <-- ignored
} <---- Jumps to here
It will only pay attention to "case 2"

That was not a tutorial on how to run the code. I was trying to explain why the code is giving garbage value.

A "switch" is a more elegant "goto." A "goto" is a command that jumps over code. Your switch is jumping over x=10. It never gets to be initialised, thus, it has no useful information in it, thus it is garbage.

switch (2) {
int x = 10;
case 2: printf("Case 2: %d\n",x);
Is the same as...
goto 2;
int x = 10;
printf("Case 2: %d\n",x);
Which is the same as...
printf("Case 2: %d\n",x);
And since x is not initialised with any value, it will print garbage.

I have had situations where I needed to graph a complex mathematical model for some application, such as compression, encryption, image, and number theory analysis. I honestly didn't think there were programs out there that do this kind of thing. What I would do instead was just make a GUI application in C to plot the specific nature of my analysis which takes some time to develop but in the end its quite flexible and powerful. Either way this software you mentioned intrigues me.

[QUOTE=WaltP;1595490]Actually [I]you're[/I] (note the spelling) better off using SPACEs. They are always consistent. TABs are not[/QUOTE]

If IDE A has a tab spacing length of 2 and IDE B has a tab spacing of 4, as long as you are consistent with the tabs, and as long as its only used to indent the code, that alone should be fine. Mixing tabs and spaces is the primary issue.

I have also seen IDE's where you press enter and it auto-inserts tabs for you rather than spaces, so being consistent requires looking at the IDE settings before you begin your programming. And as far as copying code from other sources, that needs to be checked for proper indentation as well.

[quote]Your indentation is horrid[/quote]

His indentation looks horrid, but notice that some of his lines are tabed and some are spaced. I have this problem too when copying code from an IDE where tabs are set to allocate an unconventional specific number of fixed width spaces. It makes copying terrible. On that kind of IDE, your better off using spaces exclusively or tabs exclusively.

[QUOTE=nezachem;1586986]To begin with, both functions make library calls, which are totally uncalled for. A standard technique here is
[CODE] while(in_BinaryString)
Result = (Result << 1) | (
in_BinaryString++ == '1')[/CODE]

The IntToBinary (which BTW fails for a negative argument) instead of calling memmove should just [ICODE]return Result + t;[/ICODE]. Since Result is static, its last byte is initialized to 0 and shall never be touched:

[CODE]char IntToBinary(unsigned int in_Integer)
static char Result[MaxLen];
static char digits = { '0', '1' };
for(dst = Result + MaxLen - 1; in_Integer > 0; in_Integer >>= 1)
*--dst = digits[in_Integer & 1];
return dst;

I do prefer a digits array to a ternary for a reason of branch elimination. Assuming an ASCII code set, one may even do
[CODE] *--dst = '0' + (in_Integer & 1);[/CODE][/QUOTE]

Aha! Delicious suggestions! I especially like sending the end segment of the string rather than move it to the beginning.

[QUOTE=Narue]Sounds like a straw man to me. Just because they failed miserably at simplifying the logic doesn't mean simplifying the logic is a bad idea. I too have seen such attempts fail, and it's nearly always due to code cowboys who spent more time hacking than designing.

It's quite a bit harder to design clear code with acceptable performance than unclear code with excellent performance. I'd honestly prefer to see beginners fail at the former than not even try.[/QUOTE]

This is essentially a debate on learning styles rather than design styles. You are right on both those points you made, and its basically a teaching preference. I started learning programming by learning logic circuitry and digital electronics engineering, then 80286 assembly language, and worked my way into high level languages like BASIC and C. This perspective helped me design simplified "to the point" code before it spiral's into undue complexity. Learning object oriented programming concepts after all this taught me the value of readability in code.

I don't expect everyone learning programming to be an engineer first, but students that start their programming experience from the top down are not given enough emphasis on design efficiency, something that is required at lower levels of electronics, firmware, and system programming.

I have seen some horrible firmware code recently, obviously made by someone who only writes at high levels, and the code was trying very hard to avoid using recursive functions and ended up taking up nearly the entire ROM space given ...

[QUOTE=tonyjv]There is however one optimization technique which beats yours any time, it is called lookup table (consider input range 0..255, it's in requirements).[/QUOTE]

I did not make the functions exclusively for the original poster. It was made to be useful for many applications.

As for the lookup table method, if you are implying your method of multiplying indexes with powers of 2, the multiplication performed in that process alone takes many more clock cycles than left shifting which my method employs.

Yours and our dear Python Master Vegaseats answers are giving little much answer ready to OP. We could hope he has given much effort from his side for the thing (which for you and others is under one hour thing). I for one can be thankfull for OP, for lesson in testing, that random inputs is not enough test for function, but you must find and include the corner cases also. I have also included much sceptisism on quality of input for value to base conversion in my any number any base (as long as you give enough symbols).

In the [B]BinaryToInt[/B] function, it only reads '1' characters. If the user typed "hello world" and passed that to the function, the return value would be 0. Bad data in, bad data out. It was designed to accept bad data without crashing.

I have strong bias towards optimization which I call "fast enough": If user input is got and response is given then the simple, understandable code ...

char IntToBinary(int in_Integer, char out_Result, size_t in_MaxLen);

Passing MaxLen instead of as a global constant... very very nice! I didn't think about that.

@NIGHTS: Good for you to take with challenge the simple exercise for beginner the way which is interesting to you even already more advanced in your skills.

However there is hidden content in exercises for beginner and therefore we other posters tried to adapt to the situation of the OP and give some advice that would benefit him in his life as programmer and guide him to right priorities in his life.

That's why I explained the algorithm in detail.

Unfortunately in my view, maybe unduly influenced by my main language Python, optimizing for speed in the OP's case is what in Python circles is frowned upon as premature optimization. That does not however mean that your solution is wrong for [B]you[/B].

For those that don't know what [B]tonyjv[/B] is talking about, read these links:


As for your concern about premature optimization, while I agree that in the ladder of programming logic the highest level logic should always be programmed for readability over speed, however, this procedure is purely mathematical in nature, and as such, should be programmed consistent with the efficiency and philosophies that guide such fundamental mathematical logic.

There is few inconsistencies in your variable naming which is maybe more important to OP than optimization of the code: You have three kinds of names:

[B]Result[Maxlen][/B] Capitalized

I prefer to title case my variables, such that all words are connected together [b]InThisManner[/b].

[B]t[/B] cryptic type it fast letter

The ...

The conversion methods used in this thread are inefficient.

Just now I created and tested two functions for you to use that perform [B][I]Binary String to Integer[/I][/B] conversion and [B][I]Integer to Binary String[/I][/B] conversion. They are the most efficient I could possibly make them. Any more efficient and i'd have to write it in assembly.

int BinaryToInt(char *in_BinaryString) {
int t, Result = 0, len = strlen(in_BinaryString);
for (t=0;t<len;t++)
Result |= ((in_BinaryString[t]=='1') << (len-t-1));
return Result;

define MaxLen 33 // Total number of bits supported (32 bit + null char)

char *IntToBinary(int in_Integer) {
static char Result[MaxLen];
int t;
for (t=MaxLen-1;in_Integer>0;in_Integer>>=1,t--)
Result[t] = (in_Integer&1)?'1':'0';
Result[MaxLen-t] = '\0';
return Result;

Unlike the original request for 8 bit conversion, these puppies can convert up to 64 bits if your computer can handle it. With a little tweaking they can convert enormous arrays of numbers to enormous arrays of numbers. The magic is in the algorithm.

In the [B][I]Binary String to Integer[/I][/B] converter, I loop through the given characters from start to end with the goal of trying to detect any '1' characters. For every one of them that is found, the [B]Result[/B] which was set initially to zero will get overlapped by a single bit that corresponds to the position in which they were found B[/B]. So basically its just a '1' detector that overlaps real 1's where it finds a fake '1'. The result is the real binary number as an integer.

In the [B][I]Integer to ...

Lets see what your code looks like so far. I'll try to put it through my debugger.

Why can't you cast? Is it because you are getting errors or is it because this is a school assignment and the instructor is defining limitations to the allowed techniques?

Also, what line of code is illegal under your specific constraints? I'm curious to know.

[B]> What does that signify? [/B]

It signifies everything I said in my first response.

[B]((void )0)[/B] is a special "command" that the C compiler looks for. Its completely different than [B]((void )2)[/B] or anything else because the C compiler specifically will look for the [B]((void *)0)[/B] combination and does something special with it.

What it does is it looks at the context in which it's used, and then try's to create a pointer which points to something completely invalid for that context, which is usually memory position 0, but it could be something else, totally depends.

This is called a "null pointer". In the C standard library you might encounter a macro used quite often named NULL. If you look at its definition, its defined as [B]((void )0)[/B]. The null pointer will always be unique from any possible other pointer, even if there is a valid pointer to 0, in which case the compiler turns [B]((void )0)[/B] into something crazy like 0xFFFFFFFF. Depends completely on what your context is.

If you want more information on null pointers, check this out:


In the example you gave, he was exploiting the null pointer feature.

[B]void[/B] is a special type which is like a place holder for any type. [B]void[/B] pointers are used as a kind of "incomplete cast" which allows you to store values without first specifying its type, but you must later specify its type by casting before using a void pointer. [B]void[/B] pointers almost always point to something, and since they are just pointers, a pointer is usually the size of int. In some advanced cases, a void pointer is used as a temporary integer.

[B][COLOR="Red"](void *)[/COLOR][/B] <-- that is a void pointer

[B][B][COLOR="Red"](void *)2[/COLOR][/B][/B] <-- that is a void pointer to address 0x00000002

[B]COLOR="Red"2[/COLOR][/B] <-- Error, cannot cast to a typeless type

[B][COLOR="Red"]int A;
(void *)&A[/COLOR][/B] <-- This is how you convert the int pointer into a pointer of any other type

[B][COLOR="Red"](void *)A;[/COLOR][/B] <-- This will point to the memory pointed to by the [I]VALUE[/I] of int A.

So yeah, that's how you would use it. It might help if you give specific examples of [B](void *)2[/B].