deceptikon 1,790 Code Sniper Team Colleague Featured Poster

... at the end of a function's parameter list says that the function takes a variable number of arguments. I'll leave it at that so you can try to figure out why it works, then I can tell you how it works. :)

One thing to note though is that the variable arguments are not type checked. Hence why printf (which is a variable argument function) uses the format string to determine the type of the argument.

As for when to use it...rarely, in my experience. The negatives tend to outweight the positives, but if you have a situation like printf or scanf, then variable parameters can be a useful solution.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Can you elaborate that part a bit ?

It's a common misunderstanding that arrays and pointers are equivalent, which causes confusion when you reach a point where object context rules (such as with the sizeof operator). The reason for the misunderstanding is that in value context, the name of an array is converted to a pointer to the first element:

int a[5];

// Prints the same address for both
printf("%p\n", (void*)a);
printf("%p\n", (void*)&a[0]);

int *p = a; // Compatible types, so no cast needed

Most of the time this conversion applies. However, this is not the case in object context, otherwise sizeof wouldn't work correctly:

printf("%zd\n", sizeof(a)); // Works like a champ

With a pointer you'd get the size of the pointer, and with an array you'd get the collective size of the elements in the array as a whole.

There are three object contexts for an array:

  • As an operand to the sizeof operator.
  • As an operand to the address-of (&) operator.
  • As a string initializer.
deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Only OP Kudos members can ask every type of questions without showing their efforts.

On the contrary, our rules apply across the board. Everyone is required to adhere to them or risk punishment; kudo posters, featured members, moderators, admins, even Dani herself.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

On a side note, the technique you're showing is called type punning. It's rather common in lower level code. If you're feeling frisky, an example of union-based punning to break down a floating-point value can be found in one of my teaching libraries here and here.

In the same way , if u say int *a = 5 , then a is the same as arr or &arr[0]. its just a pointer that points to a certain memory.

Noting, of course, that an array is not a pointer. Though an array name in most contexts is converted to a pointer.

In fact , you can just as well write printf("%d",c[1]) instead of printf("%d\n",*(c+i)) , Both mean the same thing to the program

Further solidifying the equivalence, you can also write printf("%d", 1[c]) because addition is commutative. Don't do that in real code though, it's an interesting yet useless trick.

Lastly , You do get a warning : initialization from incompatible pointer type when compiling

Which is cleared up with a cast. It's important to understand the warnings and avoid them where necessary. But if the warning is known to be spurious, it's best to make it go away so that the compilation log is clean. That way real warnings and errors stand out more.

somjit{} commented: thanks for the type punning side note! now i know what to search for :) +5
deceptikon 1,790 Code Sniper Team Colleague Featured Poster

oh yeah.... What is OP?

Original poster or original post, depending on the context.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

In the first one, why would we start writing to the file at position n * recsize instead of position 0?

Judging from the code, the intention is to overwrite an existing record that may or may not be the first record. If you always used position 0, you'd always overwrite the first record.

A more interesting question is what happens if you enter a record number that exceeds what's already stored in the file? :)

What is reinterpret_cast?

It's a specialized type cast. You could use an old style cast to the same effect:

fbin.write((char*)&age, sizeof age);

What reinterpret_cast says is that the purpose of the cast is to change how the bytes of an object are represented. There are a number of reasons to use a cast, and C (or older C++) only had one almighty cast syntax to do all of them. The casting operators break it down into usage with a searchable syntax so that it's easier to grep for. In human words (risking inaccuracy):

  • static_cast: Normal conversions that aren't implicit, like double to int.
  • const_cast: Adjusting const or volatile qualifiers.
  • reinterpret_cast: Change the byte representation.
  • dynamic_cast: Polymorphic casts with type checking.

Is it function, it doesn't have the syntax of one or is it just a built in keyword?

It's a keyword and an operator.

Why are we casting as a char pointer or are we casting as a char pointer and what's the (&age) …

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

So I decide that this is my last posting in DANIWEB and never try to help anyone.

Good luck to you in your pursuits. Hopefully one day you'll realize that the people you "help" won't always have you sitting on the sidelines to do their work when things get difficult.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

I also think it maybe best if DaniQueen introduce a way to decrease points to those who are doing this

I'm not entirely sure what you mean by "points", but voting and reputation work both ways. If you find someone spoonfeeding beginners, then by all means given them a downvote or negative rep. What you're asking for already exists. :)

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

The compiler is correct, varying doesn't exist at that point. You declare it in textBox1_TextChanged, but try to use it in a completely different method. Why not just use textBox1.Text directly and not worry about handling TextChanged?

MessageBox.Show("Hello!", textBox1.Text, MessageBoxButtons.OK);
deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Would you be kind enough to explain what exactly the problem is? It's too early for guessing games here. ;)

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Richard, are you using Google Translate, or something?

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

This is actually a common exercise, with well known solutions. But the neatest trick is to make two passes over the string:

  1. First Pass: Reverse all characters in the string (ie. "treboR si eman yM")
  2. Second Pass: Reverse each word in the string

Nothing fancy, just super basic string handling, and you have an elegant solution. :)

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Or do you just not ask that question and automatically stay away from any undefined operations?

No and yes, respectively. Undefined behavior is super bad, but questioning why something is undefined improves understanding of the concept and helps one to avoid it in cases that aren't textbook examples.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Note that Daniweb prohibits discussion of hacking. I'll allow this thread because the question is very specific and more about C in general, but take care in asking for details about how the exploit works.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Since this is a final project, I tend to agree with your teachers. You should have all of the building blocks available at this point to complete the project, they just need to be put together. Perhaps if you asked a more specific question rather than just posting your assignment, someone can shed light on how to get you started.

User should be allowed to enter a maximum of 15 record.

Translation: Use an array of records.

25 minimum of struct data

25 what? This is too vague, and can only be answered by your teachers.

Functions

Okay.

Branching/Conditional
loops

You'd have a somewhat hard time not using either of these in any non-trivial program. ;)

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Perhaps it's just me but it seems like a lot of trouble to go through to avoid using the builtin My.Settings.

After a fashion. Certainly naive use of My.Settings is simpler than managing a configuration class, serialization, and encryption in a more manual manner. That's precisely why for simple cases I prefer the former.

However, when you run into limitations of My.Settings, working around them without switching to a different solution entirely becomes non-trivial very quickly.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

What's your preferred method for saving your program's settings?

It depends on the settings and the complexity of the program in my case. For simpler things, I prefer app.config. For more complex things I prefer my own XML file using serialization. Sometimes I'll go with storing settings in a database. The registry is not recommended.

Is there a preffered industry standard?

Not a single one. :)

Does .Net provide a namespace for reading writing a programs settings?

Yes. The API that facilitates app.config is exposed for you to use. However, unless you're using it in conjunction with app.config, things get hairy. I vastly prefer managing custom XML or INI separately.

As VS creates settings in XML in the app.config file should I sipmly take this as preferred.

That should be your default preference when all else is equal.

J.C. SolvoTerra commented: This makes me happy as I don't have to change my personal preference hehehe +3
deceptikon 1,790 Code Sniper Team Colleague Featured Poster

I think that long double is not supported in many (most?) C compilers ...

long double is required to be supported. However, on some platforms it has the same range as double, so the two types are synonymous in functionality in those cases.

I'm more inclined to say that the OP's problem is an artifact of Turbo C being a dinosaur. And since I'm not about to install Turbo C on my machine, and modern compilers don't exhibit the problem, troubleshooting and fixing the code is difficult.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

It's impossible to answer your question as asked because the benefits of cores versus clock speed are situational. What's the average use case of the intended machine?

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

What compiler are you using?

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

You can't do that. There is no way other people can use your "project" without also having purchased and installed MS Access.

I'll be sure to let my clients know that the software they're using is impossible. ;)

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

let me reword that a little better, they d ont hate me but they don't exactly care for me

They have to warm up to you, which can take longer for some than others. As an example, Narue (who may be one of the more respected members on Daniweb) was almost banned and shunned when she first started posting due to her abrasive style.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

You joke, Richard, but it's not quite so clean cut. ;) For example:

Don't be reluctant to try different things with colors!

I got laughed at by coworkers for using pastels instead of primaries (the usual red, green, blue), but the users agreed pastels were easier on the eyes and no less informative.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Sorry to hear that. Perhaps if you offered something significantly less vague, someone might be able to help troubleshoot.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

I need to be able to take a string and put it into an array of chars, how can i do this?

If the string is a literal, it's as easy as this:

char test[] = "08/11/2014";

If the string is obtained some other way, and all you have is a pointer to it, you need to create an "array" that can hold it, and copy the characters. In this case, I'd suggest dynamic memory:

// Don't forget to include space for the null character
char *test = malloc(strlen(dat) + 1);

// Always check for failure
if (test)
{
    strcpy(test, dat);

    ...

    free(test); // All done, clean up the mess
}

The problem with some random string you know little about is you don't know the size. So it's tricky to use an array proper due to requiring a compile-time constant[1].

[1] Yes, C technically allows variable length arrays starting with C99. However, they're not recommended for various good reasons. Let me know if you want more detail on why VLAs should be avoided in favor of more classic techniques.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

what tips have you learned over time?

The technical stuff is easy. My biggest wins in UI design have come from end user feedback. Things I find to be obvious are often obtuse to an end user, and knowing what confuses them has helped me to build intuitive interfaces.

On the technical side, one thing I'm careful about with WinForms is control updates. Especially with a NumericUpDown control, I've found that keyboard entry tends not to get applied after focus is lost unless you're careful about binding.

Let's see...how about finishing off with an extension method I use for WinForms that's a convenience when setting/resetting a control's binding?

/// <summary>
/// Convenience method for resetting data bindings.
/// </summary>
/// <remarks>Clears and re-adds the binding.</remarks>
public static void ResetBinding(this Control ctrl, string propertyName, object dataSource, string dataMember)
{
    ctrl.DataBindings.Clear();

    if (dataSource != null)
    {
        ctrl.DataBindings.Add(propertyName, dataSource, dataMember, true, DataSourceUpdateMode.OnPropertyChanged);
    }
}

Simple, yes, but makes for cleaner code in the majority of bindings:

/// <summary>
/// Binds configuration elements to the editor.
/// </summary>
private void BindUI()
{
    textBoxName.ResetBinding("Text", Value, "Name");
    textBoxServer.ResetBinding("Text", Value, "Server");
    textBoxDatabase.ResetBinding("Text", Value, "DatabaseName");
    textBoxLogin.ResetBinding("Text", Value.Credentials, "Login");
    textBoxPassword.ResetBinding("Text", Value.Credentials, "Password");
}
deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Haaaa 'weenie points'

Everyone seems to like my peen points terminology. :D

I'm 33 guys, im not fussed about reputation.

Same age as me. I'd be lying if I said reputation doesn't affect me, but typically it only matters when coming from someone I respect. If Mike ever downvoted me or gave me negative rep, for example, it would sting more than some drive-by no name McWhothehellareyou.

"But it works because I've just tested it. Why is it wrong?"

Heh, I get that at work all the time. It's tough explaining how something is wrong even though it happens to work at the time.

is the down voter tallented enough to know its wrong and down vote it whilst at the same time not being bothered to provide a solution or help.

Unlikely. We all have huge egos, and a big part of being right is making sure everyone knows we're right. Anonymous voting doesn't contribute toward that goal. ;)

Of course, then there's the risk of making an incorrect correction. But the ego blinders have a large effect. I can guarantee from experience that knocking someone down a few notches after they correct you with their own mistake is pure pleasure. If you haven't had the chance to do that yet, I'd strongly recommend sticking around until you do.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

In terms of e-peen points, you're doing okay. What really matters is the intangible reputation you earn that is what people who matter think of you. And that kind of reputation takes time.

Were I concerned about reputation, I'd wonder what people thought about a public annoucement of leaving after less than a month of not getting enough weenie points. ;)

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Imagine an array:

{1, 2, 3, 4, 5, 6}

Now imagine dividing it up into pairs:

{1, 2}
{3, 4}
{5, 6}

Then swap each pair:

{2, 1}
{4, 3}
{6, 5}

Put back together you have the final result:

{2, 1, 4, 3, 6, 5}

Which in pseudocode would be something like this:

a = {1, 2, 3, 4, 5, 6}
n = 6
i = 0

While (i < n)
    # Swappy swap
    temp = a[i]
    a[i] = a[i + 1]
    a[i + 1] = temp;

    # Jump to the next pair
    i = i + 2
Loop
munchlaxxx commented: Thanks for clarifying :) +1
deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Need it in 15 mins

Yeah...15 minutes would be really pushing it even for an expert who knows exactly what to write and doesn't require any debugging.

I'm happy to offer advice to help you learn when you have more time, but for this particular project, you're SOL.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Internet recources on algorithms are suprisingly sparse, especially compared to "regular" programming.

Ironically, my go-to quick reference for algorithms and data structures is a website. It's the most complete reference in a single location to the best of my knowledge, and tends to be recently updated. Not perfect, but a book certainly won't be either. You'll learn to use multiple references in such cases.

rubberman commented: Thanks for the link. NIST does some good and important stuff. +0
deceptikon 1,790 Code Sniper Team Colleague Featured Poster

in·her·i·tance (n)

  1. money, property, etc., that is received from someone when that person dies
  2. something from the past that is still important or valuable
  3. the act of inheriting something
rubberman commented: LOL! Like the set of encyclopedias I inherited from my mother. +12
deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Nope, you pretty much nailed it. When swapping floating-point using arithmetic, there's a strong chance that rounding errors will muck up the original values. This problem doesn't exist with a temporary variable or integral types.

You can get around it by working with the bits directly (ie. the arithmetic approach is poor in this case), but then it becomes a question of whether the solution is complicated so much as to negate any savings of avoiding a temporary variable in the first place. And that's assuming there were any savings at all, taking compiler optimizations into consideration.

Another problem with the arithmetic approach is overflow and underflow, but that's another discussion. Ultimately I don't like that solution period. ;)

Long story short, swapping without a temporary variable is a neat trick, but only suitable for very niche and low level situations.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

So, why are we dividing the thing in headers(declarations) & cpp (defintions) ? We can write defintions in headers also.

That introduces the problem of multiple definitions. C doesn't really have an explicit one definition rule like C++, but in practice it's there. You can have as many redundant declarations as you want, but the instant you have two of the same definition in a translation unit, you're borked.

Secondly, In your example, as printf defintions is changed, then we need to change all the usages of the printf() also as you do. In that case also, my code will crash because I am using the old format only. If we can change all the lines where printf is used so as to make our code compatiable, then we can change the declartion also. what's the major use?

I was hoping you'd catch on to that. The answer is yes indeed, though fixing the declarations adds yet another thing you need to remember to change.

Now for the example I really wanted to give originally. Here's what actually happened when C99 changed declarations. Before, printf looked like this:

int printf(const char *fmt, ...);

Afterward, it looked like this:

int printf(const char * restrict fmt, ...);

On top of being a tossup as to whether that will cause compilation errors, how would you know about the change without closely monitoring the C standard?

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

The solution I gave will work for the int surely but it might give wrong for the double. Am I right?

You are correct, sir! And why is that? :)

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Actually, I am confused that what the problem was there in C and how c++ has solved it.

Let's say you have two libraries with a print function in C. How do you use them both at the same time? The answer is you don't, at least not without writing a wrapper library of your own that uses unique names to resolve the conflict. In C++, those functions would (should!) be wrapped in a namespace and there's no problem from the start.

For ex: if I say 'string' that means diffferent if I say std::string. Right?

Conceptually, yes. It's complicated with things like using namespace std, but the core of namespaces is indeed to ensure a unique name.

Headers has just the declarations as I already know. namespaces include all the definitons? right?

Namespaces don't include anything. A more accurate way to describe it would be that headers contain declarations and declarations may be wrapped in namespaces to guarantee uniqueness.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

but how is it resolved by compiler that which function to call?

Compiler magic. ++T1 gets converted to T1.operator++() and T2++ gets converted to something like T2.operator++(0) under the hood. There's no ambiguity to resolve, the compiler recognizes which operator you used and calls the correct overload.

nitin1 commented: extremely awesome ;) You always explain awesomly. how? ;) +0
deceptikon 1,790 Code Sniper Team Colleague Featured Poster

I have updated my comment. I have added one problem in the last comment. Please see that also.

It would be better to do that in a new reply rather than interleave new stuff into the thread. But anyway:

std::cout<<str.foo()<<std::endl;

What does foo return?

Secondly, why don't we have namespce thing in C?

Probably because nobody came up with a good solution to the problem before C++ came out and became popular.

Why do we need it in C++?

Because it's an obvious pain point in C, and C++ was originally intended to be a "better" C.

When namespaces are resolved in the cycle of compilation to execution?

Hmm, I do not know (or if I ever did, can't remember).

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

"Number" is ambiguous. Let's throw a wrench into the mix:

double a, b;

Beware, the answer isn't as simple as you think. :D

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Why will I need to update each and every source file?

For fun, let's use printf as our example even though it's unlikely to change for the forseeable life of C.

int printf(const char *fmt, ...);

int main(void)
{
    printf("Hello, world!\n");
    return 0;
}

You do that in every source file where you use printf, right? Because you don't want to include stdio.h yet still need a declaration. Now the standards committee decides to add another required parameter to printf and you upgrade to a compiler that implements this change. Suddenly your code breaks because printf can't be found, and you must modify the declaration in all of those files (once again, because you didn't want to include stdio.h):

int printf(const char *fmt, int level, ...);

int main(void)
{
    printf("Hello, world!\n", 0);
    return 0;
}
deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Why does it need int in postfix and not in prefix?

This is a case of Bjarne Stroustup being too clever. The two member functions need a different signature to resolve ambiguity between them. C++ doesn't differentiate between return types when comparing signatures, so it has to be done with either a function name or parameters. In the case of operator overloading, you can't change the name, so the only option without adding a new language feature is to require a dummy parameter. That's what the int parameter for postfix ++ does.

nitin1 commented: Hats off !! never knew that!! Cool. +4
deceptikon 1,790 Code Sniper Team Colleague Featured Poster

But, when I use using namespace std, then I also have to use "string" header file. So what is the difference between them?

The header provides you with declarations. using namespace std simply tells the compiler to pretend you had qualified a name with std:: if it can't resolve the name from the global namespace. The declaration is still required, otherwise the name won't resolve regardless of which namespaces you open up.

If everything is defined in namespace, then we need header and cpp files?

Yes. The thing has to exist before you can start monkeying around with how you refer to it.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster
  • my fingers are on "A", "W" and "D" keys

Yup, I've got that one. Another is that I have a tendency to end sentences with a semicolon due to excessive coding in languages that use a semicolon as a statement terminator.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Is that a Julian date format? I was assuming you had something like an ISO 8601 formatted date, but this complicates matters.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Double check that your credentials are correct. I've seen this happen when the password expires or is changed.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

If it's just a data drive, it should work straight away. Depending on the OS, you may have to mount it first, but Windows will detect the drive automatically. Depending on the drive, you may need to format it, but I think most drives these days come preformatted with NTFS.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Just try out this code. Hope it may be useful for you.

That code is pretty awful, even after adding reasonable indentation. Let's hit the low hanging fruit first.

#include<conio.h>

This is a good indicator of bad code, because it's usually unnecessary. Further look at the code confirms that this is the case for your code. All you've done is hurt portability by requiring a non-portable library when it's not needed.

void main()

This is also non-portable. If the compiler supports it, okay. If not, you've invoked undefined behavior. Not a good trade off. Especially when the code can be made correct across the board by using int main() and returning 0.

int w = 0, i = 0, j = 0, l, a[10], big;
char ch[25], st[10][20];

Variable names suck and are uninformative. The code is much harder to read as a result.

clrscr();

Unneeded, non-portable, antisocial, and anyone who does this should be ashamed. Put simply, if you create a console program that doesn't explicitly own the console and clears the screen, you'll piss off a lot of users who find the results of previous programs suddenly erased. Or maybe not, because nobody will want to use your program.

printf("Enter the string:");

stdout isn't flushed, so there's no guarantee this prompt will be visible before blocking for input. Unless you print a newline in the prompt, it's a good idea to call fflush.

printf("Enter the …
nitin1 commented: "anyone who does this should be ashamed" ... little bit funny. :-) +4
deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Yup, variables in a nested scope hide variables with the same name in parent scopes.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Since you're already using C++/CLI, why not initialize a DateTime object with the String^ and then use the ToLocalTime method?

What exactly does the string contain? Is it a standard UTC format?

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

I think a better question is why did you reach the point where malloc is failing? With virtual memory being standard operating procedure in modern OSes, it should be extremely rare unless you're doing something silly.

Can you elaborate on what problems you're doing and how you're solving them?