I didn't make any assumptions. I was always avoiding using flags in the way I described but decided today to ask what you guys thought about it purely out of curiousity.

In my applications I tend to use mutex locking as a multi-threaded resource protection mechanism.

Thats what I thought too which is why I've been naturally avoiding using them in that way, but it would have been so convinient and elegant if it did. Thus is life in the world of the compiled.

Say I have code like this...

typedef struct {
    unsigned A:1;
    unsigned B:1;
} FlagStruct;
extern FlagStruct *Flags;

Imagine that the Flags instance is a shared resource in a multi-threaded application. Say thread #1 decides to read the value of Flags->A but thread #2 decides to set or clear Flag->B at the exact same time. How isolated is Flag->A from Flag->B such that any changes to Flag->B does not affect the value of Flag->A? In other words, are set or clear operations in this structure atomic and therefore thread safe?

Just a quick word of advice. It is more efficient to use fwrite() or fputc() instead of fprintf() for writing characters to a file one at a time. This is because fprintf() needs to parse the format string whereas fwrite() and fputc() does not.

This only matters if you plan on dealing with very large files.

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.

Correction: The fix appears to be that I need to use the extern keyword instead of static, then declare it in a CPP file. The problem seems to be with the structure being created in different contexts or address spaces for each loaded CPP file or something. Might be a Borland C++ Builder specific issue.

I fixed the problem by changing line 13 of my main posted code to this:

static _Stock Stock;

Do keep in mind that my actual code is composed of literally thousands of lines of code spanning many cpp/h files. Apparently Borland C++ Builder 5 does not like working with structures defined in headers unless its anchored down with the static keyword.

I guess thats good information.

In this case its being called by a VCL button OnClick event handler. The code itself is in a cpp file compiled with the rest of the project. As an event handler, I doubt it is a constructor.

How would that matter?

Thank you for your quick reply deceptikon. The problem with your theory is that the TempPage variable worked fine using malloc(). The problem appears to be in copying the pointer to the actual Stock.Page pointer. Also, I use malloc() in other places of the code (such as creating space for strings and arrays) and that works fine.

Other parts of the code work fine in the C++ compiler. So far this seems to be the main incompatibility.

By the way, you are right about the casting issue. I mistyped the code in my post. It was actually more like this in my real world example:

AnObject *CreateInstance() {
    return (AnObject *)malloc(sizeof(AnObject));

I am importing some C code into a C++ compiler, specifically Borland C++ Builder 5. I seem to have a problem with this style of code:

// A structure that contain's itself
typedef struct AnObject AnObject;

struct AnObject {
    AnObject *Object;

// A global structure to store a pointer of the AnObject structure
typedef struct {
    AnObject  *Page;
} _Stock;

_Stock Stock;

// A function that returns an instance of AnObject
AnObject *CreateInstance() {
    return malloc(sizeof(AnObject));

// A function that copies the generated instance to the global pointer
void somefunct() {
    // The pointer will start off NULL
    Stock.Page = NULL;

    // The pointer should now receive a non-NULL pointer
    Stock.Page = CreateInstance();

In GCC, the Stock.Page pointer will be non-NULL at the end of the somerfunct() function. This is not the case for Borland C++ Builder 5. In that case Stock.Page continues to be NULL.

On the other hand, if I change somefunct() to this...

void somefunct() {
    // The pointer will start off NULL
    Stock.Page = NULL;

    // Borland C++ Builder 5 likes this syntax. TempPage is a non-NULL.
    AnObject *TempPage = CreateInstance();

    // But I still cannot copy the pointer from TempPage.
    Stock.Page = TempPage;

    // Stock.Page is still NULL at this point!

Since I am better versed in the C language, I am thinking that this is some kind of incompatibility with the language itself, and that I need to do this more C++ style. On the other hand, the compiler I ...

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=robinpurbia;1701370]how can i delete this post........ any idea??[/QUOTE]

Well, since you know English, why are you posting this code?

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

[QUOTE]I think you completely missed my point. I threw it together to brain storm how I'm going to approach the logic of it. Instead of helping, you criticized my approach. All I asked was for help brain storming how I could approach this program. I don't care if C wasn't designed for mathematics, it was an assignment and I had to do it whether or not C was designed for mathematics. Neither of you bothered to post something similar, that could've helped. [/QUOTE]

All criticism is healthy criticism in my opinion. You are right about your post being a logic issue and not a programming issue -- this is exactly why I made a point to distinguish the two things. Your question implied that the programming language could do most of this logic on its own. The Trapezoidal formula you provided, in order to be compatible with C, must be broken down into more steps.

[QUOTE]Could've pointed me to some tutorials instead of being pricks about it.[/QUOTE]

I'm sorry you feel that way. The experts on this forum are generally better with C-specific questions than mathematical formulas. Had you provided the broken down logic, I would have been more than happy to help you turn that logic into C code complete with best practice tips.

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.